two way com port communication using java swing [on hold]
up vote
-1
down vote
favorite
I want to do two way communication with an embedded controller using Java over the COM port. When I send some 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
{
public SerialPort serialPort;
/** The port we're normally going to use. */
private static final String PORT_NAMES =
{
"/dev/tty.usbserial-A9007UX1", // Mac OS X
"/dev/ttyUSB0", // Linux
"COM9", // Windows
};
public static BufferedReader input;
public static OutputStream output;
/** Milliseconds to block while waiting for port open */
public static final int TIME_OUT = 200;
/** Default bits per second for COM port. */
public static final int DATA_RATE = 9600;
public void initialize()
{
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
//First, Find an instance of serial port 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("Could not find COM port.");
return;
}
try
{
// open serial port, and use class name for the appName.
serialPort = (SerialPort) portId.open(this.getClass().getName(),
TIME_OUT);
// set 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 event listeners
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
}
catch (Exception e)
{
System.err.println(e.toString());
}
}
public synchronized void close()
{
if (serialPort != null)
{
serialPort.removeEventListener();
serialPort.close();
}
}
@Override
public synchronized void serialEvent(SerialPortEvent oEvent)
{
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE)
{
try
{
String inputLine=input.readLine();
System.out.println("aa"+inputLine);
}
catch (Exception e)
{
System.err.println(e.toString());
}
}
}
public static synchronized void writeData(String data)
{
System.out.println("Sent: " + data);
try
{
output.write(data.getBytes());
}
catch (Exception e)
{
System.out.println("could not write to port");
}
}
public static void main(String args) throws Exception {
SerialClass main = new SerialClass();
main.initialize();
Thread t=new Thread()
{
public void run()
{
//the following line will keep this app alive for 1000 seconds,
//waiting for events to occur and responding to them (printing incoming messages to console).
try
{
Thread.sleep(1500);
writeData("01 04 01 00 00 0C F1 F3");
//serialEvent
}
catch (InterruptedException ie)
{
}
}
};
t.start();
System.out.println("Started");
}
}
java serial-port
New contributor
put on hold as unclear what you're asking by πάντα ῥεῖ, Graipher, l0b0, яүυк, t3chb0t yesterday
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
-1
down vote
favorite
I want to do two way communication with an embedded controller using Java over the COM port. When I send some 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
{
public SerialPort serialPort;
/** The port we're normally going to use. */
private static final String PORT_NAMES =
{
"/dev/tty.usbserial-A9007UX1", // Mac OS X
"/dev/ttyUSB0", // Linux
"COM9", // Windows
};
public static BufferedReader input;
public static OutputStream output;
/** Milliseconds to block while waiting for port open */
public static final int TIME_OUT = 200;
/** Default bits per second for COM port. */
public static final int DATA_RATE = 9600;
public void initialize()
{
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
//First, Find an instance of serial port 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("Could not find COM port.");
return;
}
try
{
// open serial port, and use class name for the appName.
serialPort = (SerialPort) portId.open(this.getClass().getName(),
TIME_OUT);
// set 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 event listeners
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
}
catch (Exception e)
{
System.err.println(e.toString());
}
}
public synchronized void close()
{
if (serialPort != null)
{
serialPort.removeEventListener();
serialPort.close();
}
}
@Override
public synchronized void serialEvent(SerialPortEvent oEvent)
{
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE)
{
try
{
String inputLine=input.readLine();
System.out.println("aa"+inputLine);
}
catch (Exception e)
{
System.err.println(e.toString());
}
}
}
public static synchronized void writeData(String data)
{
System.out.println("Sent: " + data);
try
{
output.write(data.getBytes());
}
catch (Exception e)
{
System.out.println("could not write to port");
}
}
public static void main(String args) throws Exception {
SerialClass main = new SerialClass();
main.initialize();
Thread t=new Thread()
{
public void run()
{
//the following line will keep this app alive for 1000 seconds,
//waiting for events to occur and responding to them (printing incoming messages to console).
try
{
Thread.sleep(1500);
writeData("01 04 01 00 00 0C F1 F3");
//serialEvent
}
catch (InterruptedException ie)
{
}
}
};
t.start();
System.out.println("Started");
}
}
java serial-port
New contributor
put on hold as unclear what you're asking by πάντα ῥεῖ, Graipher, l0b0, яүυк, t3chb0t yesterday
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I want to do two way communication with an embedded controller using Java over the COM port. When I send some 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
{
public SerialPort serialPort;
/** The port we're normally going to use. */
private static final String PORT_NAMES =
{
"/dev/tty.usbserial-A9007UX1", // Mac OS X
"/dev/ttyUSB0", // Linux
"COM9", // Windows
};
public static BufferedReader input;
public static OutputStream output;
/** Milliseconds to block while waiting for port open */
public static final int TIME_OUT = 200;
/** Default bits per second for COM port. */
public static final int DATA_RATE = 9600;
public void initialize()
{
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
//First, Find an instance of serial port 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("Could not find COM port.");
return;
}
try
{
// open serial port, and use class name for the appName.
serialPort = (SerialPort) portId.open(this.getClass().getName(),
TIME_OUT);
// set 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 event listeners
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
}
catch (Exception e)
{
System.err.println(e.toString());
}
}
public synchronized void close()
{
if (serialPort != null)
{
serialPort.removeEventListener();
serialPort.close();
}
}
@Override
public synchronized void serialEvent(SerialPortEvent oEvent)
{
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE)
{
try
{
String inputLine=input.readLine();
System.out.println("aa"+inputLine);
}
catch (Exception e)
{
System.err.println(e.toString());
}
}
}
public static synchronized void writeData(String data)
{
System.out.println("Sent: " + data);
try
{
output.write(data.getBytes());
}
catch (Exception e)
{
System.out.println("could not write to port");
}
}
public static void main(String args) throws Exception {
SerialClass main = new SerialClass();
main.initialize();
Thread t=new Thread()
{
public void run()
{
//the following line will keep this app alive for 1000 seconds,
//waiting for events to occur and responding to them (printing incoming messages to console).
try
{
Thread.sleep(1500);
writeData("01 04 01 00 00 0C F1 F3");
//serialEvent
}
catch (InterruptedException ie)
{
}
}
};
t.start();
System.out.println("Started");
}
}
java serial-port
New contributor
I want to do two way communication with an embedded controller using Java over the COM port. When I send some 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
{
public SerialPort serialPort;
/** The port we're normally going to use. */
private static final String PORT_NAMES =
{
"/dev/tty.usbserial-A9007UX1", // Mac OS X
"/dev/ttyUSB0", // Linux
"COM9", // Windows
};
public static BufferedReader input;
public static OutputStream output;
/** Milliseconds to block while waiting for port open */
public static final int TIME_OUT = 200;
/** Default bits per second for COM port. */
public static final int DATA_RATE = 9600;
public void initialize()
{
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
//First, Find an instance of serial port 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("Could not find COM port.");
return;
}
try
{
// open serial port, and use class name for the appName.
serialPort = (SerialPort) portId.open(this.getClass().getName(),
TIME_OUT);
// set 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 event listeners
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
}
catch (Exception e)
{
System.err.println(e.toString());
}
}
public synchronized void close()
{
if (serialPort != null)
{
serialPort.removeEventListener();
serialPort.close();
}
}
@Override
public synchronized void serialEvent(SerialPortEvent oEvent)
{
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE)
{
try
{
String inputLine=input.readLine();
System.out.println("aa"+inputLine);
}
catch (Exception e)
{
System.err.println(e.toString());
}
}
}
public static synchronized void writeData(String data)
{
System.out.println("Sent: " + data);
try
{
output.write(data.getBytes());
}
catch (Exception e)
{
System.out.println("could not write to port");
}
}
public static void main(String args) throws Exception {
SerialClass main = new SerialClass();
main.initialize();
Thread t=new Thread()
{
public void run()
{
//the following line will keep this app alive for 1000 seconds,
//waiting for events to occur and responding to them (printing incoming messages to console).
try
{
Thread.sleep(1500);
writeData("01 04 01 00 00 0C F1 F3");
//serialEvent
}
catch (InterruptedException ie)
{
}
}
};
t.start();
System.out.println("Started");
}
}
java serial-port
java serial-port
New contributor
New contributor
edited 2 days ago
Graipher
22.3k53284
22.3k53284
New contributor
asked 2 days ago
amit gavli
11
11
New contributor
New contributor
put on hold as unclear what you're asking by πάντα ῥεῖ, Graipher, l0b0, яүυк, t3chb0t yesterday
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as unclear what you're asking by πάντα ῥεῖ, Graipher, l0b0, яүυк, t3chb0t yesterday
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes