Beispiel #1
0
        /// <summary>
        /// 读串口线程
        /// </summary>
        public static void Read()
        {
            while (isContinue)
            {
                try
                {
                    if (SyncSerialPort.IsOpen)
                    {
                        string message = SyncSerialPort.ReadTo(",END");

                        EventsClass.GpsEventArgs e       = new EventsClass.GpsEventArgs(message);
                        SyncEventHandler         handler = SyncLogEvent;
                        if (handler != null)
                        {
                            handler(null, e);
                        }
                    }
                }
                catch (Exception MyEx)
                {
                    //MessageBox.Show(MyEx.Message + ":" + MyEx.StackTrace);
                    isContinue = false;
                }
            }
        }
Beispiel #2
0
        /// <summary>
        ///  处理gps信息,刷新控件及发送信息给主窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e">gps信息</param>
        void GPSForm_GpsLogEvent(object sender, EventsClass.GpsEventArgs e)
        {
            if (GpsLog.InvokeRequired)
            {
                LogBoxCallback d = new LogBoxCallback(GPSForm_GpsLogEvent);
                this.Invoke(d, new object[] { sender, e });
            }
            else
            {
                string comming = e.gpsdata;
                comming = comming.Substring(comming.LastIndexOf("EB90"));
                string[] str = comming.Split(',');
                if (str[1] == "03")
                {
                    GpsLog.AppendText("430-->>" + e.gpsdata + ",END" + "\r\n");
                    GpsLog.ScrollToCaret();
                    if (str[2] == "Y")
                    {
                        GpsLog.AppendText("430回复ACK正确" + "\r\n");
                        GpsLog.ScrollToCaret();
                    }
                    else
                    {
                        GpsLog.AppendText("430回复ACK错误" + "\r\n");
                        GpsLog.ScrollToCaret();
                    }
                }
                else
                {
                    string oldstr = comming;


                    oldstr = oldstr.Remove(oldstr.Length - 2);
                    try
                    {
                        string crcnew = CRCHelper.CRC16(oldstr);

                        string newstr = CRCHelper.ConvertCharToHex(comming) + CRCHelper.ConvertCharToHex(",END");
                        GpsLog.AppendText("430-->>" + newstr + "\r\n");
                        GpsLog.ScrollToCaret();
                        string crc = newstr.Substring(newstr.Length - 12, 4);

                        if (crc == crcnew)
                        {
                            string ack = "EB90,03,Y,";
                            ack += CRCHelper.CRC16(ack);
                            ack += ",END";
                            ack  = CRCHelper.ConvertCharToHex(ack);
                            WriteCommand(ack);
                            GpsLog.AppendText("上位机回复ACK正确" + "\r\n");
                            GpsLog.ScrollToCaret();
                        }
                        else
                        {
                            string ack = "EB90,03,N,";
                            ack += CRCHelper.CRC16(ack);
                            ack += ",END";
                            ack  = CRCHelper.ConvertCharToHex(ack);
                            WriteCommand(ack);
                            GpsLog.AppendText("上位机回复ACK错误" + "\r\n");
                            GpsLog.ScrollToCaret();
                            wrongnumber++;
                            numberlabel.Text = "上位机校验错误次数:" + wrongnumber.ToString() + " 双击清零";
                        }
                    }
                    catch (Exception MyEx)
                    {
                        MessageBox.Show(MyEx.Message + ":" + MyEx.StackTrace);
                    }
                }
            }
        }