示例#1
0
        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 == "HW")
            {
                if (status.OPERATE == "SHT20_UPDATE")
                {
                    l_humi.Text = "";
                    l_humi.Text = status.PARAM1 + "% RH";
                    l_temp.Text = "";
                    l_temp.Text = status.PARAM2 + "℃";
                }
            }
        }
示例#2
0
        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;
                }

//                try
//                {
//                    byte[] read_data = new byte[serialPort1.BytesToRead];
//                    serialPort1.Read(read_data, 0, read_data.Length);
//                    string recv_data_string = Encoding.GetEncoding("GBK").GetString(read_data);


//                    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);
//                    }


//                }
//                catch (Exception ex)
//                {
//                    return;
//                }
            }
        }
        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.StartsWith("CAML"))
                        {
                            if (recv_data_string.Length >= (320 * 2 * 2 + 4))
                            {
                                Color[] colors;
                                colors = RGBToBitmap(recv_data_string.Substring(4, 1280));


                                for (int Ycount = 0; Ycount < 320; Ycount++)
                                {
                                    OvImage.SetPixel(Xcount, Ycount, colors[Ycount]);
                                }
                                Xcount++;
                                if (Xcount == 240)
                                {
                                    Xcount = 0;
                                }
                                pcamera_box.Image = OvImage;
                            }
                            else
                            {
                                Console.WriteLine("camera len error\n" + recv_data_string.Length);
                            }
                        }
                        else 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;
                }
            }
        }