private void timer1_Tick(object sender, EventArgs e) { int col = 1; int row; WSMBS.Result sRes = WSMBS.Result.SUCCESS; WSMBT.Result tRes = WSMBT.Result.SUCCESS; switch (m_nCmdNo) { case 0: if (m_curInterface == 2) { tRes = tcpip.ReadDiscreteInputs((byte)numStation.Value, m_nAddr, m_nLength, sw); } else { sRes = serial.ReadDiscreteInputs((byte)numStation.Value, m_nAddr, m_nLength, sw); } break; case 1: if (m_curInterface == 2) { tRes = tcpip.ReadCoils((byte)numStation.Value, m_nAddr, m_nLength, sw); } else { sRes = serial.ReadCoils((byte)numStation.Value, m_nAddr, m_nLength, sw); } break; case 2: if (m_curInterface == 2) { tRes = tcpip.ReadHoldingRegisters((byte)numStation.Value, m_nAddr, m_nLength, reg); } else { sRes = serial.ReadHoldingRegisters((byte)numStation.Value, m_nAddr, m_nLength, reg); } break; case 3: if (m_curInterface == 2) { tRes = tcpip.ReadInputRegisters((byte)numStation.Value, m_nAddr, m_nLength, reg); } else { sRes = serial.ReadInputRegisters((byte)numStation.Value, m_nAddr, m_nLength, reg); } break; default: break; } nTx++; if (sRes != WSMBS.Result.SUCCESS || tRes != WSMBT.Result.SUCCESS) { picConn.Enabled = false; nFailCounter++; } else { byte[] rxbuf = new byte[256]; int n; if (m_curInterface == 2) { n = tcpip.GetRxBuffer(rxbuf); } else { n = serial.GetRxBuffer(rxbuf); } if (n != 0) { string str = ""; for (int i = 0; i < n; i++) { str += string.Format("{0:X2} ", rxbuf[i]); if (((i + 1) % 5) == 0) { str += "_ "; } } labRecv.Text = str; } for (int i = 0; i < m_nLength; i++) { row = i / 2; gridData.Rows[row].Cells[col].Value = (m_nCmdNo > 1) ? reg[i].ToString(strFmtByte) : sw[i].ToString(); if (col == 1) { col = 3; } else { col = 1; } } if (bExtend == 1) { for (int i = 0; i < ptr_gp; i++) { CurveItem curve = gp.CurveList[i]; curve.AddPoint((double)(timeStart++), (double)reg[agpAdr[i]]); } zd.AxisChange(); zd.Refresh(); } nRx++; nFailCounter = 0; picConn.Enabled = true; } if (nFailCounter >= 3) { picConn.Enabled = false; timer1.Enabled = false; if (m_curInterface == 2) { labRecv.Text = " Modbus总线通信错误,故障原因: " + tRes.ToString(); } else { labRecv.Text = " Modbus总线通信错误,故障原因: " + sRes.ToString(); } labRecv.ForeColor = Color.Red; nFailCounter = 0; } labTx.Text = nTx.ToString(); labRx.Text = nRx.ToString(); }
private void btExec_Click(object sender, EventArgs e) { int col; int row; int no = 0; WSMBS.Result sRes = WSMBS.Result.SUCCESS; WSMBT.Result tRes = WSMBT.Result.SUCCESS; m_nAddr = (ushort)numStartAdr.Value; m_nLength = (ushort)numCount.Value; m_nCmdNo = lstCmd.SelectedIndex; m_perAdr[m_nCmdNo] = m_nAddr; m_Count[m_nCmdNo] = m_nLength; reg = new short[m_nLength]; sw = new bool[m_nLength]; switch (m_nCmdNo) { case 0: if (m_curInterface == 2) { tRes = tcpip.ReadDiscreteInputs((byte)numStation.Value, m_nAddr, m_nLength, sw); } else { sRes = serial.ReadDiscreteInputs((byte)numStation.Value, m_nAddr, m_nLength, sw); } break; case 1: if (m_curInterface == 2) { tRes = tcpip.ReadCoils((byte)numStation.Value, m_nAddr, m_nLength, sw); } else { sRes = serial.ReadCoils((byte)numStation.Value, m_nAddr, m_nLength, sw); } break; case 2: if (m_curInterface == 2) { tRes = tcpip.ReadHoldingRegisters((byte)numStation.Value, m_nAddr, m_nLength, reg); } else { sRes = serial.ReadHoldingRegisters((byte)numStation.Value, m_nAddr, m_nLength, reg); } break; case 3: if (m_curInterface == 2) { tRes = tcpip.ReadInputRegisters((byte)numStation.Value, m_nAddr, m_nLength, reg); } else { sRes = serial.ReadInputRegisters((byte)numStation.Value, m_nAddr, m_nLength, reg); } break; default: break; } byte[] txbuf = new byte[256]; int n; if (m_curInterface == 2) { n = tcpip.GetTxBuffer(txbuf); } else { n = serial.GetTxBuffer(txbuf); } if (n != 0) { string str = "发送帧:"; for (int i = 0; i < n; i++) { str += string.Format("{0:X2} ", txbuf[i]); if ((i + 1) % 5 == 0) { str += "_ "; } } labSend.Text = str; } if (sRes != WSMBS.Result.SUCCESS || tRes != WSMBT.Result.SUCCESS) { if (m_curInterface == 2) { labRecv.Text = "读控制站错误! (" + tRes.ToString() + ")"; } else { labRecv.Text = "读控制站错误! (" + sRes.ToString() + ")"; } labRecv.ForeColor = Color.Red; picConn.Enabled = false; } else { int index = this.gridData.Rows.Add(); //得到当前控件的行数 for (int i = 0; i < index + 1; i++) //删除表格中的所有行 { gridData.Rows.RemoveAt(0); } for (int i = 0; i < m_nLength; i++) { if (i % 2 == 0) { no = this.gridData.Rows.Add(); } int nFind = -1; for (int k = 0; k < lsLen; k++) { if (lsNum[k] == (m_nAddr + i)) { nFind = k; break; } } if (i % 2 == 0) { col = 0; } else { col = 2; } if (ckDispAlais.Checked && nFind != -1) { this.gridData.Rows[no].Cells[col].Value = (m_nAddr + i).ToString() + " " + lsAlais[nFind]; } else { this.gridData.Rows[no].Cells[col].Value = (m_nAddr + i).ToString() + " " + (m_nAddr + i).ToString("X") + "H "; } this.gridData.Rows[no].Cells[col + 1].Value = (m_nCmdNo > 1) ? reg[i].ToString(strFmtByte) : sw[i].ToString(); this.gridData.Rows[no].Cells[col + 1].Style.Alignment = DataGridViewContentAlignment.MiddleRight; if ((i / 10) % 2 == 0) { this.gridData.Rows[no].Cells[col].Style.BackColor = Color.LightGray; this.gridData.Rows[no].Cells[col + 1].Style.BackColor = Color.LightBlue; } } if (bExtend == 1) { bool bLast = false; int address; for (int i = 0; i < ptr_gp; i++) { address = agpStartAdr[i] + agpAdr[i]; if (agpCmdNo[i] == m_nCmdNo && address >= m_nAddr && address < (m_nAddr + m_nLength)) { agpAdr[i] = address - m_nAddr; agpStartAdr[i] = m_nAddr; } else { bLast = true; } } if (bLast) { bExtend = 2; gp.CurveList.Clear(); zd.Refresh(); ptr_gp = 0; timeStart = 0; } } labRecv.ForeColor = Color.Blue; btnSupend.Enabled = true; timer1.Interval = (int)numCycle.Value; timer1.Enabled = true; picConn.Enabled = true; } }