上一章说布局,这一章说相关的类
串口类
网上找的,很容易看懂,我自己根据我的项目做了改动,原文链接如下
https://www.cnblogs.com/dodoJavaLearner/p/5428418.html
创建包 存放class类
这是串口类 里面关于串口的操作,比如打开、写入、监听、关闭等
public class SerialManager implements Runnable, SerialPortEventListener{
private String appName = "触摸屏 端口COM3 测试";
private int timeout = 2000;//open 端口时的等待时间
private int threadTime = 0;//线程时间
private CommPortIdentifier commPort;
private SerialPort serialPort;
private InputStream inputStream;//输入流
private OutputStream outputStream;//输出流
/**
* @方法名称 :listPort
* @功能描述 :列出所有可用的串口
* @返回值类型 :void
*/
@SuppressWarnings("rawtypes")
public void listPort(){
CommPortIdentifier cpid;
Enumeration en = CommPortIdentifier.getPortIdentifiers();
System.out.println("现在列出这个PC的所有端口:" + en);
while(en.hasMoreElements()){
cpid = (CommPortIdentifier)en.nextElement();
if(cpid.getPortType() == CommPortIdentifier.PORT_SERIAL){
System.out.println(cpid.getName() + ", " + cpid.getCurrentOwner());
}
}
}
public SerialManager() {}
/**
* @方法名称 :selectPort
* @功能描述 :选择一个端口,比如:COM1
* @返回值类型 :void
* @param portName
* @throws UnsupportedCommOperationException
*/
@SuppressWarnings("rawtypes")
public void selectPort(String portName) throws UnsupportedCommOperationException{
this.commPort = null;
CommPortIdentifier cpid;
Enumeration en = CommPortIdentifier.getPortIdentifiers();//获取端口标识符
while(en.hasMoreElements()){
cpid = (CommPortIdentifier)en.nextElement();
if(cpid.getPortType() == CommPortIdentifier.PORT_SERIAL&& cpid.getName().equals(portName)){
this.commPort = cpid;
bre