简体   繁体   中英

How to send AT commands to serial port through C - Linux

Of course there is termios.h , but here I am talking about AT commands. I want them to get executed .

How to send AT commands to serial port through C in Linux so that they get executed ?

Look at this brief example (it works):

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

Stumbled across this, might help:

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

Also the definitive guide for Advanced Linux Programming in C http://www.advancedlinuxprogramming.com/alp-folder/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM