Using the Serial Port

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
mhoneywill
Posts: 66
Joined: Fri Feb 05, 2010 7:31 pm

Using the Serial Port

Post by mhoneywill » Fri Feb 18, 2011 12:46 pm

Hi,

I'm now the proud owner of a LiveCode licence so now its time to get down to some programming. I'm initially working on a windows platform but want my final application to be crossplatform Win/Mac and maybe Linux so need to write everything in livecode.

My first experiences are (these are tainted by my background C and VB6).

1. Its difficult to get an overall view of your code you can't see all the source in one place, I need to select every control and then view the code in that control. Have I missed something or is this just how the livecode works?
2. Its a real shame that the .rev and .livecode files are not ascii files as it makes file comparison and version control harder. Looking inside the files they are "almost" text anyway.


My first task involves communicating via the serial port I understand that the serial port is opened as a file using the "open file" command. The only example I could find for using serial ports is from Sarah here http://www.troz.net/rev/stacks/SerialTest.rev Thanks Sarah for sharing it.

Some details first about my serial requirements. I will need to communicate to up to 4 serial ports running at 115200 baud. The protocol I'm using is ModBus RTU which is used extensively in the automation business. The key feature of Modbus RTU is that the end of message is signified by a message gap of greater that 3.5 characters at 115200 baud this is about 300us. More details on the Modbus protocol can be found here http://en.wikipedia.org/wiki/Modbus

I have a number of questions that I hope people can help me with

1. I've already proved using Sarah's stack that I can talk to com ports above com9 :D
2. Is there anyway to reliably get a list of installed com ports on a computer using Livecode (This could be read from the registry in windows, but I would like a cross platform solution)
3. My device usually connects via a USB-Serial chip which we program with a unique name. It would be good to further restrict the list of available ports to those with the unique name. But this would require accessing USB device details.
4. My next issue might be a real show stopper. Am I correct in saying the the reception of characters through serial ports do not generate an event? It can't be that the only way to handle receiving characters is to poll the port?
5. I need to get a FAST throughput on the Serial port so am hoping that there is some form of events driven interface for the serial port. I presume this is how UDP comms works.

I fully understand that some of the above issues may be because of my lack of knowledge of Livecode, so any help would be greatly appreciated.

Cheers

Martin

mhoneywill
Posts: 66
Joined: Fri Feb 05, 2010 7:31 pm

Re: Using the Serial Port

Post by mhoneywill » Fri Feb 18, 2011 9:47 pm

Well I've found out that I can only Poll serial ports, I'll just have to see how fast I can do that.

I tried to modify the code in the Stack created by Sarah.

Just adding a space into a cooment in the following code

Code: Select all

on mouseUp
  -- check which port is in use
  put the label of btn "Port" into thePort
   
  -- set the port parameters
  put "BAUD=" & the label of btn "Baud" & \
      " PARITY=" & the label of btn "Parity" & \
      " DATA=" & the label of btn "Data" & \
      " STOP=" & the label of btn "Stop" into serial
  set the serialControlString to serial
   
  -- open the port & show any error
  set the cursor to watch
  if the hilite of btn "OS X" then
    open driver thePort for binary update
  else
    open file thePort for binary update
  end if
  if the result is not empty then
    answer the result
  else
    set the hilite of btn "Port open" to true
    
    -- start reading the port if open
    send readPort to this card in 10 ticks
  end if
end mouseUp
Causes the following error

button "Open Serial Port": compilation error at line 3 (Chunk: can't create a variable with that name (explicitVariables?)) near "thePort", char 31

Can someone help me? why is this producing an error? when it wasn't before. I've tried renaming "thePort" to "MyPort" but I still get an error.

What I was originally trying to do was change the line
send readPort to this card in 10 ticks
to
send readPort to this card in 10 milliseconds
To see how well Livecode copped with this sort of update rate.

Thanks

Martin

mhoneywill
Posts: 66
Joined: Fri Feb 05, 2010 7:31 pm

Re: Using the Serial Port - SOLVED

Post by mhoneywill » Thu Mar 10, 2011 10:02 pm

With Help from Scott Rossi at Tactile Media I found the problem

I'll make a guess.... In LC's preferences under Script Editor, there is a checkbox that reads Strict Compilation Mode. This *requires* that all variables be declared in scripts, otherwise they don't compile. See if that's checked on your end -- if it is, that's probably what's causing the problem.

I never use the above setting because I like the freedom that LC scripting provides. It is true that the recommended practice is to quote names of objects in scripts, however, I don't usually do this as I prefer to reserve quotes for things like custom messages -- this is just my own scripting style. I believe most people do not use Strict Compilation Mode, but if this is enabled, LC will likely interpret my object references as undeclared variables, and refuse to compile.

melristau
Posts: 56
Joined: Tue Jul 14, 2015 5:15 pm
Contact:

Re: Using the Serial Port

Post by melristau » Sun Aug 14, 2016 5:32 pm

Martin,
Might you have any updated advice regarding LC serial communication capabilities? Thanks!
rebuilding visual programming app originally created in 1993 as Hypercard stack

rinzwind
Posts: 135
Joined: Tue May 01, 2012 10:44 am

Re: Using the Serial Port

Post by rinzwind » Sun Aug 14, 2016 10:08 pm

Strict compilation mode saves you from some annoying bug hunting. A variable typo is easy to make.

Post Reply