用C语言中的Hex-Code进行Uart通信

时间:2016-01-21 12:34:30

标签: c hex microcontroller uart

我尝试通过uart和Hex代码与嗅觉传感器通信,但它不起作用,传感器不会对给定命令的制造商做出反应。

我的写函数和uint_8数组是否有问题?

//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);

    }
}

1 个答案:

答案 0 :(得分:0)

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);
}

如果以上工作,则可以将其优化为

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