예제 #1
0
        /// <summary>
        ///
        /// </summary>
        public void senddata()
        {
            if (sp.IsOpen)
            {
                //this.WriteallBtn.Enabled = false;

                List <byte> cmd8001;
                byte[]      cmdcode = new byte[] { 0x80, 0x01 };
                cmd8001 = cmdListbyte(cmdcode);
                byte[] cmd8001bytearray = new byte[cmd8001.Count];
                for (int i = 0; i < cmd8001.Count; i++)
                {
                    cmd8001bytearray[i] = cmd8001[i];
                }

                Console.WriteLine("\r\n" + DateTime.Now.ToString("T").Replace("/", "-") + "  发送:" + "\r\n" + byteToHexStr(cmd8001bytearray) + "\r\n");
                receivedata = Transportation.SendAndRecvData(cmd8001bytearray, 1000, sp);


                if (receivedata != null && receivedata.Length >= 8)
                {
                    byte[] receivedata1 = new byte[cmd8001bytearray.Length - 3 /*receivedata.Length - 3 - cmd8001bytearray.Length*/];
                    Array.Copy(receivedata, receivedata1, cmd8001bytearray.Length - 3 /*receivedata.Length - 3 -cmd8001bytearray.Length */);

                    if (checkSum(receivedata1) == receivedata[cmd8001bytearray.Length - 3 /*receivedata.Length - 3 - cmd8001bytearray.Length*/])
                    {
                        Console.WriteLine(DateTime.Now.ToString("T").Replace("/", "-") + "  写入数据的响应校验正确");
                        Console.WriteLine("" + DateTime.Now.ToString("T").Replace("/", "-") + "  接收:" + "\r\n" + byteToHexStr(receivedata) + "\r\n");
                        this.toolStripStatusLabels.Text = (DateTime.Now.ToString("T").Replace("/", "-") + "  写入设置成功");// 判断出了问题。
                    }
                    else
                    {
                        this.toolStripStatusLabels.Text = (DateTime.Now.ToString("T").Replace("/", "-") + "  接收到的数据无效,请重试或选择正确的串口");
                    }
                }
                else
                {
                    this.toolStripStatusLabels.Text = (DateTime.Now.ToString("T").Replace("/", "-") + "  没有接收到有效的数据,请重试或选择正确的串口");
                }

                //this.WriteallBtn.Enabled = true;
                //this.WriteallBtn.f = true;
            }
            else
            {
                this.toolStripStatusLabels.Text = (DateTime.Now.ToString("T").Replace("/", "-") + "  没有串口连接");
            }
        }
예제 #2
0
        /// <summary>
        /// 读命令,返回数据,解析打印数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void ReadallBtn_Click(object sender, EventArgs e)
        {
            this.ReadallBtn.Enabled = false;
            if (sp.IsOpen)
            {
                List <byte> cmd8002 = new List <byte> {
                    0xaf, 0xaf, 0x00, 0x00, 0xaf, 0x80, 0x02, 0x0d
                };

                byte[] cmd8002bytearray = new byte[0];
                foreach (byte item in cmd8002)
                {
                    cmd8002bytearray = Transportation.MergerArray(cmd8002bytearray, new byte[] { item });
                }
                byte cs = checkSum(cmd8002bytearray);

                cmd8002bytearray = Transportation.MergerArray(cmd8002bytearray, new byte[] { cs });
                byte[] laterbytea = new byte[] { 0x0d, 0x0a };
                cmd8002bytearray = Transportation.MergerArray(cmd8002bytearray, laterbytea);

                Console.WriteLine("\r\n" + DateTime.Now.ToString("T").Replace("/", "-") + "  发送:" + "\r\n" + byteToHexStr(cmd8002bytearray) + "\r\n");
                receivedata = Transportation.SendAndRecvData(cmd8002bytearray, 1000, sp);

                //判断回码
                if (receivedata != null && receivedata.Length >= 8)
                {
                    //this.ReadallBtn.Enabled = false;
                    byte[] receivedata1 = new byte[receivedata.Length - 3];
                    Array.Copy(receivedata, receivedata1, receivedata1.Length);
                    if (checkSum(receivedata1) == receivedata[receivedata.Length - 3])
                    {
                        Console.WriteLine(DateTime.Now.ToString("T").Replace("/", "-") + "读数据的响应校验正确");
                        Console.WriteLine("" + DateTime.Now.ToString("T").Replace("/", "-") + "  接收:" + "\r\n" + byteToHexStr(receivedata) + "\r\n");
                        try
                        {
                            //串口
                            //校验
                            //频率
                            //扩频
                            //模式
                            //扩频带宽
                            //id
                            //netid
                            //power
                            //Breath
                            byte[] raw          = receivedata;
                            string baudrateRead = receivedata[8].ToString();
                            foreach (int key in DicBaudrateSet.Keys)
                            {
                                if (int.Parse(baudrateRead) == DicBaudrateSet[key])
                                {
                                    BaudratesetCbb.Text = key.ToString();
                                }
                            }
                            //
                            string parityRead = receivedata[9].ToString();
                            foreach (string key in DicParitySet.Keys)
                            {
                                if (int.Parse(parityRead) == DicParitySet[key])
                                {
                                    ParitysetCbb.Text = key;
                                }
                            }
                            //refrequency
                            string rffrequencyRead  = receivedata[10].ToString("X2") + receivedata[11].ToString("X2") + receivedata[12].ToString("X2");
                            long   rffrequency      = Convert.ToInt64(rffrequencyRead, 16);
                            double rffrequencyread1 = rffrequency * 61.03515625 / 1000000;
                            string rffrequencyread2 = String.Format("{0:0.00}", rffrequencyread1);
                            RffrequencyCbb.Text = rffrequencyread2;

                            //
                            string a_rffrequencyRead = receivedata[13].ToString();
                            foreach (int key in DicRffactor.Keys)
                            {
                                if (int.Parse(a_rffrequencyRead) == DicRffactor[key])
                                {
                                    RffactorCbb.Text = key.ToString();
                                }
                            }
                            foreach (string key in DicParitySet.Keys)
                            {
                                if (int.Parse(parityRead) == DicParitySet[key])
                                {
                                    ParitysetCbb.Text = key;
                                }
                            }
                            //
                            string modeidRead = receivedata[14].ToString();
                            foreach (string key in DicRfmode.Keys)
                            {
                                if (int.Parse(modeidRead) == DicRfmode[key])
                                {
                                    RfmodeCbb.Text = key;
                                }
                            }
                            //
                            string rfbwRead = receivedata[15].ToString();
                            foreach (string key in DicRfbw.Keys)
                            {
                                if (int.Parse(rfbwRead) == DicRfbw[key])
                                {
                                    RfbwCbb.Text = key;
                                }
                            }
                            //NODEid
                            string raw1 = byteToHexStr(raw);
                            string raw2 = raw1.Replace(" ", "");
                            string nodeidRead;
                            if (RfmodeCbb.Text == "中继")
                            {
                                nodeidRead = raw2.Remove(raw2.Length - 12, 12).Remove(0, 32);
                            }
                            else
                            {
                                nodeidRead = raw2.Remove(raw2.Length - 12, 12).Remove(0, 32);
                            }

                            NodeidCbb.Text = Convert.ToInt64(nodeidRead, 16).ToString();

                            //

                            string netidRead = receivedata[receivedata.Length - 6].ToString("X2");
                            NetidCbb.Text = Convert.ToInt64(netidRead, 16).ToString();

                            //
                            string powerRead = receivedata[receivedata.Length - 5].ToString("X2");

                            PowerCbb.Text = powerRead.Substring(1, 1);
                            //
                            string breathRead = receivedata[receivedata.Length - 4].ToString("X2");
                            foreach (string key in DicBreath.Keys)
                            {
                                if (int.Parse(breathRead) == DicBreath[key])
                                {
                                    BreathCbb.Text = key;
                                }
                            }
                            this.toolStripStatusLabels.Text = (DateTime.Now.ToString("T").Replace("/", "-") + "  读取设置成功");
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(DateTime.Now.ToString("T").Replace("/", "-") + "  接收数据的解析有问题,可能是数据格式的问题");
                        }
                    }
                    else
                    {
                        this.toolStripStatusLabels.Text = (DateTime.Now.ToString("T").Replace("/", "-") + "  没有接收到有效的数据,请重试或选择正确的串口");
                    }
                }
                else
                {
                    Console.WriteLine("Reveive invalid data");
                    this.toolStripStatusLabels.Text = (DateTime.Now.ToString("T").Replace("/", "-") + "  没有接收到有效的数据,请重试或选择正确的串口");
                }
            }
            else
            {
                this.toolStripStatusLabels.Text = (DateTime.Now.ToString("T").Replace("/", "-") + "  没有串口连接");
            }
            Thread.Sleep(500);
            this.ReadallBtn.Enabled = true;
        }