谁可以发给我个在windows下能用的JAVA串口通信啊~~ 谁可以发给我个在windows下能用的JAVA串口通信啊~~要可以用的 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 下面是Java 串口通信的核心代码,希望对你有用! import java.io.*; import java.util.*; import javax.comm.*; public class SimpleRead implements Runnable, SerialPortEventListener { static CommPortIdentifier portId; static Enumeration portList; InputStream inputStream; OutputStream outputStream; SerialPort serialPort; Thread readThread; public static void main(String[] args) { portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) { portId = (CommPortIdentifier) portList.nextElement(); if (portId.getPortType()==CommPortIdentifier.PORT_SERIAL) { if (portId.getName().equals( "COM1 ")) { SimpleRead reader = new SimpleRead(); } } } } public SimpleRead() { try {//打开串口COM1 serialPort = (SerialPort) portId.open( "SimpleReadApp ", 2000); } catch (PortInUseException e) {} try {//获得输入输出流 inputStream = serialPort.getInputStream(); outputStream = serialPort.getOutputStream(); } catch (IOException e) {} try {//为串口添加监听器 serialPort.addEventListener(this); } catch (TooManyListenersException e) {} serialPort.notifyOnDataAvailable(true); try {//配置串口 serialPort.setSerialPortParams(9600,//波特率9600bps SerialPort.DATABITS_7,//7位数据位 SerialPort.STOPBITS_1,//1位停止位 SerialPort.PARITY_EVEN);//偶校验 } catch (UnsupportedCommOperationException e) {} readThread = new Thread(this);//新建一线程 readThread.start();//启动线程 } public void run() { try { Thread.sleep(20000); } catch (InterruptedException e) {} } public void serialEvent(SerialPortEvent event) { switch(event.getEventType()) { case SerialPortEvent.BI: case SerialPortEvent.OE: case SerialPortEvent.FE: case SerialPortEvent.PE: case SerialPortEvent.CD: case SerialPortEvent.CTS: case SerialPortEvent.DSR: case SerialPortEvent.RI: case SerialPortEvent.OUTPUT_BUFFER_EMPTY: break; case SerialPortEvent.DATA_AVAILABLE://如果有数据到达 byte[] readBuffer = new byte[20]; try { while (inputStream.available() > 0) { //读取一个字节到readBuffer int numBytes = inputStream.read(readBuffer); } System.out.print(new String(readBuffer)); } catch (IOException e) {} break; } } } comm.jar 可以从我的网站,或者我在CSDN的资源里找到import java.util.Enumeration;import javax.comm.CommPortIdentifier;public class TestCom { public static void main(String argv[]) { new TestCom(); } public TestCom() { CommPortIdentifier portId; Enumeration ports = CommPortIdentifier.getPortIdentifiers(); if (ports == null) { System.out.println("No comm ports found!"); return; } // print out all ports System.out.println("printing all ports..."); while (ports.hasMoreElements()) { System.out.println(" " + ((CommPortIdentifier) ports.nextElement()).getName()); } }} Component.java里的一段代码,不知道第二个方法上的注释是什么意思,求助 jsp问题,各位大大帮帮忙 super(super(a)) 这个有意义吗? Log4j问题,请高人指教!! 如何在JTextArea中设置字体的颜色 各位高手帮小弟看个线程的小程序^_^ 一个实现了Runnable接口的类中还有其它方法的执行问题,请教 为什么选不中JTable首列的checkbox?? 关于ie打印的问题 求实体bean的例子 java串口程序出了个错误,我不知道哪里有错?请高手指点下,谢谢~~ 如何在当前程序中引入自己的包啊?
import java.io.*;
import java.util.*;
import javax.comm.*;
public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
OutputStream outputStream;
SerialPort serialPort;
Thread readThread;
public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType()==CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals( "COM1 ")) {
SimpleRead reader = new SimpleRead();
}
}
}
}
public SimpleRead() {
try {//打开串口COM1
serialPort = (SerialPort) portId.open( "SimpleReadApp ", 2000);
} catch (PortInUseException e) {}
try {//获得输入输出流
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();
} catch (IOException e) {}
try {//为串口添加监听器
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {}
serialPort.notifyOnDataAvailable(true);
try {//配置串口
serialPort.setSerialPortParams(9600,//波特率9600bps
SerialPort.DATABITS_7,//7位数据位
SerialPort.STOPBITS_1,//1位停止位
SerialPort.PARITY_EVEN);//偶校验
} catch (UnsupportedCommOperationException e) {}
readThread = new Thread(this);//新建一线程
readThread.start();//启动线程
}
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {}
}
public void serialEvent(SerialPortEvent event) {
switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE://如果有数据到达
byte[] readBuffer = new byte[20];
try {
while (inputStream.available() > 0) {
//读取一个字节到readBuffer
int numBytes = inputStream.read(readBuffer);
}
System.out.print(new String(readBuffer));
} catch (IOException e) {}
break;
}
}
}
public static void main(String argv[]) {
new TestCom();
} public TestCom() {
CommPortIdentifier portId; Enumeration ports = CommPortIdentifier.getPortIdentifiers(); if (ports == null) {
System.out.println("No comm ports found!");
return;
} // print out all ports
System.out.println("printing all ports...");
while (ports.hasMoreElements()) {
System.out.println(" " + ((CommPortIdentifier) ports.nextElement()).getName()); }
}}