/// <summary> /// 发送请求 /// </summary> /// <param name="ip">目标设备</param> /// <param name="port">端口</param> /// <param name="req"></param> /// <param name="timeout"></param> /// <returns></returns> public JDQResponse Request(string ip, JDQRequest req, ushort port = 50000, uint timeout = 2000) { lock (this) { SocketClient conn = TryConnect(ip, port); JDQResponse resp; if (conn != null && conn.IsConnected) { SocketError error; byte[] recv = conn.SSend(req.Encode(), out error); resp = new JDQResponse(req.Type,recv); resp.Error = error; resp.ExtError = error.ToString(); } else { // ERROR response log.ErrorFormat("Connection null or Not Ready."); resp = new JDQResponse(req.Type, new byte[0]); resp.Error = SocketError.NotConnected; resp.ExtError = "连接未就绪"; } return resp; } }
public void TestEncodeReadInput() { JDQRequest req = new JDQRequest(1, JDQRequestType.ReadInput); byte[] buff = req.Encode(); Console.WriteLine("buff is: {0}", ValueHelper.BytesToHexStr(buff)); Assert.AreEqual(9, buff.Length); Assert.AreEqual(0xC0, buff[2]); Assert.AreEqual(0xCE, buff[7]); Assert.AreEqual(0x9C, buff[8]); }
private void OnTimerCallback(object state) { if (_running) { lock (this) { _zone.OpenCnt = 0; _zone.ErrorCnt = 0; _zone.CloseCnt = 0; foreach (Door d in _zone.Doors) { if (d.Enabled) { // Connection c = _doorConnections[d]; JDQRequest req1 = new JDQRequest(d.DevId, JDQRequestType.ReadInput); JDQResponse rsp1 = _visitor.Request(d.IpAddr, req1, DEV_SCAN_PORT, DEV_SCAN_TIMEOUT); Thread.Sleep(10); JDQRequest req2 = new JDQRequest(d.DevId, JDQRequestType.ReadOutput); JDQResponse rsp2 = _visitor.Request(d.IpAddr, req2, DEV_SCAN_PORT, DEV_SCAN_TIMEOUT); // 更新门状态 UpdateDoorState(d, rsp1, rsp2); if (DoorError.Success != d.State.Error) { _zone.ErrorCnt++; //错误. } else { if (d.State.IsOpened) { _zone.OpenCnt++; } else if (d.State.IsClosed) { _zone.CloseCnt++; } } if (OnDoorStateChanged != null) { OnDoorStateChanged.Invoke(_zone, d); // 门变更. } } } UpdateZoneState(_zone); // 更新 Zone 状态. if (OnZoneStateChanged != null) { OnZoneStateChanged.Invoke(_zone, false); // 区域变更. } } } }
public void TestFastReadOutput() { JDQVisitor visitor = new JDQVisitor(); JDQRequest req = new JDQRequest(1, JDQRequestType.ReadOutput); string ip = "192.168.0.18"; // turn off JDQResponse resp; for (int i = 0; i < 100; i++) { resp = visitor.Request(ip, req); } visitor.Terminate(); }
public void TestEncodeWriteOutput() { JDQRequest req = new JDQRequest(1, JDQRequestType.SetOutput); req.TurnOnOutput(4); req.TurnOnOutput(5); for (byte i = 0; i < 16; i++) { req.TurnOnOutput(i); } byte[] buff = req.Encode(); Console.WriteLine("buff is: {0}", ValueHelper.BytesToHexStr(buff)); Assert.AreEqual(10, buff.Length); Assert.AreEqual(0xCC, buff[0]); Assert.AreEqual(0x9E, buff[8]); Assert.AreEqual(0x3C, buff[9]); }
public void TestFastSetOutput() { JDQVisitor visitor = new JDQVisitor(); JDQRequest req = new JDQRequest(1, JDQRequestType.SetOutput); string ip = "192.168.0.18"; for (int i = 0; i < 20; i++) { // turn on all req.TurnOnOutput(JDQRequest.ALL); visitor.Request(ip, req); // turn off all req.TurnOffOutput(JDQRequest.ALL); visitor.Request(ip, req); } visitor.Terminate(); }
private void _OnZoneStateChanged(Zone _z, bool allOn) { bool isTotalOpen = UpdateFunctionTableByZone(_z); // 设置 中控 输出. lock (_ccs) { try { JDQRequest req1 = new JDQRequest(1, JDQRequestType.SetOutput); // Read FAS/IBP req1.SetOutput(_z.Reg.ZoneCoil, _z.IsZoneOpen()); req1.SetOutput(6, isTotalOpen); // 全区域. SocketError err; byte[] recv = _ccs.SSend(req1.Encode(), out err, 500); } catch (Exception) { } } if (this.OnZoneStateChanged != null) { OnZoneStateChanged.Invoke(_z, isTotalOpen); // 发出通知. } }
private byte Request(Door d, JDQRequest req) { JDQResponse resp = _jdqVisitor.Request(d.IpAddr, req); if (resp.IsOK) { return MBException.MB_SUCCESS; } else { return MBException.E0B_GATEWAY_TARGET_DEVICE_FAILED_TO_RESPOND; // 设置异常. } }
private void OnTimerCallback(object state) { if (_running) { if (_ccs == null) { return; } lock (_ccs) { try { JDQRequest req1 = new JDQRequest(1, JDQRequestType.ReadInput); // Read FAS/IBP SocketError err; byte[] recv = _ccs.SSend(req1.Encode(), out err); OnOff ibp, fas; if (SocketError.Success == err) { // IN1 IBP触发信号 信号有效:IBP被触发 // IN2 FAS触发信号 信号有效:FAS被触发 JDQResponse resp = new JDQResponse(JDQRequestType.ReadInput, recv); if (resp.GetLength() == 0) return; ibp = resp.GetInputState(0); fas = resp.GetInputState(1); } else { ibp = OnOff.UNKNOWN; fas = OnOff.UNKNOWN; } TryNotifyIpbFas(err, ibp, fas); Thread.Sleep(10); } catch (Exception) { } } } }
// 关闭警告指示(红灯, 蜂鸣器) 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); } } }
/// 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); }
/// 控制一樘门 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); }
public void TestReadOutput() { JDQVisitor visitor = new JDQVisitor(); JDQRequest req = new JDQRequest(1, JDQRequestType.SetOutput); string ip = "192.168.0.18"; // turn off req.TurnOffOutput(JDQRequest.ALL); req.TurnOnOutput(0); req.TurnOnOutput(4); JDQResponse resp = visitor.Request(ip, req); Assert.IsTrue(resp.IsOK); //SetOutput OK JDQRequest ro = new JDQRequest(1, JDQRequestType.ReadOutput); resp = visitor.Request(ip, ro); Assert.IsTrue(resp.IsOK); // ReadOutputOK Assert.AreEqual(RelayState.ACTION, resp.GetRelayState(0)); Assert.AreEqual(RelayState.RESET, resp.GetRelayState(1)); Assert.AreEqual(RelayState.RESET, resp.GetRelayState(2)); Assert.AreEqual(RelayState.RESET, resp.GetRelayState(3)); Assert.AreEqual(RelayState.ACTION, resp.GetRelayState(4)); Assert.AreEqual(RelayState.RESET, resp.GetRelayState(5)); Assert.AreEqual(RelayState.RESET, resp.GetRelayState(6)); Assert.AreEqual(RelayState.RESET, resp.GetRelayState(7)); visitor.Terminate(); }
public void TestSetOutput() { JDQVisitor visitor = new JDQVisitor(); JDQRequest req = new JDQRequest(1, JDQRequestType.SetOutput); string ip = "192.168.0.18"; // turn off req.TurnOnOutput(JDQRequest.ALL); JDQResponse resp = visitor.Request(ip, req); Assert.IsTrue(resp.IsOK); Thread.Sleep(2000); // turn off req.TurnOffOutput(JDQRequest.ALL); resp = visitor.Request(ip, req); Assert.IsTrue(resp.IsOK); Thread.Sleep(2000); // turn on 1,3,5 req.TurnOnOutput(0); req.TurnOnOutput(2); req.TurnOnOutput(4); resp = visitor.Request(ip, req); Assert.IsTrue(resp.IsOK); Thread.Sleep(2000); // turn off 3 req.TurnOffOutput(2); resp = visitor.Request(ip, req); Assert.IsTrue(resp.IsOK); }