IonicWind Software

IWBasic => General Questions => Topic started by: Boris on April 27, 2009, 01:01:02 PM

Title: Commread problems
Post by: Boris on April 27, 2009, 01:01:02 PM
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.
Title: Re: Commread problems
Post by: sapero on April 27, 2009, 03:37:55 PM
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.
Title: Re: Commread problems
Post by: Boris on April 28, 2009, 08:08:14 AM
Thanks Sapero.
Title: Re: Commread problems
Post by: r.budras on May 02, 2009, 07:37:57 AM
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
Title: Re: Commread problems
Post by: sapero on May 02, 2009, 11:17:39 AM
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.