如何通过C发送AT指令到串口 - Linux

问题描述:

当然有termios.h,但这里我正在谈论AT指令。我希望他们得到执行如何通过C发送AT指令到串口 - Linux

如何通过在Linux 中的C发送AT命令到串口,以便它们得到执行

+0

只是将这些命令作为文本发送。 – lqs 2013-02-18 09:56:03

+0

设备是* nix中的文件。尝试将串行端口作为文件打开,并从/向它读/写。 – 2013-02-18 09:56:11

+0

[串口读取与C写入]可能的重复(http://*.com/questions/3452359/serial-port-reading-and-writing-with-c) – 2013-02-18 09:57:50

看看这个简单的例子(它的工作原理):

struct termios options; 
int fd; 

fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY); 

if (fd < 0) 
{ 
    printf("Error opening serial port\n"); 
    exit(1); 
} 

bzero(&options, sizeof(options)); 
options.c_cflag = B9600 | CS8 | CLOCAL | CREAD | IGNPAR; 
tcflush(fd, TCIFLUSH); 
tcsetattr(fd, TCSANOW, &options); 

if (write(fd, "ATZ\r", 4) < 4) 
{ 
    printf("Write error - %s \n", strerror(errno)); 
    exit (1); 
} 

// read back for OK or KO then do your stuff... 
+0

谢谢,我想知道如何端口分辨在命令和文本之间。 – 2013-02-18 10:04:38

+1

这是Hayes与调制解调器进行通信的方法。它按照前面AT命令的格式进行区分。检查下面关于Serial_Programming的答案中的链接 – 2013-02-18 10:14:26

+0

@DaneBalia感谢您的跟进。 – 2013-02-18 10:47:15

过这个迷迷糊糊中,可能会有帮助:

http://en.wikibooks.org/wiki/Serial_Programming/Serial_Linux

还为高级Linux编程权威指南用C http://www.advancedlinuxprogramming.com/alp-folder/

+0

欲了解更多关于AT命令的信息,请阅读V.250标准http://www.itu.int/rec/T-REC-V.250-200307-I/en。 – hlovdal 2013-03-30 23:12:20