www.delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2009/09/24/11:40:26

X-Recipient: archive-cygwin AT delorie DOT com
X-SWARE-Spam-Status: No, hits=-2.0 required=5.0 tests=BAYES_00,SARE_MSGID_LONG40,SPF_PASS
X-Spam-Check-By: sourceware.org
MIME-Version: 1.0
Date: Thu, 24 Sep 2009 17:40:09 +0200
Message-ID: <f25b4a80909240840j270ce2dfwfb0cda26f60bb58@mail.gmail.com>
Subject: Problem reading from serial port
From: Benjamin Oostveen <benjamin DOT oostveen AT gmail DOT com>
To: cygwin AT cygwin DOT com
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

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

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019