0

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?

Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411

2 Answers2

3

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...
Davide Berra
  • 6,387
  • 2
  • 29
  • 50
1

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/

Dane Balia
  • 5,271
  • 5
  • 32
  • 57
  • And to learn a bit more about AT commands, by all means read the V.250 standard, http://www.itu.int/rec/T-REC-V.250-200307-I/en. – hlovdal Mar 30 '13 at 23:12