May 15, 2024, 02:02:36 AM

News:

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


Commread problems

Started by Boris, April 27, 2009, 01:01:02 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Boris

April 27, 2009, 01:01:02 PM Last Edit: April 27, 2009, 01:03:40 PM by Boris
Is there a windows message for â€ËÅ"byte entering serial port’ or â€ËÅ"com1 buffer not empty’ or something like that?

I’m trying to use the code in the commread example with a windows program. The data is sent to the PC from a Picaxe microcontroler as a packet of 11 bytes, once about every 2 seconds. The data will be drawn as lines, then about two seconds later the lines will be redrawn and so on. I can get it to work fine using the console, but in a windows servicing routine things are a little different.

I’m not having any success with:

sub w1_handler
IF @CLASS = @IDCLOSEWINDOW
   CLOSEWINDOW w1
elseif ReadFile(hcom,data1,1,bytesread,0) <> 0
   ...read and draw data...
ENDIF
RETURN
ENDSUB


It just hangs and crashes. Probably due to the timeouts blocking the message queue.
Thank you for not breeding

sapero

Hey Boris, never mix GUI with blocking calls. For file based devices you have at least three possibilities:
1. use FILE_FLAG_OVERLAPPED
a) create your own message loop using MsgWaitForMultipleObjects instead GetMessage. It will return 0 if the device is ready to read the incomming data.
b) use WaitForSingleObject for the event provided in OVERLAPPED, usnig separate thread.
c) use RegisterWaitForSingleObject (also event based) - it creates a background thread and calls your callback function when the device is receiving data.
2. Use a (2s/2=1000ms for interval) timer and set zero timeout for reading from the device. Call ReadFile inside @IDTIMER. This is bad, but acceptable.
3. Create separate thread, use ReadFile and do not set timeouts. Let ReadFile read the data into global buffer, and send a message to your window when you receive something.

I think you pick the option with timer.COMMTIMEOUTS ct
' specifies that the read operation is to return immediately with the bytes that have already been received
ct.ReadIntervalTimeout         = -1
ct.ReadTotalTimeoutMultiplier  = 0
ct.ReadTotalTimeoutConstant    = 0

ct.WriteTotalTimeoutMultiplier = 0
ct.WriteTotalTimeoutConstant   = 0


OPENWINDOW w
STARTTIMER w,1000
hPort = CreateFile com1
...
SetCommTimeouts(hPort, &ct)
...
case @IDTIMER
ReadFile(hPort,..., &received, 0)

Not tested, I do not have COM ports.

Boris

Thank you for not breeding

r.budras

May 02, 2009, 07:37:57 AM #3 Last Edit: May 02, 2009, 08:12:42 AM by r.budras
This is a thing i am waiting for long time : the option 1 with FILE_FLAG_OVERLAPPED

Has someone an example program for the FILE_FLAG_OVERLAPPED option.
That would help me a lot.

I am not that good in coding and at the moment I ask
periodiccally if data are present with :

  ClearCommError(hCom_A,ComErrFlags_A,ClearComErr_Buf_A)

and this is not realtime.

What I really wish, is something like this :

select class
  case Com_data_received
    select comnum
      "read the data from COM comnum and process them"



By the way, sometimes i need help in programming.
Beneath reading the com I need a subroutine to record from the sound card and save is as a mp3-file

Is there someone better than me who will make some dollars ?

Reinhard, Germany, Munich, Oktoberfest

sapero

May 02, 2009, 11:17:39 AM #4 Last Edit: May 02, 2009, 04:04:58 PM by sapero
Moin Reinhard,
I have two three examples so far, tested only on my built-in offline fax modem ;)

EDIT: added third example with a dialog.