void modbus_ReceivedData(string str) { rtbResult.Text += str + "\n"; rtbResult.Select(rtbResult.TextLength, 0); //跳到末尾 rtbResult.ScrollToCaret(); if (str == "CRC_Error") { return; } int start = int.Parse(txtStart.Text); int valueLen = Convert.ToInt32(str.Substring(4, 2), 16); Dictionary <string, string> dicValue = new Dictionary <string, string>(); switch (str.Substring(2, 2)) { case "01": case "02": for (int i = 0; i < valueLen; i++) { byte dataByte = Convert.ToByte(str.Substring(6 + i * 2, 2), 16); for (int j = 0; j < 8; j++) { if (i * 8 + j < int.Parse(txtCount.Text)) { dicValue.Add(string.Format("{0:D2}", (i * 8 + j + start - 1) / 8) + ":" + (j + start - 1) % 8, "[" + (CommonTools.GetBit(dataByte, j) ? "ON" : "OFF") + "]"); } } } break; case "03": case "04": for (int i = 0; i < valueLen / 2; i++) { dicValue.Add(string.Format("{0:D4}", i + start), "[" + str.Substring(6 + i * 4, 4) + "]"); } break; } showValue(dicValue); }
private void timer1_Tick(object sender, EventArgs e) { txtSendStr.Text = CommonTools.ToHexString(modbus.GetFromPLC(Convert.ToByte(((ComboxItem)(cmbFunction.SelectedItem)).Value), int.Parse(txtAddress.Text, NumberStyles.HexNumber), int.Parse(txtStart.Text, NumberStyles.HexNumber), int.Parse(txtCount.Text))); }