IonicWind Software

IWBasic => General Questions => Topic started by: gajningi on January 27, 2007, 12:56:44 AM

Title: Serial port ReadFile problem
Post by: gajningi on January 27, 2007, 12:56:44 AM
I am trying to read from COM1 serial port. I can write with no problems, and my serial device responds correctly (as viewed with a serial port monitor (http://www.serial-port-monitor.com/index.html).

When I open the comport, len(hcom) always reports that 4 bytes are in the buffer. This is alway what len(hcom) reports.
My problem is that the number of bytes returned from the serial device is of a variable length, depending on what function I ask of it and the type of device it is.

I am using the two examples provided with Ebasic, combined then, using DECLARE IMPORT (tried DECLARE KERNEL32) and changed the value 4 to len(hcom).

I have tried waiting for different periods (up to 10 seconds) of time after writing to the serial device to read the buffer

original code                ReadFile(hcom,data1,4,bytesread,0)

my code                     ReadFile(hcom,data1,len(hcom),bytesread,0)

It appears that len(hcom) is not returning the correct value.


Any assistance would be greatly appreciated.

Thanks

John
Title: Re: Serial port ReadFile problem
Post by: Ionic Wind Support Team on January 27, 2007, 01:04:01 AM
LEN doesn't know what a serial port is.  It is returning the length of the variable hcom, which is probably an INT in your code, which is 4 bytes on Intel 32 bit systems.

When you use ReadFile you give it a buffer, and the length of that buffer so it knows when to stop filling it.  Since the device is serial there isn't a way ahead of time to know how much data is available, you just call ReadFile in a loop until there isn't any data remaining, in which case bytesread will be less than your requested size.

Paul.
Title: Re: Serial port ReadFile problem
Post by: gajningi on January 27, 2007, 01:07:33 AM
Thanks Paul... I will give that a try.