/* ================================================================== * PlcProtocol.cs -- Ai8051U-32Bit 简易 PLC 上位机协议层 * * 职责:CRC8、规则打包下发、状态读回、事件/心跳解析 * 依赖:HidDevice(底层 USB 访问) * * 与 MCU 协议严格对齐(每包 64 字节): * 0x01 开始下发 [1]=总条数 * 0x02 规则数据 [1]=包序号,[2..61]=最多5条规则,[62]=CRC8([0..61]) * 方案乙:不等应答,MCU 静默接收 * 0x03 下发结束 [1]=总条数,[2]=12×N 字节累加和低8位,[62]=CRC8([0..61]) * MCU 回 0xA5=生效 / 0x5A=失败 * 0x04 读回状态 MCU 回 0xA4 + 状态 * 0x05 输出全复位 * 0x06 停止所有计时 * * 单条规则 12 字节: * +0 src_type 0=数字,1=模拟 * +1 src_ch 数字0~7=P1.x;模拟8~14=P0.0~P0.6 * +2 trig_mode 0=上升(低变高),1=下降(高变低),2=任意变化 * +3 th_low_L / +4 th_low_H 模拟下限 mV * +5 th_high_L / +6 th_high_H 模拟上限 mV * +7 out_ch 0~7=P2.x,8~14=P4.x,15=P3.5 * +8 act_level 0=置低,1=置高 * +9 delay_L / +10 delay_H 延时值 * +11 time_unit 0=ms,1=秒,2=分钟 * * 上报包: * 0x81 事件 [1]=类型(1数字/2输出/3模拟进区间/4模拟出区间) * [2]=通道 [3..4]=数值(低字节在前) [63]=CRC8 * 0x82 心跳 [1..2]=SysTickMs低16位 [63]=CRC8 * ================================================================== */ using System; using System.Collections.Generic; namespace Ai8051U_PLC { public class PlcProtocol { public const int RULE_MAX = 32; public const int RULE_LEN = 12; public const int RULE_PER_PKT = 5; private HidDevice m_hid; /* 状态回传解析结果 */ public byte StRuleNum; public byte StRecvCnt; public byte StP1In; public byte StP2Out; public byte StP4Out; public bool StP35Out; public uint StPending; public ushort[] StAdcMv = new ushort[7]; public ushort StAvcc; public PlcProtocol(HidDevice hid) { m_hid = hid; } /* ---------- CRC8:多项式 0x07,初值 0,不反转 ---------- */ public static byte Crc8(byte[] p, int offset, int len) { byte crc; int i; int j; crc = 0x00; for (i = 0; i < len; i++) { crc ^= p[offset + i]; for (j = 0; j < 8; j++) { if ((crc & 0x80) != 0) crc = (byte)((crc << 1) ^ 0x07); else crc = (byte)(crc << 1); } } return crc; } /* ---------- 发送并等待指定首字节应答 ---------- */ private byte[] SendWait(byte[] pkt, byte expectHead, int timeoutMs) { byte[] rsp; int elapsed; if (!m_hid.Write(pkt)) return null; elapsed = 0; while (elapsed < timeoutMs) { rsp = m_hid.Read(50); if (rsp != null) { /* 跳过事件包/心跳包,只取命令应答 */ if (rsp[0] == 0x81 || rsp[0] == 0x82) continue; if (Crc8(rsp, 0, 63) != rsp[63]) continue; /* 应答自身被干扰,丢弃 */ if (rsp[0] == expectHead) return rsp; } elapsed += 50; } return null; } /* ============================================================== * 下发整张规则表 * rules:已合并、已按行号升序的连续字节(N×12) * 返回:true=MCU 回 0xA5 生效 * ============================================================== */ public bool DownloadRules(byte[] rules, int ruleCount) { byte[] pkt; byte[] rsp; int pktNo; int i; int start; int cnt; byte ck; /* ---- 1. 0x01 开始下发(MCU 不回应答) ---- */ pkt = new byte[64]; pkt[0] = 0x01; pkt[1] = (byte)ruleCount; if (!m_hid.Write(pkt)) return false; /* ---- 2. 0x02 逐包发送(方案乙:不等应答) ---- */ pktNo = 0; while (pktNo * RULE_PER_PKT < ruleCount) { start = pktNo * RULE_PER_PKT * RULE_LEN; cnt = Math.Min(RULE_PER_PKT, ruleCount - pktNo * RULE_PER_PKT); pkt = new byte[64]; pkt[0] = 0x02; pkt[1] = (byte)pktNo; Array.Copy(rules, start, pkt, 2, cnt * RULE_LEN); pkt[62] = Crc8(pkt, 0, 62); /* 包级 CRC8 */ if (!m_hid.Write(pkt)) return false; pktNo++; } /* ---- 3. 0x03 下发结束:12×N 累加和 ---- */ ck = 0; for (i = 0; i < ruleCount * RULE_LEN; i++) ck = (byte)(ck + rules[i]); /* 低8位自然回绕 */ pkt = new byte[64]; pkt[0] = 0x03; pkt[1] = (byte)ruleCount; pkt[2] = ck; pkt[62] = Crc8(pkt, 0, 62); /* MCU 的 0x03 分支不校验此字节,填上无妨 */ rsp = SendWait(pkt, 0xA5, 1500); if (rsp == null) { /* 再等一次 0x5A(失败应答) */ rsp = SendWait(pkt, 0x5A, 500); return false; } return true; } /* ============================================================== * 读回状态,成功返回 true 并填充 St* 字段 * ============================================================== */ public bool ReadStatus() { byte[] pkt; byte[] rsp; int i; pkt = new byte[64]; pkt[0] = 0x04; pkt[62] = Crc8(pkt, 0, 62); rsp = SendWait(pkt, 0xA4, 500); if (rsp == null) return false; StRuleNum = rsp[1]; StRecvCnt = rsp[2]; StP1In = rsp[3]; StP2Out = rsp[4]; StP4Out = rsp[5]; StP35Out = (rsp[6] & 0x01) != 0; StPending = (uint)(rsp[7] | (rsp[8] << 8) | (rsp[9] << 16) | (rsp[10] << 24)); for (i = 0; i < 7; i++) StAdcMv[i] = (ushort)(rsp[11 + i * 2] | (rsp[12 + i * 2] << 8)); StAvcc = (ushort)(rsp[25] | (rsp[26] << 8)); return true; } /* ============================================================== * 输出全复位 / 停止计时(无应答) * ============================================================== */ public void OutputResetAll() { byte[] pkt = new byte[64]; pkt[0] = 0x05; pkt[62] = Crc8(pkt, 0, 62); m_hid.Write(pkt); } public void StopAllTimer() { byte[] pkt = new byte[64]; pkt[0] = 0x06; pkt[62] = Crc8(pkt, 0, 62); m_hid.Write(pkt); } /* ============================================================== * 解析一包上报(供 Form1 轮询调用) * 返回 false = 不是有效上报包 * ============================================================== */ public bool ParseReport(byte[] r, out int evtType, out int ch, out int val) { evtType = 0; ch = 0; val = 0; if (r == null) return false; if (r[0] != 0x81 && r[0] != 0x82) return false; if (Crc8(r, 0, 63) != r[63]) return false; evtType = r[1]; ch = r[2]; val = r[3] | (r[4] << 8); return true; } } }