public bool ScanOnce() { bool bRet = true; WSMBS.Result sres; WSMBT.Result tres; sres = WSMBS.Result.SUCCESS; tres = WSMBT.Result.SUCCESS; for (int Index = 0; Index < m_scanArea.Count; Index++) { if (m_nProcotol == 2) { //write register if (queue.Count > 0) { WrReg_Coil node = queue.Dequeue(); bool val = (node.val != 0); if (node.cmd == 0) { tres = tcp_svr.WriteSingleCoil((byte)m_nStation, node.adr, val); } if (node.cmd == 2) { tres = tcp_svr.WriteSingleRegister((byte)m_nStation, node.adr, node.val); } } else { //read register switch (m_nFunc) { case 0: tres = tcp_svr.ReadCoils((byte)m_nStation, m_scanArea[Index].start_adr, (ushort)m_scanArea[Index].length, m_bValue[Index]); break; case 1: tres = tcp_svr.ReadDiscreteInputs((byte)m_nStation, m_scanArea[Index].start_adr, (ushort)m_scanArea[Index].length, m_bValue[Index]); break; case 2: tres = tcp_svr.ReadHoldingRegisters((byte)m_nStation, m_scanArea[Index].start_adr, (ushort)m_scanArea[Index].length, m_sValue[Index]); break; case 3: tres = tcp_svr.ReadInputRegisters((byte)m_nStation, m_scanArea[Index].start_adr, (ushort)m_scanArea[Index].length, m_sValue[Index]); break; } } if (tres == WSMBT.Result.SUCCESS) { nSucc++; } else { nFail++; err_msg = tres.ToString(); bRet = false; } } else { //write register if (queue.Count > 0) { WrReg_Coil node = queue.Dequeue(); bool val = (node.val != 0); if (node.cmd == 0) { sres = ser_svr.WriteSingleCoil((byte)m_nStation, node.adr, val); } if (node.cmd == 2) { sres = ser_svr.WriteSingleRegister((byte)m_nStation, node.adr, node.val); } } else { //read register switch (m_nFunc) { case 0: sres = ser_svr.ReadCoils((byte)m_nStation, m_scanArea[Index].start_adr, (ushort)m_scanArea[Index].length, m_bValue[Index]); break; case 1: sres = ser_svr.ReadDiscreteInputs((byte)m_nStation, m_scanArea[Index].start_adr, (ushort)m_scanArea[Index].length, m_bValue[Index]); break; case 2: sres = ser_svr.ReadHoldingRegisters((byte)m_nStation, m_scanArea[Index].start_adr, (ushort)m_scanArea[Index].length, m_sValue[Index]); break; case 3: sres = ser_svr.ReadInputRegisters((byte)m_nStation, m_scanArea[Index].start_adr, (ushort)m_scanArea[Index].length, m_sValue[Index]); break; } if (tres == WSMBT.Result.SUCCESS) { nSucc++; } else { nFail++; err_msg = tres.ToString(); bRet = false; } } } } return(bRet); }
private void _bw_DoWork(object sender, DoWorkEventArgs e) { try { AveragingStack <UInt16> node1DepthAvg = new AveragingStack <ushort>(); AveragingStack <UInt16> node2DepthAvg = new AveragingStack <ushort>(); UInt16 node1Roll = 0; UInt16 node1State = 0; UInt16 node2Roll = 0; UInt16 node2state = 0; var register = new short[_registerSize]; ReadingCount = 0; InStartupMode = true; ReadInProgress = true; for (int i = 0; i <= 5; i++) { _bw.ReportProgress(i); Node1ModResult = _modbus.ReadHoldingRegisters(1, 0, _registerSize, register); node1State = (UInt16)register[_node1StateReg]; if (node1State == 128) { node1DepthAvg.Add((UInt16)register[_node1DepthReg]); node1Roll = (UInt16)register[_node1RollReg]; } else { node1Roll = 10; } node2state = (UInt16)register[_node2StateReg]; if (node2state == 128) { node2DepthAvg.Add((UInt16)register[_node2DepthReg]); node2Roll = (UInt16)register[_node2RollReg]; } else { node2Roll = 10; } System.Threading.Thread.Sleep(_readInterval * 1000); } Double[] readings = { node1DepthAvg.Average, node1Roll, node1State, node2DepthAvg.Average, node2Roll, node2state }; ProcessReadings(readings); ReadInProgress = false; } catch (Exception ex) { ReadInProgress = false; throw ex; } }