简体   繁体   中英

serial port twoway communication java

Hi i'm begginer in java,in my project datalogger is connected to com port i have to send 15 integer value to port then other device will send back 15 as a response,now i'm writing to outputstream but i'm not getting response.how to solve this problem plz help me.(i'm using javax.com package)

thanks for reply

You have to get an InputStream as well, you can't read from the OutputStream. Or am I missing something?

Also, remember to do OutputStream.flush() after writing your output, otherwise your data might be buffered to be sent later - if the responder is waiting for your data, this is most likely where things goes wrong.

Having said that: the javax.comm package is really old. Last time I worked with it, it almost seemed deprecated by Sun, or at least not maintained in any way. You might want to look at other solutions ( SerialIO comes to mind).

Try following sample code

public static void init(String port) {
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL){
System.out.println(portId.getName());
    if (portId.getName().equals(port)) {



try {sPort = (SerialPort) portId.open("PORT_NAME", 2000);
             reader = new sms();
             break;
            } 
        catch (Exception e) { System.out.println(e);continue;}
   }
}

}

and call init() method with com port name (like COM15,COM11,COM12 etc..) check your device com port to which it is connected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM