コード例 #1
0
 private void Serial_OnReceived(object sender, ReceiveEventArgs e)
 {
     if (InvokeRequired)
     {
         BeginInvoke(new ReceivedEventHandler(Serial_OnReceived), sender, e);
     }
     else
     {
         try
         {
             byte[] data = (byte[])e.Data;
             if (data != null && data.Length != 0)
             {
                 if (showHex)
                 {
                     Trace.AppendText(GXCommon.ToHex((byte[])e.Data, true));
                 }
                 else
                 {
                     string tmp = ASCIIEncoding.ASCII.GetString((byte[])e.Data);
                     if (data[0] == '\t' && data.Length != 1 && data[1] == ':')
                     {
                         tmp = DateTime.Now + "\t" + tmp.Substring(2);
                     }
                     else if (data[0] == '\t' && data.Length == 1)
                     {
                         startTrace = true;
                         return;
                     }
                     else if (startTrace)
                     {
                         if (data[0] == ':')
                         {
                             if (data.Length == 1)
                             {
                                 return;
                             }
                             tmp = tmp.Substring(1);
                         }
                         tmp        = DateTime.Now + "\t" + tmp;
                         startTrace = false;
                     }
                     Trace.AppendText(tmp);
                     if (data[data.Length - 1] == 0)
                     {
                         Trace.AppendText(Environment.NewLine);
                     }
                 }
             }
         }
         catch (Exception ex)
         {
             Trace.AppendText(ex.Message);
         }
     }
 }