private void But_Clear_Click(object sender, EventArgs e) { Device_List.Clear(); lbReceivedCount.Text = "0"; lbSendCount.Text = "0"; frame_data = ""; }
private void Btn_Delete_Click(object sender, EventArgs e) { for (var i = 0; i < Device_List.Items.Count; i++) { if (Device_List.GetItemChecked(i)) { setting.DeleteDevice(Device_List.Items[i].ToString()); Device_List.Items.RemoveAt(i); i--; } } }
public void refresh_page() { Device_List.DataSource = DetectDevice(); Device_List.DataTextField = "DeviceNameField"; Device_List.DataValueField = "DeviceIDField"; Device_List.DataBind(); try { if (mDeviceId.Length != 0) { Device_List.SelectedIndex = Device_List.Items.IndexOf(Device_List.Items.FindByValue(mDeviceId)); select_device(); } }catch { Device_List.SelectedIndex = 0; } }
void Protocol_Resolution() { while (true) { if (ins_struct.ins_length >= 8) //指令队列中存在未处理的指令 { //1、寻找SOH if (CDMP_SOH == ins_struct.ins_buf[ins_struct.ins_current])//非SOH || 地址错误 { int Frame_Length; int L_end; Console.WriteLine($"ins_struct.ins_current = {ins_struct.ins_current}"); L_end = ins_struct.ins_buf.Length - ins_struct.ins_current; if (L_end > 2) { Frame_Length = ins_struct.ins_buf[ins_struct.ins_current + 2]; } else { Frame_Length = ins_struct.ins_buf[2 - L_end]; } if (Frame_Length > ins_struct.ins_length) { right_shift_current(1); } else //将数据移动出来做进一步的检查 { UInt16 crc16; byte[] P_frame = new byte[Frame_Length]; if (Frame_Length <= L_end) { Array.Copy(ins_struct.ins_buf, ins_struct.ins_current, P_frame, 0, Frame_Length); } else { Array.Copy(ins_struct.ins_buf, ins_struct.ins_current, P_frame, 0, L_end); Array.Copy(ins_struct.ins_buf, 0, P_frame, L_end, Frame_Length - L_end); } crc16 = CRC16_MODBUS(P_frame, Frame_Length - 2); Console.WriteLine($"crc16 = {crc16}"); if ((crc16 == ((P_frame[Frame_Length - 2] << 8) | (P_frame[Frame_Length - 1])))) { frame_data = "原始数据:"; foreach (byte b in P_frame) { frame_data += b.ToString("X2"); frame_data += " "; } frame_data += "\r\n"; frame_data += "数据说明:"; switch ((CMD_TYPE)P_frame[5]) { case CMD_TYPE.READ_SINGLE: frame_data += $"设备地址:{P_frame[3].ToString("x2")} "; foreach (Device_info d_inf in device_list) { if (d_inf.Device_Addr == P_frame[3]) { frame_data += $"设备类型:{d_inf.Device_str} "; break; } } frame_data += $"读单个寄存器:{P_frame[6].ToString("x2")}的值为{P_frame[7]}\r\n"; break; case CMD_TYPE.READ_MUL: Console.WriteLine("read some data\n"); break; case CMD_TYPE.WRITE_SINGLE: frame_data += $"设备地址:{P_frame[3].ToString("x2")} "; foreach (Device_info d_inf in device_list) { if (d_inf.Device_Addr == P_frame[3]) { frame_data += $"设备类型:{d_inf.Device_str} "; break; } } frame_data += $"写单个寄存器:{P_frame[6].ToString("x2")}\r\n"; break; break; case CMD_TYPE.WRITE_MUL: Console.WriteLine("respond data\n"); break; case CMD_TYPE.UPDATE_DATE: Console.WriteLine("update date\n"); break; case CMD_TYPE.INVENTORY: //盘点设备 //Device_List.Text += "盘点设备:\r\n"; frame_data += "设备地址:"; frame_data += ("0x" + P_frame[3].ToString("x2")) + " "; Device_info device_Info; frame_data += "设备类型:"; switch ((DEVICE_TYPE)P_frame[6]) { case DEVICE_TYPE.DEV_BOOT: device_Info.Device_str = "Boot Loader"; frame_data += "Boot Loader\r\n"; break; case DEVICE_TYPE.DEV_FAN_COIL: device_Info.Device_str = "风机盘管"; frame_data += "风机盘管\r\n"; break; case DEVICE_TYPE.DEV_FLOOR_HEAT: device_Info.Device_str = "地暖"; frame_data += "地暖\r\n"; break; case DEVICE_TYPE.DEV_FRESH: device_Info.Device_str = "新风"; frame_data += "新风\r\n"; break; case DEVICE_TYPE.DEV_HUMID: device_Info.Device_str = "加湿机"; frame_data += "加湿机\r\n"; break; case DEVICE_TYPE.DEV_DEHUMID: device_Info.Device_str = "除湿机"; frame_data += "除湿机\r\n"; break; default: device_Info.Device_str = "未定义设备"; frame_data += "未定义设备\r\n"; break; } device_Info.Device_Addr = P_frame[3]; device_Info.DEVICE_TYPE = (DEVICE_TYPE)P_frame[6]; device_list.Add(device_Info); Console.WriteLine($"pack = {pack}"); pack++; break; default: Console.WriteLine("invalid cmd\n"); break; } this.Invoke(new MethodInvoker(() => { Device_List.AppendText(frame_data); Device_List.ScrollToCaret(); })); right_shift_current(Frame_Length); } else { right_shift_current(1); } } } else //向后移动 { right_shift_current(1); } } } }