从Raspberry PI的uart中读取数据

时间:2016-08-01 04:45:35

标签: python c++ linux uart raspberry-pi3

我正在测试从Raspberry PI& uart读取数据。 我有USB转UART适配器并连接PI的USB端口和PI的UART(g,Rx,Tx)。 然后运行USB端口的代码。代码每100毫秒将数据发送到uart。

#!/usr/bin/env python
import time
import serial
ser = serial.Serial(
    port='/dev/ttyUSB0',
    baudrate = 9600,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS,
    timeout=1
)
counter=0
while 1:
  print counter
  if counter>=1000:
      ser.write('\r')
  else:
       ser.write('%d \n'%(counter))
  time.sleep(0.1)//100msec
  counter += 1

然后在我的UART端,C ++程序从UART读取数据。 代码如下,每100毫秒I call the ReadData_helper function using a timer

void InterfaceUART::PortInitialization()
{
        fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY | O_NDELAY ); 
     if (fd <0) {perror(MODEMDEVICE); exit(-1); }

     tcgetattr(fd,&oldtio); /* save current serial port settings */
     bzero(&newtio, sizeof(newtio)); /* clear struct for new port settings */

        /* 
          BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed.
          CRTSCTS : output hardware flow control (only used if the cable has
                    all necessary lines. See sect. 7 of Serial-HOWTO)
          CS8     : 8n1 (8bit,no parity,1 stopbit)
          CLOCAL  : local connection, no modem contol
          CREAD   : enable receiving characters
        */
         newtio.c_cflag = B9600 | CRTSCTS | CS8 | CLOCAL | CREAD;

        /*
          IGNPAR  : ignore bytes with parity errors
          ICRNL   : map CR to NL (otherwise a CR input on the other computer
                    will not terminate input)
          otherwise make device raw (no other input processing)
        */
         newtio.c_iflag = IGNPAR | ICRNL;

        /*
         Raw output.
        */
         newtio.c_oflag = 0;

        /*
          ICANON  : enable canonical input
          disable all echo functionality, and don't send signals to calling program
        */
         newtio.c_lflag = ICANON;

     /*set signal*/
         //saio.sa_handler = signal_handler_IO;
         //saio.sa_flags = 0;
         //saio.sa_restorer = NULL; 
         //sigaction(SIGIO,&saio,NULL);

         fcntl(fd, F_SETFL, FNDELAY);//|FASYNC//FNDELAY ,#FNDELAY makes read function to return with 0 if no characters are available on the port and if FNDELAY == 0, it is blocking mode
         fcntl(fd, F_SETOWN, getpid());

    /* 
      initialize all control characters 
      default values can be found in /usr/include/termios.h, and are given
      in the comments, but we don't need them here
    */
     newtio.c_cc[VINTR]    = 0;     /* Ctrl-c */ 
     newtio.c_cc[VQUIT]    = 0;     /* Ctrl-\ */
     newtio.c_cc[VERASE]   = 0;     /* del */
     newtio.c_cc[VKILL]    = 0;     /* @ */
     newtio.c_cc[VEOF]     = 4;     /* Ctrl-d */
     newtio.c_cc[VTIME]    = 0;     /* inter-character timer unused */
     newtio.c_cc[VMIN]     = 1;     /* blocking read until 1 character arrives */
     newtio.c_cc[VSWTC]    = 0;     /* '\0' */
     newtio.c_cc[VSTART]   = 0;     /* Ctrl-q */ 
     newtio.c_cc[VSTOP]    = 0;     /* Ctrl-s */
     newtio.c_cc[VSUSP]    = 0;     /* Ctrl-z */
     newtio.c_cc[VEOL]     = 0;     /* '\0' */
     newtio.c_cc[VREPRINT] = 0;     /* Ctrl-r */
     newtio.c_cc[VDISCARD] = 0;     /* Ctrl-u */
     newtio.c_cc[VWERASE]  = 0;     /* Ctrl-w */
     newtio.c_cc[VLNEXT]   = 0;     /* Ctrl-v */
     newtio.c_cc[VEOL2]    = 0;     /* '\0' */

    /* 
      now clean the modem line and activate the settings for the port
    */
     tcflush(fd, TCIFLUSH);
     tcsetattr(fd,TCSANOW,&newtio);

        return;
}

void *InterfaceUART::ReadData(void *p){     
      //InterfaceUART *app = (InterfaceUART*)p;
      res = read(fd,buf,255); 
    //buf[res] = 0;    
      float *a = (float*)malloc(sizeof(float));
      *a = (float)atof(buf);
      //pthread_exit((void*)&a); //success
      return (void*)a;
}
void *InterfaceUART::ReadData_helper (void *p){
    InterfaceUART *app = (InterfaceUART*)p;
    return ((InterfaceUART*)p)->ReadData(app);
}

接口可以工作一段时间,然后重复读取相同的数据。

deg is : 0 speed is : 135
time diff 0
deg is : 0 speed is : 136
time diff 1
deg is : 0 speed is : 137
time diff 0
deg is : 0 speed is : 138
time diff 0
deg is : 0 speed is : 139
time diff 0
deg is : 0 speed is : 140
time diff 0
deg is : 0 speed is : 141
time diff 1
deg is : 0 speed is : 142
time diff 0
deg is : 0 speed is : 143
time diff 1
deg is : 0 speed is : 144
time diff 0
deg is : 0 speed is : 145
time diff 0
deg is : 0 speed is : 146
time diff 0
deg is : 0 speed is : 147
time diff 0
deg is : 0 speed is : 148
time diff 1
deg is : 0 speed is : 149
time diff 1
deg is : 0 speed is : 150
time diff 0
deg is : 0 speed is : 151
time diff 0
deg is : 0 speed is : 152
time diff 0
deg is : 0 speed is : 153
time diff 0
deg is : 0 speed is : 154
time diff 1
deg is : 0 speed is : 155
time diff 0
deg is : 0 speed is : 156
time diff 1
deg is : 0 speed is : 156
time diff 0
deg is : 0 speed is : 156
time diff 1
deg is : 0 speed is : 156

正确读取155,在156读取数据。 156不是固定的,它有时会随机停止71,有时是153等。达到一个数字后再也不会改变。

可能出现什么问题?

谢谢

0 个答案:

没有答案