private void btn_SettingAutoList_add_Click(object sender, EventArgs e) { string str_data = txt_SettingAutoList_add.Text; char[] char_send = str_data.ToCharArray(); byte[] data_payload = new byte[char_send.Length / 2]; if (char_send.Length % 2 != 0 || str_data.Length != 12) { txt_log.Text += "ERROR_ADD" + Environment.NewLine; return; } str_data = ""; for (int i = 0; i < (char_send.Length / 2); i++) { string str = char_send[2 * i].ToString() + char_send[2 * i + 1].ToString(); try { data_payload[i] = Convert.ToByte(str, 16); } catch (Exception ex) { txt_log.Text += "ERROR_ADD" + Environment.NewLine; return; } str_data += String.Format("{0:X2}", data_payload[i]); if (i != 5) { str_data += ":"; } } device_info info = new device_info(); info.peer = data_payload; class_MP_dongle.Flag_is_change_device_show = true; class_MP_dongle.ble_device_mac.Add(str_data); info.str_device_name = "Add"; info.str_device_mac = str_data; class_MP_dongle.Set_Auto_connect_list_info.Add(info); }
public string serial_Receive(byte[] rx_data, string time) { string log = ""; string str = ""; device_info info = new device_info(); Flag_show_log = true; try { if (rx_data[0] == (byte)CMD.RX_STX && rx_data[rx_data.Length - 1] == (byte)CMD.RX_ETX && check_crc(rx_data, rx_data.Length - 2) ) { Flag_is_change_device_show = false; switch (rx_data[2]) { case (byte)CMD.Scan_Start: log += "Scan start" + Environment.NewLine; ble_device_mac.Clear(); ble_device.Clear(); scan_device_info.Clear(); //Flag_is_change_device_show = true; //Flag_is_change_device_connect_show = true; break; case (byte)CMD.AVD_Report: Flag_show_log = false; string str_deviec_name = ""; info.peer = new byte[6]; for (int i = 0; i < 6; i++) { str += String.Format("{0:X2}", rx_data[i + 3]); if (i != 5) { str += ":"; } info.peer[i] = rx_data[i + 3]; } for (int i = 0; i < rx_data[1] - 6 - 1 - 1; i++) { if (rx_data[i + 9] != 0x00) { str_deviec_name += Convert.ToChar(rx_data[i + 9]); } } info.str_device_name = str_deviec_name; info.str_device_mac = str; if (Flag_set_auto_list_scan == false) { Flag_is_change_device_show = false; for (int i = 0; i < ble_device_mac.Count; i++) { if (ble_device_mac[i] == str) { Flag_is_change_device_show = true; int rssi = rx_data[rx_data[1] + 2]; rssi = rssi % 127; ble_device[i] = str_deviec_name + " " + str + "(-" + (rssi).ToString() + "dBm)"; scan_device_info[i] = info; } } if (Flag_is_change_device_show == false) { Flag_is_change_device_show = true; ble_device_mac.Add(str); int rssi = rx_data[rx_data[1] + 2]; rssi = rssi % 127; ble_device.Add(str_deviec_name + " " + str + "(-" + (rssi).ToString() + "dBm)"); scan_device_info.Add(info); } } else { Flag_is_change_device_show = false; for (int i = 0; i < Set_Auto_connect_list_info.Count; i++) { if (Set_Auto_connect_list_info[i].str_device_mac == str) { Flag_is_change_device_show = true; } } if (Flag_is_change_device_show == false) { Flag_is_change_device_show = true; ble_device_mac.Add(str); info.str_device_name = "Scan"; Set_Auto_connect_list_info.Add(info); } } break; case (byte)CMD.Connect: info.peer = new byte[6]; for (int i = 0; i < 6; i++) { info.peer[i] = rx_data[i + 3]; str += String.Format("{0:X2}", rx_data[i + 3]); if (i != 5) { str += ":"; } } for (int i = 0; i < scan_device_info.Count; i++) { if (scan_device_info[i].str_device_mac == str) { info = scan_device_info[i]; } } info.last_number = 0; info.error_counter = 0; info.catch_counter = 0; info.str_device_mac = str; //ble_device_connect.Add(info.str_device_name + " " + info.str_device_mac); for (int i = 0; i < scan_device_info.Count; i++) { if (scan_device_info[i].str_device_mac == str) { log += "Connect:" + str + Environment.NewLine; Flag_is_change_device_connect_show = true; Flag_is_connect_success = true; break; } } connected_device_info.Add(info); log += "Connect:" + str + Environment.NewLine; Flag_is_change_device_connect_show = true; Flag_is_connect_success = true; break; case (byte)CMD.Disconnect: for (int i = 0; i < 6; i++) { str += String.Format("{0:X2}", rx_data[i + 3]); if (i != 5) { str += ":"; } } for (int i = 0; i < connected_device_info.Count; i++) { if (str == connected_device_info[i].str_device_mac) { connected_device_info.RemoveAt(i); break; } } Flag_is_change_device_connect_show = true; log += "Disconnect" + Environment.NewLine; break; case (byte)CMD.BLE_RX: Flag_show_log = false; payload payload_tmp = new payload(); string str_detail = ""; int number = 0; int index = 0; string str_ASCII = ""; for (int i = 0; i < 6; i++) { str += String.Format("{0:X2}", rx_data[i + 3]); if (i != 5) { str += ":"; } } for (int i = 0; i < connected_device_info.Count; i++) { if (connected_device_info[i].str_device_mac == str) { if (connected_device_info[i].str_device_name != null) { str = connected_device_info[i].str_device_name + "_" + str; } else { str = "n_a" + "_" + str; } index = i; } } for (int i = 0; i < rx_data[1] - 7; i++) { str_detail += String.Format("{0:X2}", rx_data[i + 9]) + " "; str_ASCII += (char)rx_data[i + 9]; } //try //{ // number = Int32.Parse(str_ASCII); // int buffer = number - connected_device_info[index].last_number; // if (number == 0 && connected_device_info[index].last_number == 65535) // { // //nothing // } // else if (buffer != 1) // { // if(buffer > 0) // connected_device_info[index].error_counter += buffer-1; // else // connected_device_info[index].error_counter ++; // } // connected_device_info[index].last_number = number; // connected_device_info[index].catch_counter++; // //Flag_is_change_device_connect_show = true; //} //catch (Exception ex) //{ //} //try //{ // connected_device_info[index].catch_counter++; // payload_tmp.doc_name = str.Replace(':', '-') + ".txt"; // payload_tmp.content = "[" + time + "] " + str_detail + " " + str_ASCII + " " + connected_device_info[index].error_counter + "/" + connected_device_info[index].catch_counter; //} //catch (Exception ex) //{ // payload_tmp.doc_name = str.Replace(':', '-') + ".txt"; // payload_tmp.content = "[" + time + "] " + str_detail + " " + str_ASCII; //} //ble_rx_payload.Add(payload_tmp); //Flag_is_no_show_log = true; //log += "[" + time + "] " + str + " RX: " + str_detail+Environment.NewLine; break; case (byte)CMD.BLE_TX: for (int i = 0; i < 6; i++) { str += String.Format("{0:X2}", rx_data[i + 3]); if (i != 5) { str += ":"; } } if (rx_data[9] == 0) { log += "[" + time + "] " + str + " TX: fail" + Environment.NewLine; } else { log += "[" + time + "] " + str + " TX: ok" + Environment.NewLine; } Flag_is_no_show_log = true; break; case (byte)CMD.Scan_Stop: if (rx_data[3] == 0x01) { log += "Set Scan stop (ok)" + Environment.NewLine; } else { log += "Set Scan stop (fail)" + Environment.NewLine; } break; case (byte)CMD.Set_UUID: if (rx_data[3] == 0x01) { log += "Set UUID ok" + Environment.NewLine; } else { log += "Set UUID error(busy)" + Environment.NewLine; } break; case (byte)CMD.Get_UUID: for (int i = 0; i < 16; i++) { str += String.Format("{0:X2}", rx_data[18 - i]); } str_UUID = str; Flag_is_UUID_show = true; log = "Get UUID :" + str + Environment.NewLine; break; case (byte)CMD.Set_Auto_connect_list: if (rx_data[3] == 0x01) { Flag_set_auto_list = true; } break; case (byte)CMD.Get_Auto_connect_list: if (rx_data[4] == 0x01 || rx_data[4] == 0x00) { Get_Auto_connect_list.Clear(); } if (rx_data[4] == 0x00) { break; } info.peer = new byte[6]; for (int i = 0; i < 6; i++) { info.peer[i] = rx_data[i + 5]; info.str_device_mac += String.Format("{0:X2}", rx_data[i + 5]); if (i != 5) { info.str_device_mac += ":"; } } Flag_get_auto_connect_list = true; Flag_is_auto_connect_list_show = true; Get_Auto_connect_list.Add(info); Flag_is_change_device_connect_show = true; break; case (byte)CMD.Get_Connect_list: bool flag = false; if (rx_data[4] == 0x01) { connected_device_info.Clear(); } info.peer = new byte[6]; if (rx_data[5] == 1) { flag = true; } for (int i = 0; i < 6; i++) { info.peer[i] = rx_data[i + 6]; info.str_device_mac += String.Format("{0:X2}", rx_data[i + 6]); if (i != 5) { info.str_device_mac += ":"; } } if (flag) { connected_device_info.Add(info); } Flag_is_change_device_connect_show = true; Flag_get_connected_list = true; break; case (byte)CMD.Start: log = "START" + Environment.NewLine; break; case (byte)CMD.Reset: log = "Reset" + Environment.NewLine; break; case (byte)CMD.Version: str_version = ""; for (int i = 0; i < rx_data[1] - 1; i++) { str_version += (char)rx_data[i + 3]; } if (rx_data[1] > 1) { Flag_get_version = true; } break; } } } catch (Exception ex) { } return(log); }