📄 serialbean.java
字号:
package serial;
import java.io.*;
import java.util.*;
import javax.comm.*;
import javax.swing.*;
public class SerialBean implements SerialPortEventListener
{
static CommPortIdentifier portId;
static SerialPort serialPort;
static OutputStream outputStream;
static InputStream inputStream;
static Enumeration portList;
static String PortName;
String Msg,Content;
boolean boolean1;
int ChangDu,ChangDu1,ChangDu2; //记录数据长度的变量
SerialBuffer SB = new SerialBuffer();
public SerialBean(int PortID)
{
PortName = "COM" + PortID;
}
public void Initialize()
{
boolean portFound = false;
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements())
{
portId = (CommPortIdentifier) portList.nextElement();
if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
if (portId.getName().equals(PortName))
{
portFound = true;
try
{
serialPort = (SerialPort)
portId.open("SimpleWriteApp", 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(19200,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e1) {}
}
}
}
if(!portFound)
{
JOptionPane.showMessageDialog(null, PortName+"串口不存在!",
"系统提示",JOptionPane.INFORMATION_MESSAGE);
}
}
public void XiaoYanWei(int MS)
{
if(MS == 1)
{
try
{
serialPort.setSerialPortParams(19200,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_ODD);
}catch(UnsupportedCommOperationException e){}
}
if(MS == 2)
{
try
{
serialPort.setSerialPortParams(19200,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_EVEN);
}catch(UnsupportedCommOperationException e){}
}
if(MS == 3)
{
try
{
serialPort.setSerialPortParams(19200,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
}catch(UnsupportedCommOperationException e){}
}
}
public void serialEvent(SerialPortEvent event)
{
int c;
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:
try
{
while (inputStream.available()>0)
{
c= inputStream.read();
SB.PutChar(c);
ChangDu++;
}
}
catch (IOException e) {}
break;
}
}
public synchronized String ReadPort(int i)
{
while(ChangDu<i)
{
SB.GuanBi();
if(i-ChangDu<5)
{
if(boolean1 == false)
{
ChangDu1 = ChangDu;
boolean1 = true; //停止再次复值
}
ChangDu1++;
if(ChangDu1>i)
{
ChangDu1 = 0;
break;
}
}
if(ChangDu<2)
{
ChangDu2++; //错误垒加
break;
}
}
Msg = SB.GetMsg(ChangDu);
if(Msg==null)
{
Msg = "start";
}
if(ChangDu2>4)
{
Msg = "stop"; //数据清空
ChangDu2 = 0;
}
boolean1 = false;
ChangDu = 0; //数据个数清零
i = 0; //数据个数清零
return Msg;
}
public synchronized void WritePort(String XiaoXi)
{
try
{
outputStream.write(XiaoXi.getBytes());
}catch(IOException e){}
}
public void ClosePort()
{
serialPort.close();
serialPort = null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -