private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) { if (serialPort1.IsOpen) { try { string recv_data_string; int recv_data_count = serialPort1.BytesToRead; while (serialPort1.BytesToRead > 0) { recv_data_string = serialPort1.ReadLine(); if (recv_data_string.Contains("{") || (serial_recv_status == PARSEING_JSON)) { serial_recv_status = PARSEING_JSON; recv_json_str += recv_data_string; if (recv_data_string.Contains("}")) { serial_recv_status = PRINTFING_LOG; #if CONSOLE_DEBUG Console.WriteLine(recv_json_str); #endif json_status status = JsonConvert.DeserializeObject <json_status>(recv_json_str); json_status_recv_parse(status); recv_json_str = ""; //return; } } else { t_data_recv.AppendText(recv_data_string + '\r' + '\n'); } } } catch (Exception ex) { return; } } }
private void json_status_recv_parse(json_status status) { #if CONSOLE_DEBUG Console.WriteLine("----------json_status_recv_parse-------------"); Console.WriteLine("json func:" + status.FUNC); Console.WriteLine("json operate:" + status.OPERATE); Console.WriteLine("json status:" + status.STATUS); Console.WriteLine("json param1:" + status.PARAM1); Console.WriteLine("json param2:" + status.PARAM2); Console.WriteLine("json param3:" + status.PARAM3); Console.WriteLine("json param4:" + status.PARAM4); Console.WriteLine("json param5:" + status.PARAM5); Console.WriteLine("----------json_status_recv_parse end--------"); #endif if (status.FUNC == "WIFI") { if (status.OPERATE == "WIFI_ON") { if (status.STATUS == "SUCCESS") { //linfo_status.Text = "初始化成功"; bwifi_start.Enabled = false; bwifi_stop.Enabled = true; /* STA角色按钮状态 */ bwifi_scan.Enabled = true; bwifi_connect.Enabled = true; /* AP角色按钮状态 */ bwifi_start_ap.Enabled = true; basic_func_enable(true); } } if (status.OPERATE == "WIFI_SCAN") { int index = dgwifi_scan_result.Rows.Add(); dgwifi_scan_result.Rows[index].Cells[0].Value = status.PARAM1; dgwifi_scan_result.Rows[index].Cells[1].Value = status.PARAM2; dgwifi_scan_result.Rows[index].Cells[2].Value = status.PARAM3; dgwifi_scan_result.Rows[index].Cells[3].Value = status.PARAM4; } if (status.OPERATE == "WIFI_CONNECT") { if (status.STATUS == "SUCCESS") { //linfo_status.Text = "连接成功"; bwifi_connect.Enabled = false; bwifi_disconnect.Enabled = true; bshow_ip.Enabled = true; } } if (status.OPERATE == "WIFI_SHOW_IP") { lshow_ip.Text = "IP地址:" + status.PARAM1 + " 子网掩码: " + status.PARAM2 + " 网关: " + status.PARAM3; } if (status.OPERATE == "WIFI_START_AP") { if (status.STATUS == "SUCCESS") { //linfo_role.Text = "AP"; //linfo_status.Text = "开启热点成功"; bwifi_start_ap.Enabled = false; bwifi_stop_ap.Enabled = true; bshow_ip.Enabled = false; } } if (status.OPERATE == "WIFI_AP_CON_IND") { int count = dgwifi_ap_con_result.Rows.Count; for (int index = 0; index < count - 1; index++) { if (dgwifi_ap_con_result.Rows[index].Cells[0].Value.ToString() == status.PARAM1) { #if CONSOLE_DEBUG Console.WriteLine("数据存在"); #endif return; } } int data_index = dgwifi_ap_con_result.Rows.Add(); dgwifi_ap_con_result.Rows[data_index].Cells[0].Value = status.PARAM1; dgwifi_ap_con_result.Rows[data_index].Cells[1].Value = status.PARAM2; dgwifi_ap_con_result.Rows[data_index].Cells[2].Value = status.PARAM3; if (dgwifi_ap_con_result.Rows.Count > 0) { bwifi_disconnect_sta.Enabled = true; } } if (status.OPERATE == "WIFI_AP_DISCON_RET") { int count = dgwifi_ap_con_result.Rows.Count; for (int index = 0; index < count - 1; index++) { string mac = dgwifi_ap_con_result.Rows[index].Cells[1].Value.ToString(); if (mac == status.PARAM1) { dgwifi_ap_con_result.Rows.RemoveAt(index); } } if (dgwifi_ap_con_result.Rows.Count > 1) { bwifi_disconnect_sta.Enabled = true; } else { bwifi_disconnect_sta.Enabled = false; } } if (status.OPERATE == "WIFI_PING") { if (Convert.ToInt32(status.PARAM2) == 0) { tping_result.AppendText("receive from " + status.PARAM1 + " time< 1ms" + '\r' + '\n'); } else { tping_result.AppendText("receive from " + status.PARAM1 + " time=" + status.PARAM2 + " ms" + '\r' + '\n'); } } if (status.OPERATE == "WIFI_STOP_PING") { Int32 loss_all = 0; double packet_loss = 0; if (Convert.ToInt32(status.PARAM2) == 0) { loss_all = 1; } else { packet_loss = (Convert.ToDouble(status.PARAM1) - Convert.ToDouble(status.PARAM2)) / Convert.ToDouble(status.PARAM2); } tping_result.AppendText("" + '\r' + '\n'); tping_result.AppendText("--- ping statistics ---" + '\r' + '\n'); tping_result.AppendText(status.PARAM1 + " packets transmitted, " + status.PARAM2 + " received, "); if (loss_all == 1) { tping_result.AppendText("100%packet loss" + '\r' + '\n'); } else { tping_result.AppendText(packet_loss.ToString() + "%packet loss" + '\r' + '\n'); } } if (status.OPERATE == "WIFI_UDP_RECV") { /* PARAM1:DATA PARAM2:LENGTH */ int udp_recv_count = Convert.ToInt32(ludp_recv_count.Text) + Convert.ToInt32(status.PARAM2); ludp_recv_count.Text = udp_recv_count.ToString(); tudp_recv.AppendText(status.PARAM1 + '\r' + '\n'); } if (status.OPERATE == "WIFI_TCPC_RECV") { /* PARAM1:DATA PARAM2:LENGTH */ int tcp_recv_count = Convert.ToInt32(ltcp_recv_count.Text) + Convert.ToInt32(status.PARAM2); ltcp_recv_count.Text = tcp_recv_count.ToString(); ttcp_recv.AppendText(status.PARAM1 + '\r' + '\n'); } if (status.OPERATE == "WIFI_TCPS_RECV") { /* PARAM1:IP PARAM2:PORT PARMA3:DATA PARAM4:LENGTH */ int tcp_recv_count = Convert.ToInt32(ltcp_recv_count.Text) + Convert.ToInt32(status.PARAM4); ltcp_recv_count.Text = tcp_recv_count.ToString(); ttcp_recv.AppendText("收到IP:" + status.PARAM1 + " 端口:" + status.PARAM2 + " 数据:" + status.PARAM3 + '\r' + '\n'); } if (status.OPERATE == "WIFI_MQTT_SUBCRIBE_RECV") { tcloud_subcribe_data.AppendText(status.PARAM1 + '\r' + '\n'); } } }