Reputation: 599
I have modified the example shown on https://code.google.com/p/java-simple-serial-connector/wiki/jSSC_examples to show read/write from java program. I can run the program, however the data I send using serialPort.writeString("HelloWorld"); does not seem to be read in the SerialPortReader event class. Could any one please point what the issue is ?
public class SerialReaderWriter {
static SerialPort serialPort;
public static void main(String[] args) {
serialPort = new SerialPort("COM1");
try {
serialPort.openPort();
serialPort.setParams(9600, 8, 1, 0);
//Preparing a mask. In a mask, we need to specify the types of events that we want to track.
//Well, for example, we need to know what came some data, thus in the mask must have the
//following value: MASK_RXCHAR. If we, for example, still need to know about changes in states
//of lines CTS and DSR, the mask has to look like this: SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR
int mask = SerialPort.MASK_RXCHAR;
//Set the prepared mask
serialPort.setEventsMask(mask);
//Add an interface through which we will receive information about events
serialPort.addEventListener(new SerialPortReader());
serialPort.writeString("HelloWorld");
}
catch (SerialPortException ex) {
System.out.println(ex);
}
}
static class SerialPortReader implements SerialPortEventListener {
public void serialEvent(SerialPortEvent event) {
//Object type SerialPortEvent carries information about which event occurred and a value.
//For example, if the data came a method event.getEventValue() returns us the number of bytes in the input buffer.
System.out.println(event.getEventType());
if(event.isRXCHAR()){
if(event.getEventValue() == 10){
try {
String data= serialPort.readString();
System.out.println(data);
}
catch (SerialPortException ex) {
System.out.println(ex);
}
}
}
//If the CTS line status has changed, then the method event.getEventValue() returns 1 if the line is ON and 0 if it is OFF.
else if(event.isCTS()){
if(event.getEventValue() == 1){
System.out.println("CTS - ON");
}
else {
System.out.println("CTS - OFF");
}
}
else if(event.isDSR()){
if(event.getEventValue() == 1){
System.out.println("DSR - ON");
}
else {
System.out.println("DSR - OFF");
}
}
}
}
}
Upvotes: 2
Views: 18335
Reputation: 114
You can't read data from the same port where you write(COM1 here). I have followed the below steps for reading and writing using JSSC.
Fake your serial port with SerialPortMonitor.
Send data from COM2 from the SerialPortMonitor device installed.
Mode->Spy would show your written string "HelloWorld" and received String "OK"
Make the below modifications and check your code:
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN |
SerialPort.FLOWCONTROL_RTSCTS_OUT);
serialPort.writeBytes("HelloWorld");//Write data to port
PortReader portReader=new PortReader(serialPort)
serialPort.addEventListener(portReader, SerialPort.MASK_RXCHAR);
int[][] eventArray=serialPort.waitEvents()
for (int i = 0; i < eventArray.length; i++) {
if ((eventArray[i][0] > 0) ) {
serialPort.eventListener.serialEvent(new SerialPortEvent("COM1", eventArray[i][0], eventArray[i][1])); // wait for the listener event to complete
}
}
The port reader class: (You were missing the Override annotation and passing in the serial port)
public class PortReader implements SerialPortEventListener{
SerialPort serialPort
public PortReader(){}
public PortReader(SerialPort serialPort){this.serialPort=serialPort}
@Override
public void serialEvent(SerialPortEvent event) {
if(event.isRXCHAR() && event.getEventValue() > 0) {
try {
String receivedData = this.serialPort.readString(event.getEventValue());
System.out.println("Received response: " + receivedData);
this.serialPort.closePort();//Close serial port
}
catch (SerialPortException ex) {
System.out.println("Error in receiving string from COM-port: " + ex);
this.serialPort.closePort();//Close serial port
}
}
}
}
Upvotes: 1
Reputation: 1
if your device doesn't require RTS/CTS flow control or you dont have a fully connected serial cable
( only RX, TX, GND)
you should switch off the data terminal ready (dtr) signal for the serial communication
add this
serialPort.setRTS(false);
serialPort.setDTR(false);
after
serialPort.addEventListener(new SerialPortReader());
Upvotes: 0
Reputation: 16
The command
serialPort.writeString("HelloWorld");
sends the string
HelloWorld
from COM1. The SerialPortReader class that you are implementing causes COM1 to listen for the event type isRXCHAR (AKA when COM1 receives a char).
Do you have a serial cable connected to the serial port?
Unless you cross the RX and TX pins of COM1 or have a separate COM port (whose TX and RX is connected to COM1's RX and TX pins respectively), the SerialPortReader will never be activated.
Upvotes: 0