I have a TS-7260 trying to communicate with an RS232 device (a motor).
I can open my port, set my baud, send data to the device, the motor moves,
sends a response back, which I can read.
Then I start a polling loop where I continually call
iReturn = ioctl(fd, FIONREAD, &iBytesAvail)
and then sleep for 20ms.
Eventually (like after 100 iterations), this returns -1
with an errno = EIO (input/output error).
I can close the port and reopen and it works, but again, only for a few seconds
before I get that error again.
Any thoughts?
source code:
int fd = open("/dev/ttyAM1", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
printf("fail opening\n");
return -1;
}
struct termios port_settings;
cfsetispeed ( &port_settings, B9600 ) ;
cfsetospeed ( &port_settings, B9600 ) ;
port_settings.c_cc[VMIN] = 0 ;
port_settings.c_cc[VTIME] = 0 ;
port_settings.c_iflag=0; // no input processing
port_settings.c_oflag=0; // no output processing
port_settings.c_lflag=0;
port_settings.c_cflag=189; //8N1, 9600 baud
tcflush( fd, TCIOFLUSH ) ;
tcsetattr ( fd, TCSANOW, &port_settings ) ;
tcgetattr (fd, &port_settings); // make sure that the settings were
accepted
printf("%d %d %d %d\n", port_settings.c_cflag, port_settings.c_lflag,
port_settings.c_iflag, port_settings.c_oflag);
int iCount=0;
int iBytesAvail, iReturn;
unsigned char command[6]={0,2,0,0,0,0}; //packet to tell my motor to
return home
//command[0]=1;
//command[1]=55; //echo
//command[2]=254;
iReturn=write(fd, command, 6);
printf("written\n");
sleep(2);
iReturn = ioctl(fd, FIONREAD, &iBytesAvail); //checks the file
described by iFileDescriptor to see how many bytes are available, stores in
iBytesAvail
printf("%d bytes avail\n", iBytesAvail);
if (iBytesAvail >=6) {
iReturn = read(fd, &command, 6);
printf("%d bytes read\n", iReturn);
}
while (iCount<1000) {
usleep(20000);
iReturn = ioctl(fd, FIONREAD, &iBytesAvail);
if (iReturn==-1) {
printf("Error: %d. %s\n", errno, strerror(errno));
return -1;
}
printf("%d\n", ++iCount);
}
close(fd);
return 0;
}
output:
------------------------
yes?
189 0 0 0
189 0 0 0 <--- settings are as expected
written <--------wrote the data
12 bytes avail <----response is available
6 bytes read <----- read the first half of it
6 bytes read <----- read the second half of it
1
2 <-----------waiting for more data
3
...
284
285
Error: 5. Input/output error <------- EIO
------------------------------------
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/ts-7000/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http://groups.yahoo.com/group/ts-7000/join
(Yahoo! ID required)
<*> To change settings via email:
<*> To unsubscribe from this group, send an email to:
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
|