Serail Port Read

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Workingwim
Posts: 1
Joined: Sun Jan 03, 2010 7:58 pm

Serail Port Read

Post by Workingwim » Mon Jan 04, 2010 8:42 am

Hello,

Is there a way to read data fram a serial port?.
Mij device send always data and send
FFF0000001000000 Temp
FFF0000002000000 Name

So i like to read those strings and put them on a window.

Regards Wim Kuiper.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Serail Port Read

Post by Mark » Mon Jan 04, 2010 10:18 am

Dear Wim,

This should be possible, but you need to figure out the name of the serial port. Maybe the driverNames function will help you to get the name of the port or perhaps there is a system control panel or a utility that give you this name.

Once you have figured out the name, you can do things like:

Code: Select all

put  ":serialportname" into myFIle
open file myFIle for binary read
read from file myFIle until EOF
close file myFile
Sometimes, you need to handle the data as it is being read:

Code: Select all

put  ":serialportname" into myFIle
open file myFIle for binary read
repeat
  read from file ":serialportname" until cr
  if it is empty then exit repeat
end repeat
close file myFile
Have a look at the entry for the read command in the dictionary for more information.


If you would like to discuss Revolution in Dutch, there is a Dutch forum to be found at http://runrev.info/rrforum/ Feel free to register and ask your questions.

Vriendelijke groet,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Serail Port Read

Post by Mark » Thu Jan 14, 2010 12:03 pm

Did the answer help you, Wim?

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Simon Knight
Posts: 919
Joined: Wed Nov 04, 2009 11:41 am

Re: Serail Port Read

Post by Simon Knight » Wed Feb 03, 2010 5:27 pm

Can anyone suggest a strategy to process data from a serial port that is receiving continuos data from a device? I have written some code that reads the data and then calls its self a short while later, the snag the processing of the data collides with the next call to the routine and if I extend the time between calls I either loose data or start to lag behind. The device is a GPS unit and I am attempting to read position data and calculate average speed from it. I need to extract my data from a whole lot of other data then process it for display before the next fix update arrives.

I am experimenting with things like the amount of data read from the port and the frequency of handler recaller but I feel that I am blundering about in the dark.

Any help will be much appreciated

best wishes
Simon
best wishes
Skids

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Serail Port Read

Post by Mark » Fri Feb 05, 2010 1:53 am

Simon,

Without knowing the protocol you need to deal with, I'd guess you need a script similar to this:

Code: Select all

global gDataBuffer

on initiateSerial
  open file ":serialportname" for binary read
end initiateSerial

on readSerial
  read from file ":serialportname" until cr
  if it is not empty then put it after gDataBuffer
  send "readSerial" to me in 25 millisecs
end readSerial

on closeSerial
  close file ":serialportname"
  lock messages
  put the pendingMessages into myMessages
  repeat for each line myMessage in myMessages
    if myMessage contains "readSerial" then cancel item 1 of myMessage
  end repeat
  unlock messages
end closeSerial
I haven't tested this.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Simon Knight
Posts: 919
Joined: Wed Nov 04, 2009 11:41 am

Re: Serail Port Read

Post by Simon Knight » Fri Feb 05, 2010 7:35 am

Hi Mark,
Thanks for your reply. I have managed to connect and disconnect from my GPS device via a serial to USB converter and things work o.k. if all I want to do is put the data into a field. My problems start when I try to extract portions of the data and conduct calculations: so far the extra processing (time) seems to cause loss of data. It is certainly a timing issue and as yet I am not certain if the problem occurs when reading the data from the port (likely) or when displaying it in a field. I have just received some advice to use a multiple stack approach where one stack just reads the port writing the data to a buffer and a second stack does the extraction and number crunching and display.

I have been experimenting with my read routines recall frequency and note that you call yours every 25 milliseconds, have you used this time in your own projects and if so does it allow enough time to process the data stored in the buffer? Also, when using a variable as a buffer is it safe or desirable to have the code that reads the buffer reset it to empty or is there a danger of the port read routine attempting to write more data to the buffer while the data processing routine is attempting to read and zeroise the buffer (or does RunRev sort all this out by waiting for the read routine to finish before allowing the next call to the port read routine to get through?). As you can tell I don't fully understand how the RunRev messages operate.

Thanks
best wishes
Skids

Post Reply