示例#1
0
 /// <summary>
 /// 数据包装
 /// </summary>
 /// <param name="data"></param>
 public void Send(string data)
 {
     if (!string.IsNullOrEmpty(data))
     {
         // 加锁Lock 防止设备处理不过来
         lock (this)
         {
             try
             {
                 //需要传输的数据
                 Console.WriteLine("发出数据 " + data);
                 //Encoding utf8Encoding = Encoding.UTF8;
                 //Decoder utf8Decoder = Encoding.UTF8.GetDecoder();
                 //utf8Decoder.Convert()
                 byte[] bytes = DMMessage.PackData(data);
                 Thread.Sleep(1000);
                 clientSocket.Send(bytes);
             }
             catch (Exception e)
             {
                 Console.WriteLine("设备已经离去:" + e);
                 this.CloseConnect();
             }
         }
     }
 }
示例#2
0
        public string SendAndReceive(string data)
        {
            if (!string.IsNullOrEmpty(data))
            {
                // 加锁Lock 防止设备处理不过来
                lock (this)
                {
                    try
                    {
                        int loopNum = 0;
                        //需要传输的数据 debug Write
                        Console.WriteLine("发出数据2 " + data);
                        byte[] bytes = DMMessage.PackData(data);
                        Thread.Sleep(1000);
                        clientSocket.Send(bytes);

                        //Thread.Sleep(500);
                        //string bstring = NetDebugHelper.ToHexStrFromByte(bytes);
                        //Console.WriteLine("向设备发送:" + bstring);
                        while (string.IsNullOrEmpty(this.reply))
                        {
                            Thread.Sleep(200);

                            if (loopNum > 25)
                            {
                                return(dmservermainthread.GetErrMessageToLIMS(this.DeviceID, "Device response timeout", (int)COMMAND.ERR_DEVICE));
                            }
                            else
                            {
                                loopNum++;
                            }
                        }
                        string msg = this.reply;
                        this.reply = null;
                        return(msg);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("设备已经离去:" + e);
                        this.CloseConnect();
                        return(dmservermainthread.GetErrMessageToLIMS(this.DeviceID, "The device is disconnected", (int)COMMAND.ERR_CONCET));
                    }
                }
            }
            else
            {
                return(dmservermainthread.GetErrMessageToLIMS(this.DeviceID, "LIMS data is empty or null.", (int)COMMAND.ERR_PROTOCOL));
            }
        }