Пример #1
0
 void AcceptData()
 {
     if (txtReceiveData.InvokeRequired)
     {
         try
         {
             DelegateAcceptData ddd = new DelegateAcceptData(AcceptData);
             this.Invoke(ddd, new object[] { });
         }
         catch { }
     }
     else
     {
         try
         {
             strRecieve = sp.ReadExisting();
             if (!String.IsNullOrEmpty(strRecieve))
             {
                 txtReceiveData.AppendText(strRecieve);
             }
         }
         catch (Exception ex) { }
     }
 }
Пример #2
0
        void AcceptData()
        {
            string lightdata, timedata, statusdata;
            int    int_lightdata, int_timedata, int_statusdata;

            if (txtReceiveData.InvokeRequired)
            {
                try
                {
                    DelegateAcceptData ddd = new DelegateAcceptData(AcceptData);
                    this.Invoke(ddd, new object[] { });
                }
                catch { }
            }
            else
            {
                try
                {
                    byte[] spRevBytes = new byte[sp.BytesToRead]; //读取一个字节
                    sp.Read(spRevBytes, 0, spRevBytes.Length);    //读取到的是ASCII值

                    for (int i = 0; i < spRevBytes.Length; i++)
                    {
                        txtReceiveData.AppendText(spRevBytes[i].ToString() + " ");
                        //spbuf[spRevnum] = spRevBytes[i];
                        //spRevnum++;
                        if (i + 12 < spRevBytes.Length)
                        {
                            if ((spbuf[i] == 0x5a) && (spbuf[i + 1] == 0xa5) && (spbuf[i + 11] == 0x0d) && (spbuf[i + 12] == 0x0a))
                            {
                                usefuldata[0] = spbuf[i + 2];//时间数据
                                usefuldata[1] = spbuf[i + 3];
                                int_lightdata = usefuldata[0] * 256 + usefuldata[1];

                                usefuldata[2] = spbuf[i + 4];//光照数据
                                usefuldata[3] = spbuf[i + 5];
                                int_timedata  = usefuldata[2] * 256 + usefuldata[3];


                                usefuldata[4]  = spbuf[i + 6];//状态数据
                                int_statusdata = usefuldata[4];

                                usefuldata[5] = spbuf[i + 7];
                                usefuldata[6] = spbuf[i + 8];
                                usefuldata[7] = spbuf[i + 9];
                                usefuldata[8] = spbuf[i + 10];
                                picdata       = usefuldata[0].ToString();
                            }
                        }
                    }

                    /*  if (spRevnum > 100)
                     * {
                     *  spRevnum = 0;
                     *  return;
                     *
                     * }
                     * // if()
                     * if (spRevnum > 7)
                     * {
                     *  for (int j = 0; j < spRevnum - 7; j++)
                     *      if (spRevBytes[j] == 97 && spRevBytes[j + 1] == 97 && spRevBytes[j + 2] == 53 && spRevBytes[j + 3] == 53)
                     *      {
                     *          zijieshu= int.Parse(dataconver.Chr(spRevBytes[j + 4]));
                     *             // if (spRevnum > 5 + zijieshu)
                     *              {
                     *                  int d;
                     *                  for(int jj=0;jj<zijieshu;jj++)
                     *                  spbuf[jj] = spRevBytes[jj + 5];
                     *                  d = int.Parse(dataconver.Chr(spbuf[0])) * 100
                     + int.Parse(dataconver.Chr(spbuf[1])) * 10
                     + int.Parse(dataconver.Chr(spbuf[2])) * 1;
                     +                  picdata = Convert.ToString(d, 10).ToString();
                     +
                     +                  // spRevnum -= 7;
                     +                  txtReceiveDataDec.AppendText(picdata);
                     +              }
                     +
                     +      }
                     +
                     + }*/

                    /*txtReceiveDataHex.AppendText(spRevBytes.Length.ToString());
                     * for (int i = 0; i < spRevBytes.Length - 4; i++)
                     * {
                     *  txtReceiveData.AppendText(spRevBytes[i].ToString() + " ");
                     * // txtReceiveDataHex.AppendText(dataconver.Chr(spRevBytes[i]));//ASCII转为字符
                     *  {
                     *      if (spRevBytes[0] == 0xaa && spRevBytes[1] == 0x55)
                     *      {
                     *          int a, b, c, d;
                     *          a = spRevBytes[2] * 256 + spRevBytes[3];
                     *          picdata = Convert.ToString(a, 10).ToString();
                     *          // spRevnum -= 7;
                     *          txtReceiveDataDec.AppendText(picdata);
                     *      }
                     *
                     *  }
                     * }*/

/*
 *                  for (int i = 0; i < spRevBytes.Length; i++)
 *                  {
 *                      txtReceiveData.AppendText(spRevBytes[i].ToString()  + " ");
 *                      spbuf[spRevnum] = spRevBytes[i];
 *                      spRevnum++;
 *
 *                  }
 *                  txtReceiveDataHex.Text=spRevnum.ToString();
 *                   if (spRevnum > 10)
 *                      {
 *                          for (int j = 0; j < spRevnum-5; j++)
 *                          {
 *                              if (spRevBytes[j] == 97 && spRevBytes[j + 1] == 97 && spRevBytes[j + 2] == 53 && spRevBytes[j + 3] == 53)
 *                              {
 *                                  for (int i = 0; i < spRevBytes[j + 4]; i++)
 *                                   {
 *                                       spbuf1[i] = (byte)(int.Parse(dataconver.Chr(spRevBytes[j + 5 + i])));
 *
 *                                   }
 *                              }
 *
 *                          }
 *                      }
 *
 *                   int a, b, c, d;
 *
 *
 *                   d = spbuf1[0] * 1000 + spbuf1[1] * 100 + spbuf1[2] * 10 + spbuf1[3];
 *                   picdata = d.ToString();
 *                   txtReceiveDataDec.AppendText(picdata);*/
                    /* for (int i = 0; i < spRevnum; i++)
                     * {
                     *   spbuf1[spRevnum+i] = spbuf[i];
                     * }
                     *   for (int i = 0; i < spRevBytes.Length; i++)
                     *   {
                     *       //txtReceiveData.AppendText(spRevBytes[i].ToString() + " ");
                     *
                     *       txtReceiveDataHex.AppendText(dataconver.Chr(spRevBytes[i]));//ASCII转为字符
                     *
                     *       {
                     *           if (spRevBytes[0] == 97 && spRevBytes[1] == 97 && spRevBytes[2] == 53 && spRevBytes[3] == 53)
                     *           {
                     *               int a, b, c, d;
                     *               a = int.Parse(dataconver.Chr(spRevBytes[4]));
                     *               b = int.Parse(dataconver.Chr(spRevBytes[5]));
                     *               c = int.Parse(dataconver.Chr(spRevBytes[6]));
                     *               d = a * 100 + b * 10 + c;
                     *               picdata = d.ToString();
                     *               // spRevnum -= 7;
                     *               txtReceiveDataDec.AppendText(picdata);
                     *           }
                     *
                     *       }
                     *   }
                     *
                     *
                     * /*  strRecieve = sp.ReadExisting();
                     * txtReceiveData.AppendText(strRecieve);
                     * .......................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
                     * byte[] returnBytes1 = dataconver.strToToHexByte(istrRecieve);//字符串转换为16进制数组
                     * for (int i = 0; i < returnBytes1.Length; i++)
                     * {
                     *   string inpustring = returnBytes1[i].ToString();//dataconver.byteToHexStr(returnBytes)
                     *   txtReceiveDataHex.AppendText(inpustring + " ");
                     * }
                     *
                     * txtReceiveDataDec.AppendText(dataconver.StringToHexString(strRecieve, Encoding.UTF8));//16进制字符串,
                     * // txtReceiveData255.AppendText(dataconver.StringToHexString(strRecieve, Encoding.UTF8));
                     * byte[] returnBytes=dataconver.HexStringToBinary(dataconver.StringToHexString(strRecieve, Encoding.UTF8));//每一个字符串都转换为字节数
                     * for (int i = 0; i < returnBytes.Length; i++)
                     * {
                     *   string inpustring= returnBytes[i].ToString();//dataconver.byteToHexStr(returnBytes)
                     *   txtReceiveData255.AppendText(inpustring+" ");
                     * // // // int i = Convert.ToInt32(dataconver.byteToHexStr(returnBytes), 16);
                     *   //txtReceiveDataHex.AppendText(dataconver.Data_Hex_Asc(strRecieve));
                     * }*/



                    //txtReceiveDataHex.AppendText(" ");
                    // strRecieve = sp.ReadExisting();
                    //  txtReceiveDataHex.AppendText(strRecieve);//DemoRealChart.
                    // strRecieve = "Temp4:24.213947,592,0.008862PWM=1000";
                    // /**/
                    //  dataconver.StringToHexString(strRecieve, Encoding.UTF8);
                    //  // txtReceiveDataHex.AppendText(dataconver.Data_Hex_Asc(strRecieve));
                    // byte[] returnBytes = dataconver.strToToHexByte(strRecieve);//字符串转换为16进制数组
                    // txtReceiveDataHex.AppendText(dataconver.byteToHexStr(returnBytes));

                    //System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
                    //byte[] inputBytes = converter.GetBytes(inputString);
                    // //   // string inputString = converter.GetString(i);

                    /*  foreach(int i in returnBytes)
                     * {
                     *  // txtReceiveDataHex.AppendText(i.ToString(""));
                     *   string inputString = converter.GetString(i);
                     *
                     * }*/



                    // txtReceiveDataHex.AppendText(inputString);

                    /* foreach(int i in returnBytes)
                     * {
                     *  txtReceiveDataHex.AppendText(i.ToString(""));
                     *
                     * }*/
                }
                catch (Exception ex) { }
            }
            // sp.DiscardInBuffer();
        }