I couldn’t sleep, so got up and decided to fiddle with the SSC-32 and Python some more. I knew it had to be possible to read a response from the SSC-32 without having to depend on a serial port time out. I have done this many times with various languages. I defined a function to read characters from the SSC-32 until a CR is encountered and then return the result.
[code]#!/usr/bin/python
Import the Serial module
import serial;
Reads single characters until a CR is read
def Response(port):
ich = “”;
resp = “”;
while (ich <> '\r'):
ich = port.read(1);
if (ich <> '\r'):
resp = resp + ich;
return resp;
Open the port at 115200 Bps - defaults to 8N1
ssc32 = serial.Serial(’/dev/ttyS0’, 115200);
Send the version command
ssc32.write(“ver\r”);
Read the response
inp = Response(ssc32);
Show what we got back
print inp;
Close the port
ssc32.close();
[/code]
Notice the Response() function I defined at the top of the program. This function just reads a single character at a time from the serial port the SSC-32 is on until it reads a CR, and then stops and returns the result.
Python uses indenting to tell what statements are part of a block, so everything between the ‘while’ and the return statement is part of the ‘while’ block. Everything indented further than the ‘if’ is part of the ‘if’ block.
This program returns immediately after the last character of the SSC-32’s response has been read - when it reads a CR. There is no delay between sending the ‘ver’ command and getting the response now.
Python and the Py-Serial addon is available for Windows, and GNU Linux and FreeBSD as well as most other operating systems. I can’t imagine it being any more difficult to talk to the SSC-32 from other languages, including Java. In fact, I think I will look into reproducing this example with Java.
8-Dale