I'm trying to get some values from rs232 (card reader Roger prt66lt) with a java simple serial connetor (jssc) but it look like i dont get any events started. I'm using the example code. The card reader is set to send card number when he reads it. But i dont get any event started coz i dont see event that System.out. I'm including jssc.jar (from 2.5.0 version) and its connecting to divice. Is this version of jssc doesnt need any .dll ? I think its included in it ? The similar code to this one but with RXTX library works fine but i need to include attahed dll library.
Card reader send me HEX value: 02, 10 chars DATA, 0D, 0A, 03
Code:
import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import jssc.SerialPortException;
public class Main {
static SerialPort serialPort;
public static void main(String[] args) {
serialPort = new SerialPort("COM4");
try {
serialPort.openPort();//Open port
serialPort.setParams(9600, 8, 1, 0);//Set params
int mask = SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR;//Prepare mask
serialPort.setEventsMask(mask);//Set mask
serialPort.addEventListener(new SerialPortReader());//Add SerialPortEventListener
}
catch (SerialPortException ex) {
System.out.println(ex);
}
}
static class SerialPortReader implements SerialPortEventListener {
public void serialEvent(SerialPortEvent event) {
System.out.println("Event started");
if(event.isRXCHAR()){//If data is available
if(event.getEventValue() == 10){//Check bytes count in the input buffer
//Read data, if 10 bytes available
try {
byte buffer[] = serialPort.readBytes(10);
}
catch (SerialPortException ex) {
System.out.println(ex);
}
}
}
else if(event.isCTS()){//If CTS line has changed state
if(event.getEventValue() == 1){//If line is ON
System.out.println("CTS - ON");
}
else {
System.out.println("CTS - OFF");
}
}
else if(event.isDSR()){///If DSR line has changed state
if(event.getEventValue() == 1){//If line is ON
System.out.println("DSR - ON");
}
else {
System.out.println("DSR - OFF");
}
}
}
}
}
The problem was that the default setting of RTS and DTR line is High so i need to use another setParams method and set the RTS line to false as needed by device.
Solution:
serialPort.setParams(9600, 8, 1, 0, false, true);