April 27, 2024, 02:47:20 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Serial port ReadFile problem

Started by gajningi, January 27, 2007, 12:56:44 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

gajningi

January 27, 2007, 12:56:44 AM Last Edit: January 27, 2007, 12:59:36 AM by gajningi
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

Ionic Wind Support Team

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.
Ionic Wind Support Team

gajningi

Thanks Paul... I will give that a try.