rxtx - Java check com port connection -
i looking bit of efficient code can assist me in monitoring if com port still open using rx/tx libraries.
lets have hardware device communicates pc using virtual com port , device can plugged in , out @ time. want show connection status on pc.
i have tried buffered reader below , registered device gets disconnected have re-open port scratch in method.
i looking short comport.isopen () or something?
// set value of running start.isrunning = true; // check see if device connected while (start.isrunning) { // try connect device try { // create buffered reader bufferedreader reader = new bufferedreader( new inputstreamreader(serialport.getinputstream())); // read output if (character.tostring((char) reader.read()).equalsignorecase( "^")) { // set connected flag start.connected_flag = true; // set connected fag addcomponents.tfconnected.settext("connected"); } // close reader reader.close(); // let thread sleep thread.sleep(500); } // catch error if device disconnected catch (exception err) { // set connected flag start.connected_flag = false; // set connected fag addcomponents.tfconnected.settext("disconnected"); // let thread sleep thread.sleep(500); } }
disclaimer: consider partial answer because not have intimate knowledge of workings of serial ports, , tests not produce useful. posting here regardless in hopes of helpful.
unfortunately, far know, there no way receive kind of "connection / disconnection" event messages. sadly, not intimately familiar workings of serial ports, cannot give full , proper explanation. however, some research, 1 of answers posted in forum had say:
there's no event system inform of [a disconnection event] because require exclusive use of com port. if have serialport object created , have opened port should cdchanged when devices plugged in , unplugged serial port. assumes device follows pins standards; not devices do.
note poster, , link i've provided, discussing within context of c#. seems related how ports work in general, regardless of language, confident same can applied rxtx java.
there some events can attempt listen for. in tests ever able receive data_available event, setup bit different (raspberry pi) , can't @ moment physically disconnect device port, can attempt block device file (which may explain failure of test).
if attempt event listening yourself, have class implement serialportlistener, register desired events, check events in serialevent method. here example:
public class yourclass implements serialportlistener{ private serialport serialport; // ... serial port gets set @ point ... public void registerevents(){ serialport.addeventlistener(this); // listen events serialport.notifyonbreakinterrupt(true); serialport.notifyoncarrierdetect(true); serialport.notifyoncts(true); serialport.notifyondataavailable(true); serialport.notifyondsr(true); serialport.notifyonframingerror(true); serialport.notifyonoutputempty(true); serialport.notifyonoverrunerror(true); serialport.notifyonparityerror(true); serialport.notifyonringindicator(true); } @override public void serialevent(serialportevent event) { system.out.println("received event. type: " + event.geteventtype() + ", old value: " + event.getoldvalue() + ", new value: " + event.getnewvalue()); } } if fails, believe other alternative similar current solution; attempt read port, , if fails, consider disconnected, , set indicator accordingly. @ each iteration, if disconnected, attempt reconnect; if reconnect succeeds, reset indicator "connected".
sorry cannot of more assistance. of may lead useful.
side note:
if want dry code slightly, put thread.sleep(500) in block instead, since appears executed regardless.
Comments
Post a Comment