Пример #1
0
        /// <summary>
        /// Display received data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void ComReceiveDataEvent(Object sender, SerialPortEventArgs e)
        {
            if (this.InvokeRequired)
            {
                try
                {
                    Invoke(new Action <Object, SerialPortEventArgs>(ComReceiveDataEvent), sender, e);
                }
                catch (System.Exception)
                {
                    //disable form destroy exception
                }
                return;
            }
            var receivedBytes = e.receivedBytes;

            LogHelper.GetLogger <ComTestForm>().Debug(string.Format("接受的消息为: {0}", ByteHelper.Byte2ReadalbeXstring(receivedBytes)));
            laserProtocolFactory.RxQueue.Push(new OriginalBytes(DateTime.Now, receivedBytes));
            if (recStrRadiobtn.Checked) //display as string
            {
                receivetbx.AppendText(Encoding.Default.GetString(receivedBytes));
            }
            else //display as hex
            {
                if (receivetbx.Text.Length > 0)
                {
                    receivetbx.AppendText("-");
                }
                receivetbx.AppendText(IController.Bytes2Hex(e.receivedBytes));
            }
            //update status bar
            receiveBytesCount     += e.receivedBytes.Length;
            toolStripStatusRx.Text = "Received: " + receiveBytesCount.ToString();

            //auto reply
            if (autoReplyCbx.Checked)
            {
                sendbtn_Click(this, new EventArgs());
            }
        }
Пример #2
0
        private void AssembleData(byte[] rawData, ref byte[] finalData)
        {
            if (rawData != null)
            {
                if (rawData.Length == 6)
                {
                    if (rawData[0] == 0x80)
                    {
                        //检查最后一位是否是校验位
                        byte oddCheck = GetOddCheckData(rawData);
                        if (oddCheck == rawData[rawData.Length - 1])
                        {
                            finalData = rawData;
                            buffer    = null;
                            //LogHelper.GetLogger<SerialPortManager>().Error(string.Format("1.激光器接受的原始数据为: {0}", ByteHelper.Byte2ReadalbeXstring(rawData)));
                        }
                        else
                        {
                            LogHelper.GetLogger <SerialPortManager>().Error(string.Format("激光器接受的原始数据异常1,数据为: {0}",
                                                                                          ByteHelper.Byte2ReadalbeXstring(rawData)));
                        }
                    }
                }
                else
                {
                    if (buffer == null)
                    {
                        if (rawData.Length > 0 && rawData[0] == 0x80)
                        {
                            buffer = rawData;
                        }
                    }
                    else
                    {
                        //与buffer中数据拼接
                        int length        = rawData.Length;
                        int bufferLength  = buffer.Length;
                        int sumDataLength = bufferLength + length;
                        if (sumDataLength < 6)

                        {
                            byte[] tempData = new byte[sumDataLength];
                            Array.Copy(buffer, 0, tempData, 0, bufferLength);
                            Array.Copy(rawData, 0, tempData, bufferLength, length);
                            buffer = tempData;
                        }
                        else if (sumDataLength >= 6)
                        {
                            byte[] tempData = new byte[6];
                            Array.Copy(buffer, 0, tempData, 0, bufferLength);
                            Array.Copy(rawData, 0, tempData, bufferLength, 6 - bufferLength);
                            byte oddCheck = GetOddCheckData(tempData);
                            if (oddCheck == tempData[tempData.Length - 1])
                            {
                                finalData = tempData;
                                SetRemainData(rawData);
                                //LogHelper.GetLogger<SerialPortManager>().Error(string.Format("2.激光器接受的原始数据为: {0}",
                                //    ByteHelper.Byte2ReadalbeXstring(rawData)));
                            }
                            else
                            {
                                //数据异常,应该丢弃
                                SetRemainData(rawData);
                                LogHelper.GetLogger <SerialPortManager>().Error(string.Format("激光器接受的原始数据异常2,数据为: {0}",
                                                                                              ByteHelper.Byte2ReadalbeXstring(rawData)));
                            }
                        }
                    }
                }
            }
        }
Пример #3
0
        public byte[] SendData(byte[] bytes, bool catchLog = true)
        {
            byte[] recData = null;
            if (serialPort != null && serialPort.IsOpen)
            {
                try
                {
                    if (catchLog)
                    {
                        LogHelper.GetLogger <SerialPortManager>().Error("------>发送数据 : " + ByteHelper.Byte2ReadalbeXstring(bytes));
                    }

                    serialPort.Write(bytes, 0, bytes.Length);
                    Thread.Sleep(200);
                    while (true)
                    {
                        int dataLength = serialPort.BytesToRead;
                        if (dataLength == 0)
                        {
                            break;
                        }
                        byte[] data = new byte[dataLength];
                        serialPort.Read(data, 0, dataLength);

                        AssembleData(data, ref recData);
                        if (catchLog)
                        {
                            LogHelper.GetLogger <SerialPortManager>().Error("<------接受数据: " + ByteHelper.Byte2ReadalbeXstring(recData));
                        }

                        Thread.Sleep(100);
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.GetLogger <SerialPortManager>().Error(string.Format("激光器串口发送数据异常1:"));
                    LogHelper.GetLogger <SerialPortManager>().Error(ex.Message);
                    LogHelper.GetLogger <SerialPortManager>().Error(ex.StackTrace);
                    return(null);
                }
            }

            return(recData);
        }
Пример #4
0
        public byte[] SendData(List <byte[]> bytesList)
        {
            byte[] recData = null;
            if (serialPort.IsOpen)
            {
                try
                {
                    for (int i = 0; i < bytesList.Count; i++)
                    {
                        serialPort.Write(bytesList[i], 0, bytesList[i].Length);
                        LogHelper.GetLogger <SerialPortManager>().Error(string.Format("激光器发送的原始数据为: {0}", ByteHelper.Byte2ReadalbeXstring(bytesList[i])));
                    }
                    Thread.Sleep(200);
                    while (true)
                    {
                        int dataLength = serialPort.BytesToRead;
                        if (dataLength == 0)
                        {
                            break;
                        }
                        byte[] data = new byte[dataLength];
                        serialPort.Read(data, 0, dataLength);

                        AssembleData(data, ref recData);
                        LogHelper.GetLogger <SerialPortManager>().Error("receive data: " + ByteHelper.Byte2ReadalbeXstring(recData));

                        Thread.Sleep(100);
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.GetLogger <SerialPortManager>().Error(string.Format("激光器串口发送数据异常2:"));
                    LogHelper.GetLogger <SerialPortManager>().Error(ex.Message);
                    LogHelper.GetLogger <SerialPortManager>().Error(ex.StackTrace);
                    return(null);
                }
            }

            return(recData);
        }
Пример #5
0
        private void autoSendtimer_Tick(object sender, EventArgs e)
        {
            //if (laserProtocolFactory.TxQueue != null && laserProtocolFactory.TxQueue.Count > 0)
            //{
            //    var list = laserProtocolFactory.TxQueue.PopAll();
            //    foreach (var o in list)
            //    {
            //        var originalByte = o as OriginalBytes;
            //        if (originalByte != null)
            //        {
            //            controller.SendDataToCom(originalByte.Data);
            //            LogHelper.GetLogger<ComTestForm>().Debug(string.Format("发送的消息为: {0}", ByteHelper.Byte2ReadalbeXstring(originalByte.Data)));
            //        }
            //    }
            //}

            if (motorProtocolFactory.TxQueue != null && motorProtocolFactory.TxQueue.Count > 0)
            {
                var list = motorProtocolFactory.TxQueue.PopAll();
                foreach (var o in list)
                {
                    var originalByte = o as OriginalBytes;
                    if (originalByte != null)
                    {
                        controller.SendDataToCom(originalByte.Data);
                        LogHelper.GetLogger <ComTestForm>().Debug(string.Format("发送的消息为: {0}", ByteHelper.Byte2ReadalbeXstring(originalByte.Data)));
                    }
                }
            }
            //sendbtn_Click(sender, e);
        }
Пример #6
0
 //发送函数
 public byte[] SendDirectCommand(byte[] data, string portName)
 {
     byte[] rev = null;
     try
     {
         PortManager.GetInstance().GetPipe(laserPipeName).GetBusProperty().GetProperty("port").value = portName;
         PortManager.GetInstance().Save();  //不保存打开,当前串口设置失效
         PortManager.GetInstance().Reset(); //解决配置中串口不存在时,后前无法open的bug
         PortManager.GetInstance().GetPipe(laserPipeName).Open();
         LogHelper.GetLogger <SerialPortHelper>().Error("Send Data: " + ByteHelper.Byte2ReadalbeXstring(data));
         SimpleProtocolData newData = new SimpleProtocolData(data);
         object             recv    = PortManager.GetInstance().Send(LARCommandHelper.InsName1, newData);
         if (recv != null)
         {
             rev = ((ByteArrayWrap)recv).GetBytes();
             LogHelper.GetLogger <SerialPortHelper>().Error("Reveived Data: " + ByteHelper.Byte2ReadalbeXstring(data));
         }
     }
     catch (Exception ex)
     {
         LogHelper.GetLogger <SerialPortHelper>().Error("error message: " + ex.Message);
         LogHelper.GetLogger <SerialPortHelper>().Error("error stacktrace: " + ex.StackTrace);
     }
     return(rev);
 }