Question on Serial Communication in Unix using C/C++

Hi,

I have been trying to write a simple code to to send an AT Command through a program. The two serial ports of my Unix machine is connected back to back via a null modem cable. I want to send an AT command on port say /dev/ttya for an application listening on /dev/ttyb and then print the scho back to the port /dev/ttya. I tried using read and write system commands, but I was unable to communicate. The code is as follows. Can anybody please tell me what might be wrong ?

#include
#include
#include
#include
#include
#include
#include
#include
#include

/*
* Serial port tester
* Subhayan Dutta -
* 38400 baud by default, settable via -19200 or -9600 flags
* first non-baud argument is tty (e.g. /dev/term/a)
* second argument is file name (e.g. /etc/hosts)
*/

#define BUFSIZE 1000

int main(int argc, char *argv[])
{
int fd;
int file, count, i, retval;
unsigned char io[BUFSIZ], buff[BUFSIZE], *buff2 = buff, writebuffer[BUFSIZE+2];
int rdsz, writeval;
long baud;
struct termios tp;

if ( argc < 3 )
{
printf( "Invalid number of args
" );
return 0;
}
if (strcmp(argv[1], "-9600") == 0) {
baud = B9600;
argv++; argc--;
} else if (strcmp(argv[1], "-19200") == 0) {
baud = B19200;
argv++; argc--;
} else
baud = B19200; /* default baud rate is 19200 */
/*baud = B38400;*/

if ((fd = open(argv[1], O_RDWR)) < 0) {
perror("bad terminal device, try another");
exit(-1);
}

if (tcgetattr(fd, &tp) < 0) {
perror("Couldn't get term attributes");
exit(-1);
}
/* 8 bits + baud rate + local control */
tp.c_cflag = CS8|CLOCAL|baud;
tp.c_oflag = 0;
/* XON/XOFF flow control, ignore CR, ignore parity */
tp.c_iflag = IXON|IGNBRK|IGNCR|IGNPAR;
tp.c_lflag = 0;
/* set output and input baud rates */
if ( cfsetospeed(&tp, baud) < 0 )
{
perror( "Could not set output baud rate " );
exit(-1);
}
if ( cfsetispeed(&tp, baud) < 0 )
{
perror( "Could not set input baud rate " );
exit(-1);
}
if (tcsetattr(fd, TCSANOW, &tp) < 0) {
perror("Couldn't set term attributes");
exit(-1);
}

file = 0;
/* open file to send to port */
/*if ((file = open(argv[2], O_RDONLY)) < 0) {
perror("couldn't open file");
exit(1);
}*/

/* dump it to the port */
/*while ((rdsz = read(file, io, BUFSIZ)) > 0) */

rdsz = strlen( argv[2] ) + 2;
strcpy( buff2, argv[2] );
strcat( buff2, "
" );
printf( "buff2 = %s
", buff2 );

/*writebuffer[0] = 2;
strcat( writebuffer, buff2 );
writebuffer[1+rdsz] = 3;*/

printf( "Data to write to serial port :
" );
for( count = 0; count < rdsz; count++ )
{ unsigned char tt = buff2[count];
printf( "%02X", (unsigned int)tt );
}
printf( "
" );
if ( ( writeval = write(fd, (void*)buff2, rdsz) ) < 0 )
{
perror( "Error in writing : " );
exit(-1);
}
else
{
printf( "Write sucessful
" );
}
printf( "Wrote %d bytes
", writeval );

if ( ( retval = read( fd, (void*)buff, 100 ) ) < 0 )
{
perror( "Error in reading from file " );
return 0;
}
for( i = 0; i < retval; i++ )
printf( "%02X", (unsigned int)buff[i] );
printf( "
" );
if ( close(fd) < 0 )
{
perror( "Error in closing file " );
}
else
{
printf( "Closing of serial port succeeded
" );
}
return (0);
}


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories