标签:
public class SerialPortHandler implements Runnable, SerialPortEventListener
{
private String appName = "串口通讯";
private final static int timeout = 2000;// open 端口时的等待时间
private final static int dataBits = 8;// 数据位
private final static int stopBits = 1;// 停止位
private final static int parity = 0;// 校验位
private String portName;
private int baudRate;// 波特率
private int threadTime = 0;
private CommPortIdentifier commPort;
private SerialPort serialPort;
private InputStream inputStream;
private OutputStream outputStream;
private static ConcurrentHashMap<String, SerialPortHandler> instances = new ConcurrentHashMap<String, SerialPortHandler>();
private SerialPortHandler()
{
}
public static synchronized SerialPortHandler getInstance(String portName,
int baudRate) throws NoSuchPortException
{
SerialPortHandler instance;
if (!instances.containsKey(portName))
{
instance = new SerialPortHandler();
instance.portName = portName;
instance.baudRate = baudRate;
instances.put(portName, instance);
instance.selectPort();
}
else
{
instance = instances.get(portName);
}
return instance;
}
/**
* @方法名称 :listPort
* @功能描述 :列出所有可用的串口
* @返回值类型 :void
*/
public static void listPort()
{
CommPortIdentifier cpid;
Enumeration en = CommPortIdentifier.getPortIdentifiers();
System.out.println("now to list all Port of this PC:" + en);
while (en.hasMoreElements())
{
cpid = (CommPortIdentifier) en.nextElement();
if (cpid.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
System.out.println(cpid.getName() + ", "
+ cpid.getCurrentOwner());
}
}
}
/**
* @方法名称 :selectPort
* @功能描述 :选择一个端口,比如:COM1
* @返回值类型 :void
* @param portName
* @throws NoSuchPortException
*/
@SuppressWarnings("rawtypes")
private void selectPort() throws NoSuchPortException
{
this.commPort = CommPortIdentifier.getPortIdentifier(portName);
openPort();
}
/**
* @方法名称 :openPort
* @功能描述 :打开SerialPort
* @返回值类型 :void
*/
private void openPort()
{
if (commPort == null)
log(String.format("无法找到名字为‘%1$s‘的串口!", commPort.getName()));
else
{
log("端口选择成功,当前端口:" + commPort.getName() + ",现在实例化 SerialPort:");
try
{
serialPort = (SerialPort) commPort.open(appName, timeout);
serialPort.setSerialPortParams(baudRate, dataBits, stopBits,
parity);
log("实例 SerialPort 成功!");
}
catch (PortInUseException e)
{
throw new RuntimeException(String.format("端口‘%1$s‘正在使用中!",
commPort.getName()));
}
catch (UnsupportedCommOperationException e)
{
throw new RuntimeException(String.format("端口‘%1$s‘操作命令不支持!",
commPort.getName()));
}
}
}
/**
* @方法名称 :checkPort
* @功能描述 :检查端口是否正确连接
* @返回值类型 :void
*/
private void checkPort()
{
if (commPort == null)
throw new RuntimeException("没有选择端口,请使用 "
+ "selectPort(String portName) 方法选择端口");
if (serialPort == null)
{
throw new RuntimeException("SerialPort 对象无效!");
}
}
/**
* @方法名称 :write
* @功能描述 :向端口发送数据,请在调用此方法前 先选择端口,并确定SerialPort正常打开!
* @返回值类型 :void
* @param message
*/
public void write(String message)
{
checkPort();
try
{
outputStream = new BufferedOutputStream(
serialPort.getOutputStream());
}
catch (IOException e)
{
throw new RuntimeException("获取端口的OutputStream出错:" + e.getMessage());
}
try
{
outputStream.write(message.getBytes());
log("信息发送成功!");
}
catch (IOException e)
{
throw new RuntimeException("向端口发送信息时出错:" + e.getMessage());
}
finally
{
try
{
outputStream.close();
}
catch (Exception e)
{
}
}
}} |
public static void sendBySerialPorts(String contant)
{
contant = contant.replaceAll("\\$", ""); // 这里使用$符号作为终止符
contant = contant.concat("$");
SerialPortHandler sph;
try
{
sph = SerialPortHandler.getInstance(
DriveOutConductConfig.getPortName(),
DriveOutConductConfig.getBitRate());
sph.write(contant);
}
catch (NoSuchPortException e)
{
e.printStackTrace();
}
} |
标签:
原文地址:http://www.cnblogs.com/wgp13x/p/4288980.html