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

时间:2013-02-18 09:44:20

标签: c linux serial-port at-command

当然有termios.h,但我在这里谈的是AT命令。我希望他们能够执行

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

2 个答案:

答案 0 :(得分:3)

看看这个简短的例子(它有效):

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...

答案 1 :(得分:1)

偶然发现,可能有所帮助:

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

也是C语言中高级Linux编程的权威指南 http://www.advancedlinuxprogramming.com/alp-folder/