码迷,mamicode.com
首页 > Windows程序 > 详细

串口API及数据流的传输

时间:2018-01-16 00:56:19      阅读:815      评论:0      收藏:0      [点我收藏+]

标签:utils   too   print   监听器   operation   detail   over   输入   update   

参考网站:http://blog.csdn.net/update_java/article/details/46898937

直接上参考代码,比较直接,需要用到阻塞队列,否则会出现流读取不完整的情况,看简介的如上网站:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = AmsApplication.class)
@WebAppConfiguration
@Transactional
@Component
public class SerialPortInitializer extends Thread implements ApplicationListener<ContextRefreshedEvent>, SerialPortEventListener {
// 监听器,我的理解是独立开辟一个线程监听串口数据
private CommPortIdentifier portId; // 串口通信管理类
private Enumeration<?> portList; // 有效连接上的端口的枚举
private InputStream inputStream; // 从串口来的输入流
private OutputStream outputStream;// 向串口输出的流
private SerialPort serialPort; // 串口的引用
// 堵塞队列用来存放读到的数据
private BlockingQueue<String> msgQueue = new LinkedBlockingQueue<String>();

private Logger log = LoggerFactory.getLogger(getClass());

@Value("${ams.coredata.SerialPort:false}")
private boolean initialized;

@Autowired
private CorePublicAccountService corePublicAccountService;

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
log.info("ams.coredata.SerialPort:" + initialized);
if (this.initialized) {
runSerialPort();
this.initialized = false;
}
}

private void runSerialPort() {
int i = this.startComPort();
if (i == 1) {
// 启动线程来处理收到的数据
this.start();
try {
String st = "哈哈----你好";
System.out.println("发出字节数:" + st.getBytes("gbk").length);
outputStream.write(st.getBytes("gbk"), 0, st.getBytes("gbk").length);
} catch (IOException e) {
e.printStackTrace();
}
} else {
return;
}
}

/**
*
* 通过程序打开COM4串口,设置监听器以及相关的参数
*
* @return 返回1 表示端口打开成功,返回 0表示端口打开失败
*/
public int startComPort() {
// 通过串口通信管理类获得当前连接上的串口列表
portList = CommPortIdentifier.getPortIdentifiers();

while (portList.hasMoreElements()) {

// 获取相应串口对象
portId = (CommPortIdentifier) portList.nextElement();

System.out.println("设备类型:--->" + portId.getPortType());
System.out.println("设备名称:---->" + portId.getName());

log.info("设备类型:--->" + portId.getPortType());
log.info("设备名称:---->" + portId.getName());

// 判断端口类型是否为串口
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
// 判断如果COM3串口存在,就打开该串口
try {
// 打开串口名字为COM3,延迟为2毫秒
serialPort = (SerialPort) portId.open(portId.getName(), 2000);

} catch (PortInUseException e) {
e.printStackTrace();
return 0;
}
// 设置当前串口的输入输出流
try {
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
return 0;
}
// 给当前串口添加一个监听器
try {

serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
e.printStackTrace();
return 0;
}
// 设置监听器生效,即:当有数据时通知
serialPort.notifyOnDataAvailable(true);

// 设置串口的一些读写参数
try {
// 比特率、数据位、停止位、奇偶校验位
// serialPort.setInputBufferSize(MAX_PRIORITY);
serialPort.setSerialPortParams(115200,
SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
e.printStackTrace();
return 0;
}

return 1;
}
}
return 0;
}

@Override
public void run() {
try {
System.out.println("--------------任务处理线程运行了--------------");
while (true) {
// 如果堵塞队列中存在数据就将其输出
if (msgQueue.size() > 0) {
System.out.println(msgQueue.take());
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}

@Override
public void serialEvent(SerialPortEvent event) {
AccountSyncFieldsDTO accountSyncFieldsDTO = new AccountSyncFieldsDTO();
CorePublicAccountDTO corePublicAccountDTO = new CorePublicAccountDTO();

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[1024];
int numBytes = -1;
ByteArrayOutputStream result = new ByteArrayOutputStream();

try {
while (inputStream.available() > 0) {
numBytes = inputStream.read(readBuffer);
if (numBytes >= 0) {
msgQueue.add(new Date() + "真实收到的数据为:-----"
+ new String(readBuffer));
result.write(readBuffer, 0, numBytes);
readBuffer = new byte[1024];
} else {
msgQueue.add("额------没有读到数据");
}

}

String out = new String(result.toByteArray());
System.out.println("zip:" + out);
String unCompressStr = GZIPUtils.unCompress(out);
System.out.println("unzip:" + unCompressStr);
JSONObject jsonObject = JSONObject.parseObject(unCompressStr);

accountSyncFieldsDTO= (AccountSyncFieldsDTO)JSONObject.toJavaObject(jsonObject,AccountSyncFieldsDTO.class);
BeanUtils.copyProperties(accountSyncFieldsDTO, corePublicAccountDTO);
setProperties(accountSyncFieldsDTO, corePublicAccountDTO);

corePublicAccountService.save(Long.parseLong(corePublicAccountDTO.getCreatedBy()),
corePublicAccountDTO);
System.out.println("保存成功");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

 

 

package com.idea.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class GZIPUtils {
/**
* 字符串的压缩
*
* @param str
* 待压缩的字符串
* @return 返回压缩后的字符串
* @throws IOException
*/
public static String compress(String str) throws IOException {
if (null == str || str.length() <= 0) {
return str;
}
// 创建一个新的 byte 数组输出流
ByteArrayOutputStream out = new ByteArrayOutputStream();
// 使用默认缓冲区大小创建新的输出流
GZIPOutputStream gzip = new GZIPOutputStream(out);
// 将 b.length 个字节写入此输出流
gzip.write(str.getBytes());
gzip.close();
// 使用指定的 charsetName,通过解码字节将缓冲区内容转换为字符串
return out.toString("ISO-8859-1");
}

/**
* 字符串的解压
*
* @param str
* 对字符串解压
* @return 返回解压缩后的字符串
* @throws IOException
*/
public static String unCompress(String str) throws IOException {
if (null == str || str.length() <= 0) {
return str;
}
// 创建一个新的 byte 数组输出流
ByteArrayOutputStream out = new ByteArrayOutputStream();
// 创建一个 ByteArrayInputStream,使用 buf 作为其缓冲区数组
ByteArrayInputStream in = new ByteArrayInputStream(str
.getBytes("ISO-8859-1"));
// 使用默认缓冲区大小创建新的输入流
GZIPInputStream gzip = new GZIPInputStream(in);
byte[] buffer = new byte[256];
int n = 0;
while ((n = gzip.read(buffer)) >= 0) {// 将未压缩数据读入字节数组
// 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此 byte数组输出流
out.write(buffer, 0, n);
}
// 使用指定的 charsetName,通过解码字节将缓冲区内容转换为字符串
return out.toString("UTF-8");
}

}

串口API及数据流的传输

标签:utils   too   print   监听器   operation   detail   over   输入   update   

原文地址:https://www.cnblogs.com/renjiaqi/p/8290376.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!