Problem reading from serial port

Benjamin Oostveen benjamin.oostveen@gmail.com
Thu Sep 24 15:40:00 GMT 2009


Hello everybody,

i am new to cygwin so i'm not sure if my question bothers you all but
i have been spending a lot of time on this issue and i don't really
know where to search for anymore.
So here i go!

I have a function in my program that reads from the serial port. This
function (I know) works in the Linux environment. There is a need for
porting it to windows now, there is where the problems begin.

First of all, there doesn't seem to be the option to use FIONREAD in
the ioctl() function. I saw this was replaced by TIOCINQ because
FIONREAD is used for other things under cygwin. So I simply replaced
FIONREAD by TIOCINQ, is this right?

The second problem is my main problem. The source files all compile
succesfully, but when in debugging with GDB it seems to be that the
function cannot perform the read() system call. It stays there all the
time...
How is this possible when the variable bytes contains a certain number
of bytes to read?
The serialport is openened succesfully when this function is called.
After debugging I can tell that this is the case. Again: the full
program works in linux only thing I changed FIONREAD --> TIOCINQ

Does anybody have an idea what the problem is? I would appreciate your
help. I hope i did provide enough information.

This is the function i'm talking about :

int readPotentiallyTerminatedPacket(SerialInterface * interfaceTEST,
unsigned int packetSize,
char packetTerminator,
int terminatorValid) {

int bytes_read;
int pos;
int terminated;
fd_set rfds;
struct timeval tv;

assert(packetSize<INPUT_BUFFER_SIZE);
bzero(&interfaceTEST->ibuf, packetSize);

/* TIOCINQ is utilized instead of FIONREAD which has been
accupied for other purposes under CYGWIN.
Other UNIX ioctl requests has been omited because
effects of their work one can achive by standard
POSIX commands */

/* wait for message */
ioctl(interfaceTEST->port_fd, TIOCINQ, &(interfaceTEST->bytes));
printf(
"bytes = %d\n", interfaceTEST->bytes);
while (interfaceTEST->bytes==0) {
FD_ZERO(&rfds);
FD_SET(interfaceTEST->port_fd,&rfds);
tv.tv_sec = 0;
tv.tv_usec = 10000;
/* 10 ms */

select(1,&rfds,NULL,NULL,&tv);
ioctl(interfaceTEST->port_fd, TIOCINQ, &(interfaceTEST->bytes));
}


/* get octets one by one */
terminated = 0;
pos = 0;
while (((unsigned int)pos<packetSize) && !terminated) {
int rest = packetSize-pos;
/*printf("rest = %d\n", rest);*/
FD_ZERO(&rfds);
FD_SET(interfaceTEST->port_fd,&rfds);
tv.tv_sec = 0;
tv.tv_usec = 10000;
/* 10 ms */

select(1,&rfds,NULL,NULL,&tv);
bytes_read = read(interfaceTEST->port_fd, &interfaceTEST->ibuf[pos],
rest); //THIS IS WHERE THE PROGRAM BLOCKS!!
/*printf("bytes_read = %d\n", bytes_read);*/

if (bytes_read==-1) {
return -1;
}

pos += bytes_read;
if (terminatorValid) {
terminated = (interfaceTEST->ibuf[pos-1]==packetTerminator);
}
/* usleep(0); */
}
if (pos==(int)packetSize) {
if (terminatorValid) {
interfaceTEST->ibuf[pos-1] =
'\0';
}
interfaceTEST->bytes=pos;
}
else {
pos = -1;
}
return pos;
}

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple



More information about the Cygwin mailing list