linux串口读取问题

linux串口读取问题,第1张

首先你确定你那串口是否有东西可读? 就是你上面说的“一个文件不停的写数据到串口”!你可以先不这样读取,你可以在终端上用cat试试是否有数据可读:cat /dev/ttyS0

如果有的话,那你就检查串口设置是否正确,如波特率,数据位,停止位,校验位等!

最后就是你读取的函数了,看看先不要用printf打印字符串了,先看看十六进制是否有,然后再看字符等!

就是以上一些,你还可以参考Linux下串口文档,百度上很多的……

这样写肯定会写死的啊..写(也就是串口发送数据)实际应用不会这么频繁的.楼主可以在写后加上一个sleep(1)这样就不会写死了..

高并发读写这样设计也是错误的应该用队列来处理..

#include<stdio.h>

#include<stdlib.h>

#include<unistd.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<fcntl.h>

#include<termios.h>

#include<errno.h>

#define FALSE -1

#define TRUE 0

int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,B38400, B19200, B9600, B4800, B2400, B1200, B300, }

int name_arr[] = {38400,  19200,  9600,  4800,  2400,  1200,  300, 38400, 19200,  9600, 4800, 2400, 1200,  300, }

void set_speed(int fd, int speed){

  int   i 

  int   status 

  struct termios   Opt

  tcgetattr(fd, &Opt) 

  for ( i= 0  i < sizeof(speed_arr) / sizeof(int)  i++) { 

    if  (speed == name_arr[i]) {     

      tcflush(fd, TCIOFLUSH)     

      cfsetispeed(&Opt, speed_arr[i])  

      cfsetospeed(&Opt, speed_arr[i])   

      status = tcsetattr(fd, TCSANOW, &Opt)  

      if  (status != 0) {        

        perror("tcsetattr fd1")  

        return     

      }    

      tcflush(fd,TCIOFLUSH)   

    }  

  }

}

int set_Parity(int fd,int databits,int stopbits,int parity)

struct termios options 

if  ( tcgetattr( fd,&options)  !=  0) { 

perror("SetupSerial 1")     

return(FALSE)  

}

options.c_cflag &= ~CSIZE 

switch (databits) 

{   

case 7:

options.c_cflag |= CS7 

break

case 8:     

options.c_cflag |= CS8

break   

default:    

fprintf(stderr,"Unsupported data size\n") return (FALSE)  

}

switch (parity) 

{   

case 'n':

case 'N':    

options.c_cflag &= ~PARENB   /* Clear parity enable */

options.c_iflag &= ~INPCK     /* Enable parity checking */ 

break  

case 'o':   

case 'O':     

options.c_cflag |= (PARODD | PARENB) 

options.c_iflag |= INPCK             /* Disnable parity checking */ 

break  

case 'e':  

case 'E':   

options.c_cflag |= PARENB     /* Enable parity */    

options.c_cflag &= ~PARODD    

options.c_iflag |= INPCK       /* Disnable parity checking */

break

case 'S': 

case 's':  /*as no parity*/   

    options.c_cflag &= ~PARENB

options.c_cflag &= ~CSTOPBbreak  

default:   

fprintf(stderr,"Unsupported parity\n")    

return (FALSE)  

}  

switch (stopbits)

{   

case 1:    

options.c_cflag &= ~CSTOPB  

break  

case 2:    

options.c_cflag |= CSTOPB  

   break

default:    

 fprintf(stderr,"Unsupported stop bits\n")  

 return (FALSE) 

/* Set input parity option */ 

if (parity != 'n')   

options.c_iflag |= INPCK 

tcflush(fd,TCIFLUSH)

options.c_cc[VTIME] = 150 

options.c_cc[VMIN] = 0 /* Update the options and do it NOW */

if (tcsetattr(fd,TCSANOW,&options) != 0)   

perror("SetupSerial 3")   

return (FALSE)  

return (TRUE)  

}

int main()

{

printf("This program updates last time at %s   %s\n",__TIME__,__DATE__)

printf("STDIO COM1\n")

int fd

fd = open("/dev/ttyS0",O_RDWR)

if(fd == -1)

{

perror("serialport error\n")

}

else

{

printf("open ")

printf("%s",ttyname(fd))

printf(" succesfully\n")

}

set_speed(fd,115200)

if (set_Parity(fd,8,1,'N') == FALSE)  {

printf("Set Parity Error\n")

exit (0)

}

char buf[] = "fe55aa07bc010203040506073d"

write(fd,&buf,26)

char buff[512]

int nread

while(1)

{

if((nread = read(fd, buff, 512))>0)

{

printf("\nLen: %d\n",nread)

buff[nread+1] = '\0'

printf("%s",buff)

}

}

close(fd)

return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/5859880.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-02-22
下一篇 2023-02-22

发表评论

登录后才能评论

评论列表(0条)

保存