serial port problems

Sarah Reichelt sarah.reichelt at gmail.com
Sun Nov 7 18:47:59 EST 2010


On Mon, Nov 8, 2010 at 1:18 AM, Larry Walker
<larry at walkerenergysystems.com> wrote:
> I am trying to read data from a "serial port" (using a USB-serial adaptor).
>
> I am on a MacBook Pro running 10.5.8, using LiveCode 4.5, and Prolific model
> 2303 USB-serial adaptor.
>
> The following code does not work:
>
> on mouseUp
>   put "modem:" into usbSerial
>   put empty into field "Field"
>
>   open driver usbSerial for text read
>   read from driver usbSerial for 5 chars
>   put it after field "field"
>
>   close driver usbSerial
> end mouseUp
>
> Setting a breakpoint on the "read from" shows that the "open" does not
> appear to fail. Single-stepping through the read does not wait for any input
> from the connected terminal, it returns immediately. "It" is empty
> afterward.
>
> By reading for N chars I believe I am side-stepping any line -terminator
> issues.
>
> I have confirmed that the USB-serial adaptor and the terminal can talk to
> each other properly, using the command-line invocation: 'screen
> /dev/tty.usbserial'. I have confirmed that the serialControlString matches
> the terminal's settings.
>
> This fail whether I use "open file" or "open driver".
> This fails whether I use "modem" or "printer" as the device.
>
> If I use /dev/tty.usbserial", the IDE hangs and I have to Force-Quit it.

OK, I haven't tried any of this with LC 4.5, but if Mark says nothing
has changed, then this should still be valid.

Firstly, "modem:" is unlikely to work. Some Keyspans allow you to
configure one of their connections to appear to be a modem, but unless
your Mac actually has a modem, then this is not going to connect to
anything.

You MUST install drivers for your adapter. In your case, Prolific
supply all the drivers at
<http://www.prolific.com.tw/eng/downloads.asp?id=31>.

Once you have the drivers installed, check the driverNames again and
hopefully something will show up. Here is what I get on a Mac with a
Keyspan (two port) and a generic FTDI adapter attached and Rev 4.0:

usbserial-FTC8J5X3,/dev/tty.usbserial-FTC8J5X3,/dev/cu.usbserial-FTC8J5X3
KeySerial1,/dev/tty.KeySerial1,/dev/cu.KeySerial1
USA28X1a2P1.1,/dev/tty.USA28X1a2P1.1,/dev/cu.USA28X1a2P1.1
USA28X1a2P2.2,/dev/tty.USA28X1a2P2.2,/dev/cu.USA28X1a2P2.2
Bluetooth-PDA-Sync,/dev/tty.Bluetooth-PDA-Sync,/dev/cu.Bluetooth-PDA-Sync

Each line of the driverNames contains 3 items. The one you need to use
is the last one,  starting with "/dev/cu.". To open the port to the
first device on the list, I would use:

global gPortName
put "/dev/cu.usbserial-FTC8J5X3" into gPortName
open driver gPortName for binary update
if the result is not empty then
    answer error "Unable to open port" & cr & the result
end if

When finished, close the port using:
close driver gPortName

But you may need to adjust the serial port settings before you can
actually send and receive data. This depends on the device you are
communicating with, rather than the adapter, but my serial test stack
allows easy access to the common settings, so you can test this.

When writing, again it depends on the device. Some require a fixed
number of bytes, some need a specific end-of-line. Use my test stack
to work this out if you don't have the docs.

Reading: the problem with reading a fixed number of bytes is that the
bytes you don't read are still in the buffer and will mess up your
next read. This may not matter if you are closing the port after each
read, but I always prefer to use:
        read from driver gPortName until empty in 90 ticks

This grabs everything it can inside the time limit.
Then I can work out whether I have received multiple signals, or a
partial signal and act accordingly, either processing the segments in
a loop, or asking the serial port for more data.

I hope this helps you get started, but if not, feel free to get back
to me, either on or off list.

Cheers,
Sarah



More information about the use-livecode mailing list