1

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?

3
  • just send these commands as text. Commented Feb 18, 2013 at 9:56
  • Devices are files in *nix. Try just opening the serial port as a file and read/write from/to it. Commented Feb 18, 2013 at 9:56
  • possible duplicate of Serial port reading and writing with C Commented Feb 18, 2013 at 9:57

2 Answers 2

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...
Sign up to request clarification or add additional context in comments.

3 Comments

thanks, I was wondering how does port distinguish between commands and text.
It's Hayes method for communicating with modems. It distinguishes by format that is the AT command prepended. Check link in answer below about Serial_Programming
@DaneBalia Thanks for following up.
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/

1 Comment

And to learn a bit more about AT commands, by all means read the V.250 standard, itu.int/rec/T-REC-V.250-200307-I/en.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.