Java Serial Ports Run Confguration -
i want read , write serial data on same laptop , try code. works ports chosen run configurations selecting com1 , com2 1 one. however; when want use ports again in same way, error appears nosuchportexception there anyway disconnect ports second usage?
package javaserial; import java.util.scanner; import gnu.io.commportidentifier; import gnu.io.serialport; public class rs232 { public void connect(string portname) throws exception { commportidentifier portidentifier = commportidentifier.getportidentifier(portname); if (portidentifier.iscurrentlyowned()) { system.out.println("port in use!"); } else { // points owns port , connection timeout serialport serialport = (serialport) portidentifier.open("rs232example", 2000); // setup connection parameters serialport.setserialportparams( 38400, serialport.databits_8, serialport.stopbits_1, serialport.parity_none); // setup serial port writer commportsender.setwriterstream(serialport.getoutputstream()); // setup serial port reader new commportreceiver(serialport.getinputstream()).start(); } } public static void main(string[] args) throws exception { string textname; int startindex; int bytelength; scanner scan = new scanner(system.in); system.out.println("textname"); textname = scan.nextline(); system.out.println("startindex"); startindex = scan.nextint(); system.out.println("bytelength"); bytelength = scan.nextint(); // connects port name (e.g. com1) in first argument new rs232().connect(args[0]); // send helo message through serial port using protocol implementation commportsender.send(new protocolimpl().getmessage(new readfileintobytearray(textname, startindex, bytelength).getfilestring())); } }
Comments
Post a Comment