2017-11-11 253 views
0

我正在研究一个在Linux环境下编程HM-TRP无线电模块的C程序,因为后来我将编程约40个这样的模块,我想要一直输入单个编程代码来编程每一个编程代码。配置模块为115kbps,然后系统将端口重置为9600波特

在我的机器上我有三个终端窗口设置。一个是编译程序并运行它。一个用于发送数据到串行端口(用于测试),另一个用于接收来自无线电模块的响应。

这是一步一步如何解决问题。

  1. 冉程序。并收到预期的消息:

    HM-TRP模块配置

    设置HM-TRP首次... 使用115kbps的 配置完成

  2. 切换到新窗口(我称之为#2)并执行“stty -aF/dev/ttyS0”以查看已设置的设置。结果(如图所示)暗示我,我的C程序确定运作:

    speed 115200 baud; rows 0; columns 0; line = 0; 
    intr = ^C; quit = ^\; erase = ^?; kill = ^H; eof = ^D; eol = <undef>; eol2 = <undef>; 
    swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; 
    flush = ^O; min = 1; time = 0; 
    parenb -parodd cs8 -hupcl -cstopb cread clocal -crtscts 
    ignbrk -brkint ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon -ixoff -iuclc -ixany 
    -imaxbel -iutf8 
    -opost -olcuc -ocrnl -onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0 
    -isig -icanon -iexten -echo -echoe -echok -echonl -noflsh -xcase 
    -tostop -echoprt -echoctl 
    -echoke 
    
  3. 开始 “屏幕” 的程序与参数 “的/ dev/ttyS0来115200” 为115K的速度。

  4. 打开一个新的窗口(我称之为#3),并执行一个有效的命令到模块如下:

    echo -en "\xAA\xFA\x96\x07" > /dev/ttyS0 
    

这里的问题是,“画面”的报告什么时候它应该打印“确定”。于是我继续...

  • 我重温窗口#2终止屏幕程序,然后我“的/ dev/ttyS0来9600”再次重新启动屏幕为9600波特的速度刚看看发生了什么。我再次重复步骤4,这次出现“OK”。
  • 但奇怪的是,当我离开屏幕程序运行9600波特,我执行我的C程序时,屏幕报告“OK”和(正如我期待的)一行垃圾向我暗示:最后的命令序列到收音机模块:

    \xAA\xFA\x1E\x00\x01\xC2\x00 
    

    实际上确实显示波特率的变化,只是从看垃圾的结果。

    所以接下来的问题是,为什么速率会重置为9600bps?即使我在新窗口中验证了正确的速率,波特率值是虚拟化还是应用程序之间的隔离?或者有什么我失踪?

    我的意思是我没有问题,建立模块,如果我只是吐出每个长码与UNIX命令行回声声明,但我想在C.

    创建程序这是代码,对于参数,我使用/dev/ttyS0 0,其中/ dev/ttyS0是我的串行端口(COM1)。

     #include <sys/stat.h> 
        #include <sys/types.h> 
        #include <sys/io.h> 
        #include <unistd.h> 
        #include <stdio.h> 
        #include <stdlib.h> 
        #include <string.h> 
        #include <termios.h> 
        #include <fcntl.h> 
    
        void outs(int fd, char* n,unsigned int sz){ 
        char *d=n; 
        unsigned int nwrt=0,x=0; 
        for (x=0;x<sz;x++){ //send byte one by one 
         while ((nwrt=write(fd,d,1))==0){ 
        usleep(10000); //waste CPU cycles passively until byte is sent 
         }; 
         d++; 
        } 
        } 
    
        int openser(speed_t speed,const char* dev){ 
        struct termios options; 
        //open serial port 
        int fd = open(dev, O_RDWR | O_NOCTTY | O_SYNC);if (fd < 0){printf("ERROR\n");return -1;} 
        memset(&options,0,sizeof options); 
        tcflush(fd,TCIOFLUSH); 
        tcgetattr(fd,&options); 
        //set baud rate 
        cfsetispeed(&options, speed); 
        cfsetospeed(&options, speed); 
        options.c_iflag = IGNBRK | IGNPAR; //raw input 
        options.c_cflag |= (TOSTOP|NOFLSH|CLOCAL|CREAD|CS8);//ignore modem + parity: 8N1 + no rtscts 
        options.c_lflag=0; //raw input 
        options.c_oflag=0;options.c_cc[VMIN]=1;options.c_cc[VTIME]=0; //raw output 
        tcsetattr(fd, TCSANOW, &options); 
        return fd; 
        } 
    
        int main(int argc,char* argv[]){ 
        printf("HM-TRP module config\n\n"); 
        int rd=0; 
        setbuf(stdout,NULL); 
        if (argc < 2){printf("serial device + function required as argument.\n");return -1;} 
        //open serial port for 115kbps baud 
        rd=openser(115200L,argv[1]);if (rd == 0){return -1;} 
        // get function 
        int funcn=strtol(argv[2],NULL,10); 
        switch (funcn){ 
        case 0: //function 0. 1st time setup. set things up at 9600 baud then switch to 115kbps 
        printf("Setting up HM-TRP for first time...\n"); 
        close(rd);rd=openser(9600L,argv[1]);if (rd == 0){return -1;} 
        case 1: 
        //set baud + wireless speed to 115kbps 
        printf("Using 115kbps\n"); 
        outs(rd,"\xAA\xFA\xC3\x00\x01\xC2\x00",7); 
        outs(rd,"\xAA\xFA\x1E\x00\x01\xC2\x00",7); 
        break; 
        default: 
        //reset option in case something went wrong 
        printf("Resetting HM-TRP to defaults - 9600bps\n"); 
        outs(rd,"\xAA\xFA\xF0",3); 
        break; 
        } 
        close(rd); 
        printf("Config Done\n"); 
        return 0; 
        } 
    

    我在做什么错?

    回答

    0

    经过一番发现,事实证明我需要在发送命令后添加一个延迟,因为无线电模块只有半双工,因此它实际上忽略了导致速度指令不被识别的一半命令。 A usleep(100000)在每个输出函数调用之后为我做了诀窍。