资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

linux命令行读串口,linux串口读取命令

linux如何查看哪个串口是真实串口,哪个串口

1.使用ls -l ttyS*命令显示如下

创新互联公司-专业网站定制、快速模板网站建设、高性价比南通网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式南通网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖南通地区。费用合理售后完善,十余年实体公司更值得信赖。

crw-rw----. 1 root dialout 4, 64 5月 17 02:24 /dev/ttyS0

crw-rw----. 1 root dialout 4, 65 5月 17 02:24 /dev/ttyS1

crw-rw----. 1 root dialout 4, 66 5月 17 02:24 /dev/ttyS2

crw-rw----. 1 root dialout 4, 67 5月 17 02:24 /dev/ttyS3

但你不知到哪个是真实的串口,虽然一般都是ttyS0,但也不敢妄然确定。

2.使用cat /proc/tty/driver/serial

serinfo:1.0 driver revision:

0: uart:16550A port:000003F8 irq:4 tx:0 rx:0

1: uart:unknown port:000002F8 irq:3

2: uart:unknown port:000003E8 irq:4

3: uart:unknown port:000002E8 irq:3

我们发现串口0的uart值时16550A,tx值为0,rx值也为0,因此我们断定本机只有一个串口,是串口0,即ttyS0

3.也可以用dmesg | grep ttyS*,但这个不是很好用,当然你可以自己使用正则法则取找到。

注意:还应查看是否有USB转串口,这个就很简单了:ls ttyUSB*,全部搞定。

linux怎么读取串口数据

#includestdio.h

#includestdlib.h

#includeunistd.h

#includesys/types.h

#includesys/stat.h

#includefcntl.h

#includetermios.h

#includeerrno.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 = ~CSTOPB;break;  

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;

}

如何在linux上使用串口设备

简单的运行 dmesg 命令

$ dmesg | grep tty

输出:

[ 37.531286] serial8250: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A

[ 37.531841] 00:0b: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A

[ 37.532138] 0000:04:00.3: ttyS1 at I/O 0x1020 (irq = 18) is a 16550A

setserial 命令

setserial 是一个程序用于设定并/或报告某个串口关联的配置信息。该信息包括串口用到的I/O 端口和中断号,以及Break键是否应被解释为Secure Attention Key 等等。 仅仅是输出如下的命令:

$ setserial -g /dev/ttyS[0123]

输出:

/dev/ttyS0, UART: 16550A, Port: 0x03f8, IRQ: 4

/dev/ttyS1, UART: 16550A, Port: 0x1020, IRQ: 18

/dev/ttyS2, UART: unknown, Port: 0x03e8, IRQ: 4

/dev/ttyS3, UART: unknown, Port: 0x02e8, IRQ: 3

带-g选项的setserial帮助找到你的Linux板子上的物理串口。

Linux 串口控制台程序

一旦串口被确定了,你就能使用许多的工具来配置Linux板子:

minicom- 用于控制modem和连接到dump 设备的最好的串口通信程序。

wvidial or other GUI dial up networking program - 一个内建智能PPP 拨号器。

getty / agetty - agetty 打开一个 tty 端口, 提示登录名称并调用 /bin/login 命令。

grub / lilo configuration - 配置串口为系统控制台。

Linux普通用户运行串口

将USB串口设备插入USB口后,会在/dev/目录下生成/dev/ttyUSB0文件(也可能为/dev/ttyUSB1,/dev/ttyUSB2...),

查看此文件

输出为:

c说明表明设备为字符设备文件(d表示目录文件,-表示普通文件,l表示链接文件,b表示块文件),

其中rw-rw----表示root用户作为文件所有者可以读和写,dialout用户组内的用户可以读和写,其他用户不允许读、写和执行(r表示可读,w表示可写,x表示可执行)

因此,需要将当前用户增加到dialout用户组中


标题名称:linux命令行读串口,linux串口读取命令
网页路径:http://cdkjz.cn/article/dsgsshe.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220