Page 1 of 1

Capturing data from a serial port

Posted: Tue Jan 31, 2012 9:37 am
by colinpartridge
I have "discovered" livecode from a need to update an old but essential Hypercard application. The hypercard script was written to parse some data captured from a (PAT tester). At the moment the capture is being done separately with Zterm, the resultant textfile then processed by the hypercard stack. What I thought would be neat would be if I could capture the data directly in Livecode. Is this doable?, and what is the best way of storing the data? I know there is a limit to what can be stored in a field but what are my alternatives if data exceeds 65K? I'm still waiting for that eureka moment when I "get" livecode, but it seems slow in coming..and my knowledge of scripting is mostly from trial and error.Any tips or pointers to which way to proceed would be welcome.
Regards Colin

Re: Capturing data from a serial port

Posted: Wed Feb 01, 2012 4:18 pm
by colinpartridge
Answering my own questions with another one. I have now got to the stage where I am capturing the correct data but... I cannot close the serial port with a button because the user interface is not responding unless I pull the serial device from the port ( actually its a keyspan serial USB adaptor). How do I get around this or is it possible to close the port based on the data coming in, there is a convenient "END OF DATA" line send from the device connected to the serial port.

cheers

Colin

Re: Capturing data from a serial port

Posted: Wed Feb 01, 2012 5:21 pm
by sturgis
Mind sharing code? But i'll make a guess or two as to what might be up.

If you are using a read loop to read the data, most likely you are processor bound. If you are using a read loop, if you put a "wait 10 milliseconds with messages" somewhere inside the loop it opens a window of opportunity for things like buttonpresses and keypresses to be handled.

If on the other hand you start a "read until eof" type of read (I forget all the options) I THINK its blocking. So it will just sit there until the criteria is met and the read stops. If this is the case, see my first guess and use a read loop with a "wait with messages" in it.

I have a small script that does some things with an arduino board (win 7) and have a serial read loop setup for it. Not sure its well coded but it works for me.

Elsewhere I have a connection handler that sets tReading to true, then calls readloop. Since i'm not using a repeat loop I don't have to put a "wait with messages" instead it uses a send in time to get things done, but either method should work.

Code: Select all

command readLoop
   if tReading then
      read from file "COM3:" until empty 
-- this version should be fast enough to not get loop locked. Meaning it should be faster than incoming data so shouldn't hang.

      if it is not empty then put it after tOut
      send readLoop to me in 50 milliseconds
   else
      put empty into tOut 
   end if
   if the number of lines in tOut > 20 then
      delete line 1 to -21 of tOut
   end if
   lock screen
   put tOut into field "outField"
   unlock screen
end readLoop
colinpartridge wrote:Answering my own questions with another one. I have now got to the stage where I am capturing the correct data but... I cannot close the serial port with a button because the user interface is not responding unless I pull the serial device from the port ( actually its a keyspan serial USB adaptor). How do I get around this or is it possible to close the port based on the data coming in, there is a convenient "END OF DATA" line send from the device connected to the serial port.

cheers

Colin

Re: Capturing data from a serial port

Posted: Thu Feb 02, 2012 11:38 am
by colinpartridge
Thanks for your help Sturgis, with the help of your code I found out what the problem was, part of my original code read

Code: Select all

read from driver thePort until recEOL
    else
       read from file thePort until recEOL
       end if
    if it is not empty then put it after fld "Serial Data"
    
    send readPort to me in 10 ticks
recEOL was defined elsewhere, but once I change the "read from file until recEOL" to "read until empty" I got control of the interface again.

cheers

Colin