data transfer bet RH8.0 pc to & fro device thru /dev/ttyS0
hi
i need to issue a command which is of ~20 hex bytes to a device connected to /dev/ttyS0 and in turn capture the ~24 hex bytes of response.it works fine under cygwin and dos.i feel that i ve not done the settings properly. i referred the serial prg howto. but unable to resolve the write operation thru /dev/ttyS0.
i have the same problem what togunter faced long back,
he had posted a message to the same forum with thread (below)
"writing to serial port, /dev/ttyS0"
- but there are no replies.
awaiting ur help
thanks a lot
shivani
Registered: Apr 2002
Location: Los Angeles
Distribution: Red Hat 7.0 & 7.2
Posts: 4
Hi all-
I'm trying to write some data to a device on the com1 port on my Redhat 7.2 box, but when I write data to /dev/ttyS0, the characters I write appear to get garbled(and i dont even seem to get the same number of characters).
The same code works fine under windows using cygwin to write to /dev/com1 (after using stty to determine that the serial port is setup the same as in linux).
the code i am using is below.
any ideas?
thanks for any help
--tim
//
// begin code:
//
#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 */
main()
{
int fd;
struct termios options;
int i ;
unsigned char data[20] = { 0x40, 0x28, 0x6b, 0xfe,
0x00, 0x00, 0x00, 0x00,
0x00, 0x23, 0x04, 0x00,
0x00, 0x00, 0x00, 0x00,
0xd9, 0xff, 0xff, 0xff} ;
// linux
//fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NONBLOCK);
// cygwin
fd = open("/dev/com1", O_RDWR | O_NOCTTY | O_NONBLOCK);
fcntl(fd, F_SETFL, 0);
/* get the current options */
tcgetattr(fd, &options);
cfsetispeed(&options, B57600);
cfsetospeed(&options, B57600);
/* set raw input, 1 second timeout */
options.c_cflag |= (CLOCAL | CREAD);
options.c_oflag &= ~OPOST;
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 10;
options.c_cflag &= ~PARENB; /* Clear parity enable */
options.c_iflag &= ~INPCK; /* Enable parity checking */
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_cflag &= ~CRTSCTS;
// options.c_iflag &= ~(IXON | IXOFF | IXANY); /* no flow control */
options.c_oflag &= ~(IXON | IXOFF | IXANY); /* no flow control */
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_oflag &= ~OPOST; /* No output processing */
options.c_oflag &= ~ONLCR; /* Don't convert linefeeds */
/* Miscellaneous stuff */
options.c_cflag |= (CLOCAL | CREAD); /* Enable receiver, set local */
/* Linux seems to have problem with the following ??!! */
options.c_cflag |= (IXON | IXOFF); /* Software flow control */
options.c_lflag = 0; /* no local flags */
options.c_cflag |= HUPCL; /* Drop DTR on close */
/* Setup non blocking, return on 1 character */
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 1;
/* Clear the line */
tcflush(fd,TCIFLUSH);
/* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&options) != 0) {
perror("SetupSerial 3");
return(0);
}
write(fd, data, 20 ) ;
close(fd);
return 0 ;
}
|