[Rxtx] Help.. MacOSX Gurus
Percy Kwong
psk at psk.net
Mon Mar 28 20:46:06 MDT 2011
I've built the application in Eclipse Helios on MacOSX Snow Leopard
(10.6.7).
I have tried both RXTX version 2.1.7 and the latest nrjavaserial,
installed in ~/Library/Java/Extensions/. I have used both pre-compiled
RXTX binaries as well as attempting to compile them from source. The
result from RXTX is the same in both cases. nrjavaserial is distributed
as a single jar.
The pertinent snippets of code is as follows:
_List the available serial ports:_
public static Vector<String> listComPorts()
{
Vector<String> comPorts = new Vector<String>();
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
while( portList.hasMoreElements() )
{
CommPortIdentifier portId =
(CommPortIdentifier)portList.nextElement();
if( portId.getPortType() == CommPortIdentifier.PORT_SERIAL )
{ comPorts.add( portId.getName() ); }
}
return comPorts;
}
_Initialize the serial port:_
public void initializeSerialPort( String portName, int baud, int
databits, int stopbits, int parity ) throws Exception
{
this.portName = portName;
portIdentifier = CommPortIdentifier.getPortIdentifier( portName );
if( portIdentifier.isCurrentlyOwned() )
{
System.out.println( "Port owned" );
throw new RuntimeException( "Port already owned" );
}
else
{
port = portIdentifier.open( "App", 2000 );
// <!-- OPEN FAILS WITH RXTX 2.1.7 ON MACOSX -->
if( portIdentifier.getPortType() ==
CommPortIdentifier.PORT_SERIAL )
{
SerialPort sp = (SerialPort)port;
sp.setSerialPortParams( baud, databits, stopbits,
parity );
}
}
}
_Initialize the device that's attached to the serial dongle (the device
must be initialized with a known byte sequence, and expects a known byte
sequence in return):_
public void initializeMeter( InputStream in, OutputStream out )
throws Exception
{
this.in <http://this.in> = in;
this.out = out;
out.write( INIT_BYTES );
out.flush();
byte[] response = readDataFromDevice( in, 100, 3 );
// <!-- OPEN() OK, BUT RESPONSE IS EMPTY WITH NRJAVASERIAL -->
initialized = Arrays.equals( response, INIT_RESPONSE_BYTES );
if( ! initialized )
{
in = null;
out = null;
throw new Exception( "Initialization failed." );
}
}
_Try connecting (initialize serial port, then initialize the device:
_ private void tryConnection( String port ) throws Exception
{
man.initializeSerialPort( port, 19200, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE );
initializeMeter( man.getInputStream(port),
man.getOutputStream(port) );
}
When using RXTX 2.1.7, and running the application from the command
line, I get the command-line printout saying the version of the jar and
the JNI (which match). Using this, the open() in initializeSerialPort()
fails.
When I try using nrjavaserial, open() works, but when the INIT_BYTES are
sent to the open(?) serial port, I get an empty response back.
On MacOSX, the device is /dev/tty.usbserial, using a Prolific PL-2303
USB-Serial dongle (using the latest Prolific 1.4.0 drivers). The device
with which I want to communicate is attached thereto is plugged in,
powered on and in a PC transfer mode.
As an aside, this works perfectly on both Windows and Linux using RXTX
2.2pre2. Unfortunately, it would seem that RXTX 2.2pre2 is unstable on
MacOSX (it aborts unpredictably, especially in usleep()).
Let me know if there's any other questions that would be of assistance
in debugging this.
Thanks In Advance.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qbang.org/pipermail/rxtx/attachments/20110328/07487ce1/attachment-0510.htm>
More information about the Rxtx
mailing list