Esempio n. 1
0
        public void MySerialPortStart()
        {
            myComm.OpenSerialPort();
            for (int i = 0; i < 2; i++)
            {
                Console.ReadLine();
                myComm.Send("sss 订单");
            }

            for (int i = 0; i < 2; i++)
            {
                byte[] myBytes = Encoding.UTF8.GetBytes("sss 订单");
                Console.ReadLine();
                myComm.Send(myBytes);
            }

            //for (int i = 0; i < 3; i++)
            //{
            //    Console.ReadLine();
            //    Console.WriteLine(myComm.ReadAllStr());
            //}
            for (int i = 0; i < 3; i++)
            {
                Console.ReadLine();
                byte[] tp = myComm.ReadAllBytes();
                if (tp != null)
                {
                    Console.WriteLine(Encoding.UTF8.GetString(tp));
                }
                else
                {
                    Console.WriteLine("null");
                }
            }
        }
Esempio n. 2
0
        public MyExecutionDeviceResult ExecutionDeviceRun(ICaseExecutionContent yourExecutionContent, CaseActionActuator.delegateGetExecutiveData yourExecutiveDelegate, string sender, ActuatorStaticDataCollection yourActuatorStaticDataCollection, int caseId)
        {
            List <string>           errorList = new List <string>();
            string                  tempError = null;
            MyExecutionDeviceResult myResult  = new MyExecutionDeviceResult();

            myResult.staticDataResultCollection = new System.Collections.Specialized.NameValueCollection();

            //向UI推送执行过程信息
            Action <string, CaseActuatorOutPutType, string> ExecutiveDelegate = (innerSender, outType, yourContent) =>
            {
                if (yourExecutiveDelegate != null)
                {
                    yourExecutiveDelegate(innerSender, outType, yourContent);
                }
            };

            //处理执行错误(执行器无法执行的错误)
            Action <string> DealExecutiveError = (errerData) =>
            {
                if (errerData != null)
                {
                    ExecutiveDelegate(sender, CaseActuatorOutPutType.ExecutiveError, errerData);
                    errorList.Add(errerData);
                }
            };

            if (yourExecutionContent.MyCaseProtocol == CaseProtocol.com)
            {
                //在调用该函数前保证nowExecutionContent.ErrorMessage为空,且as一定成功
                MyComExecutionContent nowExecutionContent = yourExecutionContent as MyComExecutionContent;
                myResult.caseProtocol = CaseProtocol.com;
                myResult.caseTarget   = nowExecutionContent.MyExecutionTarget;
                myResult.startTime    = DateTime.Now.ToString("HH:mm:ss");
                StringBuilder tempCaseOutContent = new StringBuilder();

                System.Diagnostics.Stopwatch myWatch = new System.Diagnostics.Stopwatch();
                myWatch.Start();

                ExecutiveDelegate(sender, CaseActuatorOutPutType.ExecutiveInfo, string.Format("【ID:{0}】[com]Executive···", caseId));

                #region Send
                if (nowExecutionContent.isSend)
                {
                    string nowComData = nowExecutionContent.comContentToSend.GetTargetContentData(yourActuatorStaticDataCollection, myResult.staticDataResultCollection, out tempError);
                    if (tempError != null)
                    {
                        DealExecutiveError(string.Format("this case get static data errer with [{0}]", nowExecutionContent.comContentToSend.GetTargetContentData()));
                        tempCaseOutContent.AppendLine("error with static data");
                    }
                    else
                    {
                        byte[] nowSendBytes;
                        if (nowExecutionContent.comSendEncoding == null)
                        {
                            try
                            {
                                nowSendBytes = MyBytes.HexStringToByte(nowComData, HexaDecimal.hex16, ShowHexMode.space);
                            }
                            catch
                            {
                                nowSendBytes = null;
                            }
                        }
                        else
                        {
                            try
                            {
                                nowSendBytes = nowExecutionContent.comSendEncoding.GetBytes(nowComData);
                            }
                            catch
                            {
                                nowSendBytes = null;
                            }
                        }
                        if (nowSendBytes == null)
                        {
                            DealExecutiveError(string.Format("can not change data to bytes with [{0}]", nowExecutionContent.comContentToSend.GetTargetContentData()));
                            tempCaseOutContent.AppendLine("error with com data");
                        }
                        else
                        {
                            if (mySerialPort.Send(nowSendBytes))
                            {
                                ExecutiveDelegate(sender, CaseActuatorOutPutType.ExecutiveInfo, "send sucess");
                                tempCaseOutContent.AppendLine("send sucess");
                            }
                            else
                            {
                                ExecutiveDelegate(sender, CaseActuatorOutPutType.ExecutiveError, mySerialPort.ErroerMessage);
                                tempCaseOutContent.AppendLine(mySerialPort.ErroerMessage);
                            }
                        }
                    }
                }
                #endregion

                #region receive
                if (nowExecutionContent.isReceive)

                {
                    if (nowExecutionContent.comSleepTime > 0)
                    {
                        System.Threading.Thread.Sleep(nowExecutionContent.comSleepTime);
                    }
                    byte[] recweiveBytes = mySerialPort.ReadAllBytes();

                    if (recweiveBytes != null)
                    {
                        string receiveStr;
                        if (nowExecutionContent.comReceiveEncoding == null)
                        {
                            receiveStr = MyBytes.ByteToHexString(recweiveBytes, HexaDecimal.hex16, ShowHexMode.space);
                        }
                        else
                        {
                            try
                            {
                                receiveStr = nowExecutionContent.comReceiveEncoding.GetString(recweiveBytes);
                            }
                            catch
                            {
                                receiveStr = null;
                            }
                        }
                        if (receiveStr != null)
                        {
                            ExecutiveDelegate(sender, CaseActuatorOutPutType.ExecutiveInfo, receiveStr);
                            tempCaseOutContent.AppendLine(receiveStr);
                        }
                        else
                        {
                            ExecutiveDelegate(sender, CaseActuatorOutPutType.ExecutiveError, string.Format("can not Encoding your data with {0}", MyBytes.ByteToHexString(recweiveBytes, HexaDecimal.hex16, ShowHexMode.space)));
                            tempCaseOutContent.AppendLine("[error]receive data error can not encoding receive data");
                        }
                    }
                }
                #endregion



                myWatch.Stop();
                myResult.spanTime = myResult.requestTime = myWatch.ElapsedMilliseconds.ToString();

                myResult.backContent = tempCaseOutContent.ToString();
            }
            else
            {
                myResult.backContent = "error:your CaseProtocol is not Matching RunTimeActuator";
                DealExecutiveError(myResult.backContent);
            }


            if (errorList.Count > 0)
            {
                myResult.additionalError = errorList.MyToString("\r\n");
            }

            return(myResult);
        }