[Newbies] communication with serial port

Benjamin Schroeder benschroeder at acm.org
Fri Nov 3 03:13:44 UTC 2006

Hi Ned, everyone,

> Ideally we'd have an interface that could also take a String or  
> ByteArray with the name of the port.
> I suppose the call could be polymorphic, taking either a String or  
> a SmallInteger.

I think this is a good idea - and not too hard to make resilient on  
different platforms. I'm not too familiar with plugins, but does the  
plugin function just get an oop, and hence can decide whether it's a  
String or SmallInteger or some other thing when it runs?

> I'd like to see the code too. I'd be willing to put it on one of my  
> servers if you'd like.

Thanks for the offer. I've attached the code to this message -  
hopefully attachments come through this list OK. I will post a web  
page about it soon, but you (or anyone) should feel free to post it  
anywhere you'd like as well.

> Another possibility is to make a little proxy program that you  
> could connect to using a named pipe that would just talk to the  
> serial port. Its command protocol would have to support all the  
> SerialPort primitives.

This is a decent idea too. We did something like this when starting  
out with the Squeak/.NET bridge.

Some notes about the serial mini-library follow - please ask any  
questions or report any rough edges as needed. This library is mostly  
just what I've needed so far, so it is (to say the least) very  


Before I start, I want to give a word of caution: please make a copy  
of your image before using this code, at least in the early going. I  
don't mean to sound alarmist, but part of the Squeak code does  
involve a loop that reads from the serial port - and, while I think I  
have made that safe in the case of restarting the image (by shutting  
down the read loop, and protecting against null pointers in the C  
code), I have not tested it extensively.


This is a very basic serial communications mechanism, but maybe we  
can expand it into something general (or use it to update the built- 
in SerialPort plugin). The C code is pared down from some sample code  
I found, but I'm afraid I don't remember where (it was a couple of  
months ago).

The C code does not have many options - the only parameter besides  
the port is speed. We could add options for parity, stop bits, etc.  
as needed.

I wrote the code in Squeak 3.8 - hopefully it will work as well in a  
3.9 image, but I haven't tested it there. The two things that I can  
think of being a problem are

	- the underscore style of assignment - does this file in OK on 3.9?

	- FFI - this might need to be loaded from SqueakMap for 3.9

Let me know if this is a problem and I can investigate.

Here are some steps for getting started.

1. Install the C framework.

	Copy the "libserialport.framework" folder to your home directory's
	Library/Frameworks folder.

	You can also build a new copy of the framework if you'd like,
	or if you find the one from the zip doesn't work on your system.
	(I used Mac OS X 10.4.8, with gcc 4.0.0.)

2. "File in" the Smalltalk code.

	Go to a "file list" in Squeak. (On the World menu, choose "open..." and
	then "file list". Navigate to wherever the "SimpleSerialPort.st" is  
	(It might be easiest to put a copy in the same folder as your image,  
	that's not strictly necessary.)

	Select "SimpleSerialPort.st" and press the "install" button, above.  
	will add the serial port code to your system.

3. Determine what name the system has given to your serial port.

	As Ned said, the serial device driver on OS X doesn't assign names in
	any particular sequence. Go to a Terminal and do

		ls /dev/cu.*

	and see what's there. There will probably only be one entry that  
	USB and serial - otherwise you could try doing the "ls" with the  
device there
	and without it, as OS X will remove the entry when the device is not

	If you are using an Arduino, and have the serial monitor working,
	you can also go to the "Serial Port" menu in the Arduino software
	and see what is selected there.

4. Determine the speed at which you want to talk to the port.

	This is the serial baud rate - for Arduino, it's also listed in the  

4.5 Save your image (if you'd like)!

5. Try to connect to the serial device.

	Here is an example of reading from a USB-connected GPS module -
	communication with the Arduino is similar (but I've left mine at the  

		port := SimpleSerialPort
			newOnPort: '/dev/cu.usbserial-A1000pPF'
			speed: 4800.

		port available. "print me - results in the number of bytes available"

		port readCharacter. "print me" "results in something like $G"

		port close.

	You can also use "readByte" to read bytes as numbers instead of ASCII.
	To write bytes to the port, just use "writeByte:",  
"writeCharacter:", or

	Note that "readByte" will hang if there are no bytes available. I  
think that
	it should be interruptible, as the looping is in Smalltalk code, not  
	the C library.

Those are the basics!

Please remember to close the port ("port close", above) when you are  
done. This is one of the rough edges in my library - there is a  
separate thread that buffers data read from the underlying port, and  
it will not go away unless the port is closed.

Please post any questions, thoughts, problems, etc.

Benjamin Schroeder

-------------- next part --------------
A non-text attachment was scrubbed...
Name: libserialport.zip
Type: application/zip
Size: 7390 bytes
Desc: not available
Url : http://lists.squeakfoundation.org/pipermail/beginners/attachments/20061102/5b51dc4c/libserialport.zip

More information about the Beginners mailing list