import javax.comm.*; public class SerialCommunication { public static void main(String[] args) { SerialPort serialPort; try { CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier("COM1"); serialPort = (SerialPort) portId.open("SerialCommunication", 2000); serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); InputStream in = serialPort.getInputStream(); OutputStream out = serialPort.getOutputStream(); String data = "Hello, Serial Device!"; out.write(data.getBytes()); byte[] buffer = new byte[1024]; int len = in.read(buffer); String receivedData = new String(buffer, 0, len); System.out.println("Received data: " + receivedData); in.close(); out.close(); serialPort.close(); } catch (Exception e) { e.printStackTrace(); } } } properties javax.comm.SerialPort = com.example.SerialPortImpl


上一篇:
下一篇:
切换中文