javacomm version 3 on Linux Ubuntu - serial port not receiving data
843790Nov 14 2007 — edited Nov 14 2007Hi, I've installed javacomm version 3 on Ubuntu 'Gutsy Gibbon' and I've set up successfully,
but my code it's only capable of transmitting data and not receiving it (!!!).
SUMMARY:
Device: TMOTE IV
API: TinyOS 1.0.x
Comm: javacomm v3.0-u1
OS: Linux Ubuntu 'Gutsy Gibbon'
Driver USB-Serial: ftdi_sio included in the Linux kernel.
I'm using the /dev/ttyS0 port to communicate with the USB device on /dev/2-1 via the ftdi_sio driver.
The problem isn't the port permissions:
crw-rw-rw- 1 root root 189, 129 2007-11-14 18:24 /dev/2-1
crw-rw-rw- 1 root dialout 4, 64 2007-11-14 18:22 /dev/ttyS0
I attatch my code because any person could help me. NOTE: This code works smoothly on Windows, and in Linux I can open the port and set up it, even send data, but I'm not receiving anything...
In the comments I read some warnings about working on Linux, but I don't fully understand them to modify the code.
*** CODE: ***
package net.tinyos.packet;
import java.util.*;
import java.io.*;
import javax.comm.*;
import javax.swing.JOptionPane;
/**
* A serial port byte source, with extra special hack to deal with
* broken javax.comm implementations (IBM's javax.comm does not set the
* port to raw mode, on Linux, at least in some implementations - call
* an external program (tinyos-serial-configure) to "fix" this)
*/
public class SerialByteSource extends StreamByteSource implements SerialPortEventListener
{
private SerialPort serialPort;
private String portName;
private int baudRate;
public SerialByteSource(String portName, int baudRate) {
this.portName = portName;
this.baudRate = baudRate;
}
public void openStreams() throws IOException {
CommPortIdentifier portId = null;
try {
portId = CommPortIdentifier.getPortIdentifier(portName);
}
catch (NoSuchPortException e) {
throw new IOException("Invalid port. " + allPorts());
}
try {
serialPort = (SerialPort)portId.open("SerialByteSource",
CommPortIdentifier.PORT_SERIAL);
String own = serialPort.getName();
}
catch (PortInUseException e) {
throw new IOException("Port " + portName + " busy");
}
try {
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
serialPort.setSerialPortParams(baudRate,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
}
catch (Exception e) {
serialPort.close();
throw new IOException("Couldn't configure " + portName);
}
// Try & run external program to setup serial port correctly
// (necessary on Linux, IBM's javax.comm leaves port in cooked mode)
try {
Runtime.getRuntime().exec("tinyos-serial-configure " + portName);
}
catch (IOException e) { }
is = serialPort.getInputStream();
os = serialPort.getOutputStream();
}
public void closeStreams() throws IOException {
serialPort.close();
}
public String allPorts() {
Enumeration ports = CommPortIdentifier.getPortIdentifiers();
if (ports == null)
return "No comm ports found!";
boolean noPorts = true;
String portList = "Known serial ports:\n";
while (ports.hasMoreElements()) {
CommPortIdentifier port = (CommPortIdentifier)ports.nextElement();
if (port.getPortType() == CommPortIdentifier.PORT_SERIAL) {
portList += "- " + port.getName() + "\n";
noPorts = false;
}
}
if (noPorts)
return "No comm ports found!";
else
return portList;
}
Object sync = new Object();
public byte readByte() throws IOException {
// On Linux at least, javax.comm input streams are not interruptible.
// Make them so, relying on the DATA_AVAILABLE serial event.
synchronized (sync) {
while (is.available() == 0) {
try {
sync.wait();
}
catch (InterruptedException e) {
close();
throw new IOException("interrupted");
}
}
}
return super.readByte(); ///////// -> VIEW THE NEXT CLASS
}
public void serialEvent(SerialPortEvent ev) {
// DEBUG
System.out.println(this.getClass().getName()+ ": serialEvent: EVENTO.");
if (ev.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
// DEBUG
System.out.println(this.getClass().getName()+ ": serialEvent: DATOS DISPONIBLES.");
synchronized (sync) {
sync.notify();
}
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package net.tinyos.packet;
import java.io.*;
abstract public class StreamByteSource implements ByteSource
{
protected InputStream is;
protected OutputStream os;
private boolean opened;
protected StreamByteSource() {
}
abstract protected void openStreams() throws IOException;
abstract protected void closeStreams() throws IOException;
public void open() throws IOException {
openStreams();
opened = true;
}
public void close() {
if (opened) {
opened = false;
try {
os.close();
is.close();
closeStreams();
}
catch (Exception e) { }
}
}
public byte readByte() throws IOException {
int serialByte;
if (!opened)
throw new IOException("not open");
try {
serialByte = is.read();
}
catch (IOException e) {
serialByte = -1;
}
if (serialByte == -1) {
close();
throw new IOException("read error");
}
return (byte)serialByte;
}
public void writeBytes(byte[] bytes) throws IOException {
if (!opened)
throw new IOException("not open");
try {
os.write(bytes);
os.flush();
}
catch (IOException e) {
close();
throw new IOException("write error");
}
}
}
I'm going crazy with this, so any help would be wellcomed!!!
Thank you very much. :-)