Search code examples
chexmicrocontrolleruart

Uart communication with Hex-Code in C


I try to communicate with a olfactory sensor via uart and Hex-code, but it does not work, the sensor doesn't react on the manufaturers given commands.

Is there somethings wrong with my write function and the uint_8 array?

//declaration of the hex array

uint8_t START_USM[7];

    START_USM[0]=0x01;
    START_USM[1]=0x00;
    START_USM[2]=0x03;
    START_USM[3]=0x31;
    START_USM[4]=0x00;
    START_USM[5]=0x00;
    START_USM[6]=0x00;

// function for transmitting data

int commmandWrite2(int fileDescriptor, uint8_t *START_USM){

    int i=0;
    for(; i< COM_LEN; i++){

        int n = write(fileDescriptor,&START_USM[i],1);
        tcdrain(fileDescriptor);

    }
}

Solution

  • int commmandWrite2(int fileDescriptor, uint8_t *START_USM){
    
        int i=0;
        for(; i< COM_LEN; i++){
    
            int n = write(fileDescriptor,&START_USM[i],sizeof(uint8_t ));
    
        }
        tcdrain(fileDescriptor);
    }
    

    if the above works, than you can optimize it to

    int commmandWrite2(int fileDescriptor, uint8_t *START_USM){
           int n = write(fileDescriptor,START_USM,sizeof(uint8_t )*COM_LEN);
            tcdrain(fileDescriptor);
    }