1、首先下载Javax.comm 下载地址:http://mdubuc.freeshell.org/Jolt/javacomm20-win32.zip
2、将文件win32comm.dll拷贝到%JAVA_HOME%\bin
3、在项目中导入comm.jar
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.TooManyListenersException;
import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException;
public class PrintTest implements SerialPortEventListener {
InputStream inputStream; // 从串口来的输入流
OutputStream outputStream;// 向串口输出的流
SerialPort serialPort; // 串口的引用
CommPortIdentifier portId;
public PrintTest(Enumeration portList, String name) {
while (portList.hasMoreElements()) {
CommPortIdentifier temp = (CommPortIdentifier) portList
.nextElement();
if (temp.getPortType() == CommPortIdentifier.PORT_SERIAL) {// 判断如果端口类型是串口
if (temp.getName().equals(name)) { // 判断如果端口已经启动就连接
portId = temp;
}
}
}
try {
serialPort = (SerialPort) portId.open("My" + name, 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, SerialPort.DATABITS_8, // 设置串口读写参数
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
}
System.out.println(portId);
System.out.println(serialPort);
}
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) {
System.out.println(inputStream.available());
int numBytes = inputStream.read(readBuffer);
System.out.println(numBytes);
}
System.out.println(new String(readBuffer).trim());
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
public void send(String content) {
try {
outputStream.write((content).getBytes("gbk"));
// outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public void ClosePort() {
System.out.println(serialPort);
if (serialPort != null) {
serialPort.close();
}
}
}
import java.util.Enumeration;
import javax.comm.CommPortIdentifier;
public class Test {
public static void main(String[] args) throws InterruptedException {
Enumeration portList = CommPortIdentifier.getPortIdentifiers(); //得到当前连接上的端口
PrintTest comm3 = new PrintTest(portList,"COM4");
int lineOffset=5;
StringBuffer cmd_sb=new StringBuffer();
cmd_sb.append(0x1b+" DOWNLOAD \"XJMEI.BAS\"\r");
cmd_sb.append("CLS\r");
cmd_sb.append("SET COUNTER @1 1\r");
cmd_sb.append("@1=\"xjmei\"\r");
cmd_sb.append("TEXT 60,10,\"3\",0,1,1,@1\r");
cmd_sb.append("PRINT 1,1\r");
cmd_sb.append("EOP\r");
comm3.send(cmd_sb.toString());
comm3.ClosePort();
}
}