rhysmeister
Programmer
Been playing around with serial programming. I think I now have a good grasp of the concepts and syntax involved but I am having some problems. My code appears to be able to write happily to the serial port and appear to happily wait for input to arrive. No input ever seems to arrive from the modem.
I have confirmed the modem is functioning correctly. The serial port is fine (dual boot with XP) with hyperterminal. I have checked the modem documentation and ensure the serial settings are correct. I'm 100% sure all hardware is working.
I have various versions of this program. The one included below is a stripped down version. The configure_port function only sets the options that the modem manual mentions. I did include others in previous versions but had the same problems.
If anyone could help me on this it would be great!
I have confirmed the modem is functioning correctly. The serial port is fine (dual boot with XP) with hyperterminal. I have checked the modem documentation and ensure the serial settings are correct. I'm 100% sure all hardware is working.
I have various versions of this program. The one included below is a stripped down version. The configure_port function only sets the options that the modem manual mentions. I did include others in previous versions but had the same problems.
If anyone could help me on this it would be great!
Code:
/////////////////////////////////////////////////
// Serial port interface program //
/////////////////////////////////////////////////
#include <stdio.h> // standard input / output functions
#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 definitionss
#include <time.h> // time calls
int open_port(void)
{
int fd; // file description for the serial port
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if(fd == -1) // if open is unsucessful
{
perror("open_port: Unable to open /dev/ttyS0 - ");
}
else
{
fcntl(fd, F_SETFL, 0);
}
return(fd);
}
int configure_port(int fd) // configure the port
{
struct termios port_settings; // structure to store the port settings in
cfsetispeed(&port_settings, B9600); // set baud rates
cfsetospeed(&port_settings, B9600);
port_settings.c_cflag &= ~PARENB; // set no parity, stop bits, data bits
port_settings.c_cflag &= ~CSTOPB;
port_settings.c_cflag &= ~CSIZE;
port_settings.c_cflag |= CS8;
tcsetattr(fd, TCSANOW, &port_settings); // apply the settings to the port
return(fd);
}
int query_modem(int fd) // query modem with an AT command
{
int n;
fd_set rdfs;
struct timeval timeout;
// initialise the timeout structure
timeout.tv_sec = 10; // ten second timeout
timeout.tv_usec = 0;
write(fd, "AT\r", 3); // send an AT command followed by a CR
// do the select
n = select(fd + 1, &rdfs, NULL, NULL, &timeout);
// check if an error has occured
if(n < 0)
{
perror("select failed\n");
}
else if (n == 0)
{
puts("Timeout!");
}
else
{
printf("\nBytes detected on the port!\n");
}
}
int main(void)
{
int fd = open_port();
configure_port(fd);
query_modem(fd);
return(0);
}