Re: Talking to Mindstorms over USB

"ansofive@xxxxxxxxx" <ansofive@xxxxxxxxx> writes:


I've broken out my old Mindstorms kit and I'm working on a simple
package to send commands to the brick from my workstation. You send
data to the brick over a USB device so I open the device file (/dev/
usb/legousbtower0) and write my packet - Works fine, lasts a long
time. Here's what I have so far:

(defun send-message (message &key (port *port*))
"Send a list of bytes to the brick and wait for the reply."
(with-open-file (stream port
:direction :io
:if-does-not-exist :create
:if-exists :overwrite
:element-type '(unsigned-byte 8))
(write-sequence (mk-packet message) stream)
#+ignore(read-packet stream)))

(defun read-packet (stream)
(let ((buffer (make-array 8 :element-type '(unsigned-byte 8))))
(read-sequence buffer stream)

My problem is in the read-packet form - I would like to read any reply
that is sent by the brick in response to the packet. I've tried a
couple of different ways to read from the device - looping until read-
byte fails and read-sequence both seem to simply wait forever. I am
assuming that they seem to hang because the "file" is a character
device and not a file-system file - there is no end-of-file condition
to tell the read-stuff functions that there is no more data. It could
also be that the USB receive queue is empty by the time the read-
sequence is called and the read is simply waiting for the next byte.

How do I read the response from the USB device? Eventually I would
like to be able to read much larger chunks of data from the brick, the
maximum would be 32KB as that is the size of the RAM on the device.
Any clues are appreciated.

You ay use CL:LISTEN to learn whether a byte is available or not.

Using CL:LISTEN and CL:READ-BYTE, you should be able to avoid blocking
in a portable way.

__Pascal Bourguignon__