Use the POSIX serial i/o ioctl stuff. Makes it portable. Have not worried about flow control, but at the bottom is some snippets of C code I wrote 6 years or so ago to do IXO/TAPI alpha paging. The concept is similar. These snippets just open the serial port ("modem"), send, and get data to/from the port. There is a lot more to do, e.g., checking for locks to see if device is in use, setting a lock, etc. [Note all the ifdef's for BSD, since BSD was not POSIX compliant].
- rick On Tue, 10 Sep 2002, [iso-8859-1] cana rich wrote: > > To ansmer yours questions, i would like to make a C or C++ program which >communicate via the serial port COM1 to a device. The device is a screen plasma. I >would like the program to remote the sreen : switch on, switch off, change channel ... > The serial communication setting is : > Baud : 4800 BPS > Data length : 8 bits > Parity : none > Stop bit : 1 bit > Flow control RTS/CTS > Communication code : ASCII code > Reception time out : 4 seconds > To do it, I need to send ASCII code. For exemple, to switch off the screen i must >send the "%A0001" code. > I need to receive the acknoledgment to know if it has been well done. (code for good >receive : "@S") > Thanks for your help. > Canarich ------------------------------------------------------------------- char *modem = "/dev/ttyS1"; /* *********************************************************** Function: initmodem Purpose: Initialize modem device. Returns: File descriptor for device. ************************************************************** */ int initmodem(char *dev) { struct termios modemioctl; int modem, ioctlres; errno = 0; #ifdef DEBUG printf("Initializing modem\n"); #endif modem = open(dev, O_RDWR, 0); if (errno || modem < 1) { stop_and_exit(modem, NOTOPEN, 7); } errno = 0; #ifdef DEBUG printf("Modem fd is %d\n",modem); #endif #ifdef BSD ioctlres = tcgetattr(modem,&modemioctl); #else ioctlres = ioctl(modem,TCGETS,&modemioctl); #endif if (errno || ioctlres < 0) { stop_and_exit(modem, NOINIT, 8); } modemioctl.c_iflag |= IGNBRK; /* ignore breaks */ modemioctl.c_iflag &= ~INPCK; /* ignore parity errors */ modemioctl.c_iflag |= ISTRIP; /* strip 8th bit */ modemioctl.c_iflag &= ~INLCR; /* no CR to NL xltn */ modemioctl.c_iflag &= ~ICRNL; /* no CR to NL xltn */ modemioctl.c_iflag &= ~IGNCR; /* do not ignore CR */ modemioctl.c_oflag &= ~OPOST; #ifdef BSD cfsetspeed(&modemioctl, (speed_t) BAUDRATE); #else modemioctl.c_cflag &= ~CBAUD; /* set baud rate */ modemioctl.c_cflag |= BAUDRATE; #endif modemioctl.c_cflag &= ~CSIZE; modemioctl.c_cflag |= CS7; /* 7 bit */ modemioctl.c_cflag &= ~CSTOPB; /* 1 stop bit */ modemioctl.c_cflag |= PARENB; modemioctl.c_cflag &= ~PARODD; /* even parity */ #ifdef BSD cfsetspeed(&modemioctl, (speed_t) BAUDRATE); #else modemioctl.c_cflag &= ~CBAUD; /* set baud rate */ modemioctl.c_cflag |= BAUDRATE; #endif modemioctl.c_cflag &= ~CSIZE; modemioctl.c_cflag |= CS7; /* 7 bit */ modemioctl.c_cflag &= ~CSTOPB; /* 1 stop bit */ modemioctl.c_cflag |= PARENB; modemioctl.c_cflag &= ~PARODD; /* even parity */ modemioctl.c_cflag |= HUPCL; /* hang up */ modemioctl.c_cflag |= CRTSCTS; /* hardware handshaking */ modemioctl.c_cc[VMIN] = 0; /* read() as few as 0 bytes */ modemioctl.c_cc[VTIME] = 50; /* 5 second timeout */ modemioctl.c_lflag &= ~ISIG; /* no signals */ modemioctl.c_lflag &= ~ICANON; /* no signals */ modemioctl.c_lflag &= ~ECHO; /* no echo */ #ifdef BSD ioctlres = tcsetattr(modem,TCSANOW, &modemioctl); #else ioctlres = ioctl(modem, TCSETS, &modemioctl); #endif #ifdef DEBUG printf("Modem initialization complete. fd is: %d\n",modem); printf("Error value is: %d\n",errno); #endif if (errno || ioctlres < 0) { stop_and_exit(modem, NOINIT, 8); } return (modem); } /* *********************************************************** Function: senddata Purpose: Send data, CR terminated, to the modem device. Returns: Number of bytes written. ************************************************************** */ int senddata(int modem, char *str) { int numsent; char packet[MAXSIZE+2]; sprintf(packet,"%s\r",str); numsent = write(modem, packet, strlen(packet)); return(numsent); } /* *********************************************************** Function: getdata() Purpose: Reads input from the modem device, ignoring CR and LF. Returns: Length of the string read. ************************************************************** */ int getdata(int modem, char *str) { char c; char *packet; int numread; packet = str; while(read(modem,&c,1) == 1) { #ifdef DEBUG if ( isprint(c) ) printf("%c ",c); else printf("%x ",c); #endif switch(c) { case LF : break; case CR : break; default : *packet = c; ++packet; break; } } *packet = EOS; #ifdef DEBUG printf("\n"); #endif return(strlen(str)); } -- redhat-list mailing list unsubscribe mailto:[EMAIL PROTECTED]?subject=unsubscribe https://listman.redhat.com/mailman/listinfo/redhat-list