Java operates serial port equipment

zhaozj2021-02-16  84

To do financial terminal development, you often need to write some equipment control procedures, extracting a basic framework for serial port operations to prevent you forget, of course, for specific serial equipment might want to make relevant modifications.

Import java.io. *; import javax.comm. *;

Public Class SomeSerial Implements SerialPorteventListener {Private Byte [] ReadInfo; Private InputStream In; Private OutputStream Out; Private SerialPort SerialPort = NULL;

public SomeSerial (String port) throws Exception {CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier (port); serialPort = (SerialPort) portId.open ( "Serial_Communication", 2000); serialPort.setSerialPortParams (9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort .Parity_none);

public synchronized void serialEvent (SerialPortEvent event) {switch (event.getEventType ()) {case SerialPortEvent.BI: break; case SerialPortEvent.OE: break; case SerialPortEvent.FE: break; case SerialPortEvent.PE: break; case SerialPortEvent.CD : break; case SerialPortEvent.CTS: break; case SerialPortEvent.DSR: break; case SerialPortEvent.RI: break; case SerialPortEvent.OUTPUT_BUFFER_EMPTY: break; case SerialPortEvent.DATA_AVAILABLE: synchronized (this) {try {int count = in.available ( ); Readinfo = new byte [count]; in.read (readinfo);} catch (exception e) {E.PrintStackTrace ();} this.notify ();} Break;}}

Public int nstop () throws exception {in.close (); out.close (); serialport.close (); return 0;}

转载请注明原文地址:https://www.9cbs.com/read-12752.html

New Post(0)