Hello
I should probably show you the code.
import java.io.*;
import java.util.*;
import gnu.io.*; // for rxtxSerial library
//J2C2 is the name of the robot.
public class J2C2 implements Runnable
{
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
static OutputStream outputStream;
public static void main(String args)
{
boolean portFound = false;
String defaultPort = “COM6”;
portList = CommPortIdentifier.getPortIdentifiers(); //gets all aviable ports
while (portList.hasMoreElements())
{
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) //if the port is a serial port then enter
{
if (portId.getName().equals(defaultPort)) //if the defaultPort == the serial port name enter
{
System.out.println("Found port: "+defaultPort);
portFound = true;
// init reader thread
J2C2 reader = new J2C2();
}
}
}
if (!portFound)
{
System.out.println(“port " + defaultPort + " not found.”);
}
}
public void initwritetoport()
{
try
{
outputStream = serialPort.getOutputStream(); // get the outputstream
} catch (IOException e) {}
try
{
serialPort.notifyOnOutputEmpty(true);// activate the OUTPUT_BUFFER_EMPTY notifier
} catch (Exception e)
{
System.out.println(“Error setting event notification”);
System.out.println(e.toString());
System.exit(-1);
}
}
public void writetoport()
{
System.out.println("Writing to "+serialPort.getName());
try
{
outputStream.write(255); // The data i’m trying to write to the USART via the bluetooth module
} catch (IOException e) {}
}
public J2C2()
{
try // initalize serial port (opens the port)
{
serialPort = (SerialPort) portId.open(“J2C2 Control”, 2000); //Aplication currently that will use the port doesn’t have to be a method or class
} catch (PortInUseException e) {}
try // Gets an inputStream from the opened port
{
inputStream = serialPort.getInputStream();
} catch (IOException e) {System.out.println(“HERE1”);}
// activate the DATA_AVAILABLE notifier
serialPort.notifyOnDataAvailable(true);
try // sets up port parameters baud rate etc…
{
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.notifyOnOutputEmpty(true);
} catch (UnsupportedCommOperationException e) {}
// start the read thread
readThread = new Thread(this);
readThread.start();
}
public void run()
{
// first thing in the thread, we initialize the write operation
initwritetoport();
try
{
while (true)
{
writetoport();// write string to port, the serialEvent will read it
Thread.sleep(1000);
}
} catch (InterruptedException e) {}
}
}
It has worked before on the serial port when communicating with sc322 servo board. If anyone can comfirm that this code looks ok then at least that’s one less place to look for any problems. Thanks