Hi all,
I'm in troubles in writing C drivers for a device using RS232 port.
I cannot establish any communications between my linux box and that device! On the other hand I'm sure the RS232 on the device is working properly because using a hyperterminal on another Windows machine everything works fine.
Here is what is reported in the manual of the device:
"The RS232 communication parameters are:
9600 BAUD, 8 BIT, NO PARITY, 1 STOP BIT (8N1)
...
Each command is an ASCII string, and must be terminated by a Carriage Return character[...] The responses are terminated by a Carriage Return character."
Here is my code:
This code is compiled without any errors or warnings but unfortunately I cannot read correct values from the device (the device should respond 2 digits ASCIICode:#include <stdio.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <termios.h> #include <stdlib.h> int open_port(char *port) { int fd; if (!port) return -1; if ((fd=open(port,O_RDWR | O_NOCTTY | O_NDELAY))<0) return -2; fcntl(fd,F_SETFL,0); return fd; } void configure_port(int fd) { struct termios newtio; bzero(&newtio,sizeof(newtio)); newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD ; newtio.c_iflag = IGNPAR | ICRNL; newtio.c_oflag=0; newtio.c_lflag=ICANON; tcflush(fd,TCIFLUSH); tcsetattr(fd,TCSANOW,&newtio); } int main (int argc, char **argv) { int fd; char PORT[]="/dev/ttyUSB0"; int status; char buffer[255]; if ((fd=open_port(PORT))<0) { printf("Error in opening serial port fd=%d\n",fd); return -1; } configure_port(fd); if ((status=write(fd,"SE\r",3))<3) { printf("Error in writing on serial port\n"); return -1; } status=read(fd,buffer,255); buffer[status]=0; printf(":%s:%d\n",buffer,status); return 0; }
code terminated with Carriage Return character to the command SE).
In particular sometimes it happens to read "SE\r" (so exactly the same command I write on the RS232 from the pc) and in some other cases the read values are wrong in format and in length.
I cannot figure out where the problem is. I guess it is a rs232 configuration problem because as I told you using hyperterminal on Windows everything is ok but I really did not realize what is going wrong.
Maybe the problem is that I'm using a serial to usb converter and it needs further configuration I'm missing? (The same converter works with hyperterminal).
By the way here is the output of my linux box when I plug in the converter:
usb 2-1: new full speed USB device using uhci_hcd and address 13
usb 2-1: configuration #1 chosen from 1 choice
pl2303 2-1:1.0 pl2303 converter detected
usb 2-1: pl2303 converter now attached to ttyUSB0
Any suggestions?
Any help is really appreciated.
Thank you for your attention.
Bye



LinkBack URL
About LinkBacks


