| | |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | import chuankou.SerialPortService; |
| | | import chushihua.SlotManager; |
| | | import chushihua.lunxun; |
| | | import dialog.Charulog; |
| | | import dialog.Dingshidialog; |
| | | import publicway.ProtocolParser01.ParseResult; |
| | | import xitongshezhi.SystemDebugDialog; |
| | | |
| | | public class SerialProtocolParser { |
| | | |
| | |
| | | processorThread.setDaemon(true); |
| | | processorThread.start(); |
| | | |
| | | //System.out.println("串口协议解析器已启动"); |
| | | //System.out.println("串口协议解析器已启动 - 独立于轮询状态运行"); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | public void receiveData(byte[] rawData) { |
| | | if (!isRunning) { |
| | | //System.out.println("警告: 串口协议解析器未启动,忽略接收的数据"); |
| | | return; |
| | | // 如果解析器未运行,自动启动 |
| | | start(); |
| | | } |
| | | |
| | | if (rawData == null || rawData.length == 0) { |
| | | return; |
| | | } |
| | | |
| | | // 将数据添加到批量队列 |
| | | // 将数据添加到批量队列 - 确保始终执行 |
| | | if (!batchQueue.offer(rawData)) { |
| | | System.err.println("批量队列已满,丢弃数据"); |
| | | } |
| | |
| | | * 处理数据包的主方法 |
| | | */ |
| | | private void processPackets() { |
| | | //System.out.println("串口数据包处理线程开始运行"); |
| | | System.out.println("串口数据包处理线程开始运行"); |
| | | |
| | | while (isRunning && !Thread.currentThread().isInterrupted()) { |
| | | try { |
| | | byte[] packet = dataQueue.take(); // 阻塞直到有数据 |
| | | parsePacket(packet); |
| | | } catch (InterruptedException e) { |
| | | //System.out.println("串口数据包处理线程被中断"); |
| | | System.out.println("串口数据包处理线程被中断"); |
| | | Thread.currentThread().interrupt(); |
| | | break; |
| | | } catch (Exception e) { |
| | |
| | | } |
| | | } |
| | | |
| | | //System.out.println("串口数据包处理线程结束运行"); |
| | | System.out.println("串口数据包处理线程结束运行"); |
| | | } |
| | | |
| | | /** |
| | |
| | | byte hostAddress = packet[4]; // 主机地址 |
| | | byte slotAddress = packet[5]; // 卡槽地址 |
| | | byte functionCode = packet[6]; // 功能码 |
| | | |
| | | // 数据长度 (从协议中读取) |
| | | int dataLength = ((packet[2] & 0xFF) << 8) | (packet[3] & 0xFF); |
| | | |
| | |
| | | // 使用优化的字节数组解析方法,避免字符串转换 |
| | | ParseResult rst = ProtocolParser01.parseDDCC01Data(packet); |
| | | rst.fuzhi(); |
| | | // rst.toString(); |
| | | // System.out.println(rst.toString()); |
| | | if (lunxun.DEBUG_ENABLED) { |
| | | SystemDebugDialog.appendAsciiData(rst.toString()); |
| | | } |
| | | |
| | | } |
| | | break; |
| | | case FUNCTION_51: |
| | | // 调用 ProtocolParser51 处理数据 |
| | | String hexPacket = bytesToHex(packet); |
| | | int result = ProtocolParser51.parse(hexPacket); |
| | | int slot = slotAddress; |
| | | if (result == 1) { |
| | | //System.out.println("功能码 0x51 - 开门控制成功"); |
| | | // Dingshidialog.showTimedDialog(null, 5,slot+"号卡槽出卡成功请取走卡..."); |
| | | SlotManager.changgehaska(slot, result); |
| | | } else { |
| | | //System.out.println("功能码 0x51 - 开门控制失败或报文不合法"); |
| | | String message=slot+"号卡槽取卡失败"; |
| | | Charulog.logOperation(message); |
| | | } |
| | | break; |
| | | case FUNCTION_52: |