Bidirectional port communication using java swing

screenshot

I want to perform bi-directional communication with an integrated controller using Java through the COM port. When I send a string to the controller, my controller sends me the return response:

Example string 01 04 01 00 00 0C F1 F3
Public class SerialClass implements SerialPortEventListener
{

SerialPort public serialPort;
/ ** The port that we will normally use. * /
static static end String PORT_NAMES[] =
{
"/dev/tty.usbserial-A9007UX1", // Mac OS X
"/ dev / ttyUSB0", // Linux
"COM9", // Windows
};

public static input BufferedReader;
Public static OutputStream output;
/ ** Milliseconds to block while waiting for the open port * /
public static final int TIME_OUT = 200;
/ ** Default bits per second for the COM port. * /
public static final int DATA_RATE = 9600;

initialize public emptiness ()
{
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers ();

// First, find a serial port instance as set in PORT_NAMES.
while (portEnum.hasMoreElements ())
{
CommPortIdentifier currPortId = (CommPortIdentifier) ​​portEnum.nextElement ();
for (String portName: PORT_NAMES)
{
if (currPortId.getName (). equals (portName))
{
portId = currPortId;
break;
}
}
}
if (portId == null)
{
System.out.println ("The COM port could not be found");
he came back;
}

try
{
// open the serial port and use the name of the class for the appName.
serialPort = (SerialPort) portId.open (this.getClass (). getName (),
TIME IS OVER);

// set the port parameters
serialPort.setSerialPortParams (DATA_RATE,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);

// open the streams
input = new BufferedReader (new InputStreamReader (serialPort.getInputStream ()));
output = serialPort.getOutputStream ();
char ch = 1;
output.write (ch);

// add listeners of events
serialPort.addEventListener (this);
serialPort.notifyOnDataAvailable (true);
}
capture (Exception e)
{
System.err.println (e.toString ());
}
}

synchronized public close ()
{
yes (serialPort! = null)
{
serialPort.removeEventListener ();
serialPort.close ();
}
}

@Cancel
public synchronized empty serialEvent (SerialPortEvent oEvent)
{
if (oEvent.getEventType () == SerialPortEvent.DATA_AVAILABLE)
{
try
{
String inputLine = input.readLine ();
System.out.println ("aa" + inputLine);
}
capture (Exception e)
{
System.err.println (e.toString ());
}
}

}

writeData static and synchronized empty (string data)
{
System.out.println ("Sent:" + data);
try
{
output.write (data.getBytes ());
}
capture (Exception e)
{
System.out.println ("could not write to the port");
}
}

Empty main static public (String[] args) launches Exception {
SerialClass main = new SerialClass ();
main.initialize ();
Thread t = new thread ()
{
public void run ()
{
// the next line will keep this application alive for 1000 seconds,
// waiting for events to occur and responding (printing incoming messages to the console).
try
{
Thread.sleep (1500);
writeData ("01 04 01 00 00 0C F1 F3");

// serialEvent
}
catch (InterruptedException ie)
{

}
}
};
t.start ();
System.out.println ("Started");
}
}