java - Reading wrong value from serial port -
java - Reading wrong value from serial port -
i have read 12-digit tag number rfid reader , print console. when utilize programme read tag weird spacing in between.
e.g. tag number 4400e6ef1a57. when maintain scanning tag, console window shows following:
4400e6ef1 a57
4400e 6ef1a57
4400e6ef1a57
4400e6ef1 a57
4400e6ef1a57
4400 e6ef1a57
4 400e6ef1a57
4400e6ef1a 57
4 400e6ef1a57
4400e6ef1a5 7
4400e6ef1a57
4400e6ef1 a57
4400e6ef1a57
4400e 6ef1a57
4400 e6ef1a57
4400e6ef1a57
4400e6ef1a57
4400e6ef 1a57
4400e 6ef1a57
it appears there long string of 0's , 1's gets read in , few of actual tag ids. don't know in order reading these 0's , 1's.
here code: (some sql , jdbc stuff incorporated, can ignore)
import java.io.*; import java.util.*; import gnu.io.*; import java.sql.*; public class trying5 implements runnable, serialporteventlistener { static enumeration portlist; static commportidentifier portid; serialport serialport; inputstream inputstream; thread readthread; connection con; public static void main(string[] args) { portlist = commportidentifier.getportidentifiers(); while (portlist.hasmoreelements()) { portid = (commportidentifier) portlist.nextelement(); if (portid.getporttype() == commportidentifier.port_serial) { if (portid.getname().equals("com3")) { trying5 reader = new trying5(); } } } } public trying5() { seek { serialport = (serialport) portid.open("trying5application", 2000); } grab (portinuseexception e) { system.out.println(e); } seek { inputstream = serialport.getinputstream(); } grab (ioexception e) { system.out.println(e); } seek { serialport.addeventlistener(this); } grab (toomanylistenersexception e) { system.out.println(e); } serialport.notifyondataavailable(true); seek { serialport.setserialportparams(9600, serialport.databits_8, serialport.stopbits_1, serialport.parity_none); } grab (unsupportedcommoperationexception e) { system.out.println(e); } readthread = new thread(this); readthread.start(); } public void run() { seek { thread.sleep(20000); } grab (interruptedexception e) { system.out.println(e); } } public void serialevent(serialportevent event) { switch(event.geteventtype()) { case serialportevent.bi: case serialportevent.oe: case serialportevent.fe: case serialportevent.pe: case serialportevent.cd: case serialportevent.cts: case serialportevent.dsr: case serialportevent.ri: case serialportevent.output_buffer_empty: break; case serialportevent.data_available: byte[] readbuffer = new byte[20]; // print console seek { while (inputstream.available() > 0) { int numbytes = inputstream.read(readbuffer); } string newtuple = new string(readbuffer); usercon newcon = new usercon(con, newtuple); system.out.print(newtuple + "\n"); } grab (ioexception e) { system.out.println(e); } break; } } }
the stream gives has, , add together newlines:
system.out.print(newtuple + "\n");
you need buffer of sort, bufferedinputstream.
java serial-port
Comments
Post a Comment