The Data Layer

 

The dataLayer is still developing, and it is unclear where it will go.  Ideally, it should consist of one abstract class called dataPort which can be constructed to interface with serial ports, sockets, and USB ports.  All three of these interfaces can be made using Java objects.  However, it is unclear that this will be necessary.  At this point we only have serial port objects, which are implemented in Matlab.  However, there is also a program freely distributed by lantronix that essentially tunnels system calls to a serial port through to a particular socket.  Furthermore, the USB interface may or may not be compatible with the Matlab serial port interface.  Therefore, the serial port interface may be sufficient for everything we need.  For now, we will just use the serial port object, but we will talk about it as a general dataPort, since it is currently playing that role.

 

To setup and install and the lantronix Comm Port Redirector software go to http://ftp.lantronix.com/redirect and install.  Then, open a serial port in Matlab to access mote on remote sockets.

 

Create a data port

s = createSerialPort(‘COM1’)

 

Create multiple data ports

closeSerialPort(s)

s = createSerialPort([‘COM1’,’COM2’])

 

View the data ports

s(1)

s(2)

 

Write to the data ports (when DEBUG is set, the dataPorts display all data they send)

DEBUG=1

writebytes(s, [0 4 255 1 6 89])

 

Read 36 bytes from both the data ports (you will get an error if there is no data to be read)

data = readBytes(s, 36)

 

Clear the buffers

clearBuffers(s)

 

Close the data ports

closeSerialPort(s)

 

 

 

 

 

 

 

Tutorial ON READING/WRITING from matlab serial ports objects

This section is completely independent of the dataLayer and the comm stack. If you want to work within the comm stack, you should just use the readBytes and writeBytes interface (See note below).

 

NOTE:  The ‘read’ interface shown above is a blocking read.  That means that you do not get your command prompt back.  Furthermore, if there is no data to be read immediately you get a timeout error.  Serial Ports also have an event-driven read interface, which is described below. However, with the event-driven interface, the user has to “pull” the incoming data, i.e. after the event is fired, the user is responsible for going to the serial port and reading the data.  This is disadvantageous because only one eventListener is possible at a time.  Therefore, if you want to use the event-driven byte interface on the serial port you should be sure that nothing else (such as a packetPort also) wants to read that data. 

 

I have built a “push” interface into the packetPorts, i.e. when an event is fired, it includes a copy of the packet.  Therefore, multiple users can listen for packets on the same port at the same time.  If you want the same thing for dataPorts, you should build a dataPort wrapper class, which should also give standard interface to the socket and USB connections, as indicated above.

 

you can read/write both synchronously and asynchronously
(meaning that you either do or do not block the command prompt). There are four things
that are important to us: 1. s.ReadAsyncMode, 2. readasync, 3. fread, 4. fwrite

If you don't want to miss any bytes as they come in:
Set s.ReadAsyncMode='continous' to have the serial port object continously read data and
store it in it's buffer (until it's full, after which you start to lose bytes).
Then, you have two options:
 1. you can use fread(port, numBytes, 'uchar') to read the data from the front of the buffer.
 2. you can have the serial port object trigger an even every time there are x bytes in the buffer

If you want to start reading from several serial ports at the same time:
Set s.ReadAsyncMode='manual' for all your ports so that they don't read for now. When you want
them to read, type "readasync(port, numBytes)" for each port. Now they are filling up their
buffers. When data comes in, you have the same two options as before. Don't forget to set
their s.Timeout values high enough.

If you want to read from one port right now use data=fread(s,numBytes)

to write to a port use injectPacket(s,data) or fwrite(s,data).
InjectPacket can also take an array of serial ports and write to them at almost the same time.

If your s.OutputBufferSize is not big enough to write everything at the same time (the
default outputBufferSize is 512 bytes), you can use an OutputEmpty event to notify you
when your output buffer is empty again.