linux下的串口读写包下载地址: http://to.agreatserver.com/rxtx21.zip win32平台的可以到sun的网站上下载。 下面是一个例子 //SerialTest.java //by aico //2004-05-11 import java.io.*; import java.util.*; import gnu.io.*; public class SerialTest { public static void main(String[] args) { String commname = "Com1"; //for win32 //String commname = "/dev/ttyS0"; // for linux SerialTest st = new SerialTest(); SerialPort port = st.getSerialPort(commname); if(port != null) { st.writeString(port,"hi,serial test."); port.close(); } } public SerialPort getSerialPort(String commname) { if(commname == null) return null; Enumeration portlist = CommPortIdentifier.getPortIdentifiers(); while (portlist.hasMoreElements()) { CommPortIdentifier portid = (CommPortIdentifier) portlist.nextElement(); if ((portid.getPortType() == CommPortIdentifier.PORT_SERIAL) && portid.getName().equalsIgnoreCase(commname)) { SerialPort port = null; try { port = (SerialPort)portid.open("test", 2000); }catch(PortInUseException e) { e.printStackTrace(); } return port; } } return null; } public void writeString(SerialPort port,String message) { if(port == null ¦ ¦ message == null) return; OutputStream out = null; try { out = port.getOutputStream(); } catch (IOException e) { e.printStackTrace(); return; } try { port.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE); } catch (UnsupportedCommOperationException e) { e.printStackTrace(); return; } try { out.write(message.getBytes()); } catch (IOException e) { e.printStackTrace(); return; } } } --------------------------------------------------------------- 或许对你有帮助: http://www-900.ibm.com/developerWorks/cn/java/joy-comm/index.shtml
java 串口读写_java读写串口
最新推荐文章于 2024-05-23 16:38:42 发布