/// FC=05 写入一个线圈 internal byte MB_WriteCoil(ushort refNum, byte val) { byte reg = (byte)(refNum >> 4); // 寄存器 byte bit = (byte)(0x000F & refNum); // 位 if (!IsRegWritable(reg, bit)) { return MBException.E02_ILLEGAL_DATA_ADDRESS; //无效数据地址, 不可写入. } OnOff switchTo = val == 0xFF ? OnOff.ON : OnOff.OFF; if (reg == 05) { // 区域开关, 更新点表(没有继电器). _ptable.SetValueAt(5, bit, val == 0xFF); return DealZoneRequest(bit, switchTo); } // 找到 Door Door door = _config.FindDoorByAddr(reg, bit); if (door == null) { return MBException.E02_ILLEGAL_DATA_ADDRESS; } if (door.State!=null && !door.State.Controable) { // return MBException.E03_ILLEGAL_DATA_VALUE; //不可控制.(开门未到位或关门未到位) } JDQRequest req = new JDQRequest(JDQRequest.DEV_ID, JDQRequestType.SetOutput); byte[] addOnbits, addOffbits; //要附加操作的位. byte[] bits = GetRelayIndex(reg, out addOnbits, out addOffbits); req.SetOutputs(bits, val == 0xFF); if (addOnbits != null) req.SetOutputs(addOnbits, true); // 附加打开操作. if (addOffbits != null) req.SetOutputs(addOffbits, false); //附加关闭操作 return Request(door, req); }
// 关闭警告指示(红灯, 蜂鸣器) private void CloseAlarmAsNecessary(Door d) { if (d.State.Beep == RelayState.ACTION) { // 蜂鸣器闭合. if ((d.State.DoorAction == RelayState.ACTION && d.State.OpenState == OnOff.ON) || (d.State.DoorAction == RelayState.RESET && d.State.CloseState == OnOff.OFF)) { log.DebugFormat("门 {0} 动作已到位, 自动关闭: 继电器/红灯.", d); // 开门动作, 且已到位. 或 关门动作, 已到位. JDQRequest req = new JDQRequest(JDQRequest.DEV_ID, JDQRequestType.SetOutput); byte[] bits = new byte[] { RELAY_RED, RELAY_BEEP }; //继电器, 红灯, 蜂鸣器) req.SetOutputs(bits, false); _jdqVisitor.Request(d.IpAddr, req); } } }
/// 控制一樘门 public byte ControlDoor(Door d, OnOff to) { if (!IsActionRequired(d, to)) { //log.DebugFormat("无需操作: {0}", d); //return MBException.MB_SUCCESS; } JDQRequest req = new JDQRequest(JDQRequest.DEV_ID, JDQRequestType.SetOutput); byte[] bits = new byte[] { RELAY_OPEN_LEFT, RELAY_OPEN_RIGHT}; //0-2 (继电器, ) req.SetOutputs(bits, to == OnOff.ON); req.SetOutputs(new byte[]{RELAY_RED, RELAY_BEEP }, true ) ; //红灯, 蜂鸣器 return Request(d, req); }