Hi
I try to communicate with a GSM modem, from C, for sending SMS.
I use standart AT-commands.
Working well with terminal.
There is no problem writing ti the port.
But when I try to read I only get a echo, I write "ATI" and get "ATI" back, I should get somthing like "SIEMENS 35 Terminal..." back.
Thanks for some help!
//
// Skriv och las fran com-port
//
// Av: David Miller
//
// Andringshistorik:
//
// Datum Sign Kommentar
// ----------------------------------------------------
// 12-05-21 DM Grundversion
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#include <time.h>
main()
{
int fd;
struct termios options;
char b[255]; // Input buffer
char *bufptr; // Current char in buffer
int nbytes; // Number of bytes read
int tries; // Number of tries so far
int x;
// open the port
fd = open("/dev/ttyS0",O_RDWR | O_NOCTTY | O_NDELAY );
fcntl(fd, F_SETFL, 0);
// get the current options
tcgetattr(fd, &options);
tcsetattr(fd, TCSAFLUSH, &options); // activate the set options
cfsetispeed(&options, B9600); // 9600 baud input
cfsetospeed(&options, B9600); // 9600 baud output
options.c_cflag |= (CLOCAL | CREAD); // enable receiver, local mode
options.c_lflag &= ~( ICANON | ECHO | ECHOE | ISIG | IEXTEN ); // ensure raw input
options.c_oflag &= ~OPOST; // ensure raw output
// set the options
tcsetattr(fd, TCSANOW, &options);
printf("\nOpen: %d", fd);
x = write(fd, "ATI\t", 4);
printf("\nWrite: %d", x);
sleep(1);
x = 0;
x = read(fd, b, 30);
printf("\nRead: %s", b);
close(fd);
}