예제 #1
0
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="buff"></param>
        public void Send(byte[] buff)
        {
            string recv = DefaultEncoding.GetString(buff);

            LogRevMsg.LogText("发送", recv);
            Lib.LogManager.Logger.LogInfo("发送:" + "\r" + recv);
            this.comm.Write(buff, 0, buff.Length);
            //MachineAdapter.Common.Manager.MonitorManager.Instance.SendLog(recv);
        }
예제 #2
0
        void comm_DataReceived_Kim(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                //log.Info("开始接收数据");
                byte[]             RS232DataBuff = GetData(comm);
                SerialHandleObject handle        = new SerialHandleObject();
                if (RS232Received != null)
                {
                    RS232Received(handle, RS232DataBuff);
                    if (handle.handled)
                    {
                        return;
                    }
                }
                //Encoding utf8 = Encoding.GetEncoding(65001);
                //Encoding gb2312 = Encoding.GetEncoding("gb2312");
                string recv = DefaultEncoding.GetString(RS232DataBuff);
                //LogRevMsg.LogText("单次接收", recv);
                //log.Info("单次接收" + "\r" + recv);

                if (!IsASTM)
                {
                    currStr += recv;

                    if (CheckEndStr(currStr, ETXStr))
                    {
                        foreach (byte b in RS232DataBuff)
                        {
                            handleComand(b);
                        }
                        LogRevMsg.LogText("接收", currStr);
                        Lib.LogManager.Logger.LogInfo("接收" + "\r" + currStr);
                        //MonitorManager.Instance.SendCommand(
                        //    RemoteCommand.CMD_LOG, new string[] { LocalConfig.Instance.ItrID, currStr }); //kim 发送至监控服务器
                        if (false)
                        {
                            LogRevMsg.GetFile(currStr);
                        }
                        else
                        {
                            LogRevMsg.GetBufferLog(currStr);
                        }
                        currStr = string.Empty;
                    }
                }
                else
                {
                    try
                    {
                        if (RS232DataBuff.Length == 2 && RS232DataBuff[0] == ASTMCommon.ACK_6 && RS232DataBuff[1] == ASTMCommon.EOT_4)
                        {
                            byte[] buff = new byte[1];
                            buff[0]       = RS232DataBuff[0];
                            RS232DataBuff = buff;
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error(ex.Message);
                    }
                    foreach (byte b in RS232DataBuff)
                    {
                        if (IsASTM)
                        {
                            HandleAstmCommand(b);
                        }
                        char RecChar = Convert.ToChar(b);
                        currStr += RecChar;
                        if (CheckEndStr(currStr, ETXStr))
                        {
                            LogRevMsg.LogText("接收", currStr);
                            Lib.LogManager.Logger.LogInfo("接收" + "\r" + currStr);
                            //MonitorManager.Instance.SendCommand(
                            //    RemoteCommand.CMD_LOG, new string[] { LocalConfig.Instance.ItrID, currStr }); //kim 发送至监控服务器
                            LogRevMsg.GetBufferLog(currStr);
                            currStr = string.Empty;
                        }
                        //if (CheckEndStr(SingleStr, ETXStr) || SingleStr.Contains(ASTMCommon.cENQ_5.ToString()))
                        //{
                        //    LogRevMsg.LogText("接收", SingleStr);
                        //    log.Info("接收" + "\r" + SingleStr);
                        //    //MonitorManager.Instance.SendCommand(
                        //    //    RemoteCommand.CMD_LOG, new string[] { LocalConfig.Instance.ItrID, currStr }); //kim 发送至监控服务器
                        //    if (!SingleStr.Contains(ASTMCommon.cEOT_4.ToString()))
                        //    {
                        //        SingleStr = string.Empty;
                        //    }
                        //}
                        //if (SingleStr.Contains(ASTMCommon.cEOT_4.ToString()))
                        //{
                        //    if (ETXStr != ASTMCommon.cEOT_4.ToString())
                        //    {
                        //        LogRevMsg.LogText("接收", SingleStr);
                        //        log.Info("接收" + "\r" + SingleStr);
                        //    }
                        //    //MonitorManager.Instance.SendCommand(
                        //    //    RemoteCommand.CMD_LOG, new string[] { LocalConfig.Instance.ItrID, currStr }); //kim 发送至监控服务器
                        //    LogRevMsg.GetBufferLog(currStr);
                        //    currStr = string.Empty;
                        //    SingleStr = string.Empty;
                        //    break;
                        //}
                    }
                }
                if (IsNeedAck)
                {
                    if (RS232DataBuff.Length == 1)
                    {
                        if (RS232DataBuff[0] == ASTMCommon.NAK_21)
                        {
                            return;
                        }
                        if (RS232DataBuff[0] == ASTMCommon.ACK_6)
                        {
                            return;
                        }
                    }
                    SendACK();
                }
            }
            catch (Exception ex)
            {
            }
        }
예제 #3
0
        void AsyncReceiveBytes(NetworkStream stream)
        {
            try
            {
                // 短时后台线程:用于处理网络流的读操作,处理完成后即归还线程池。
                // 每个网络流都会分配一个线程。
                //ThreadPool.SetMaxThreads();根据需要设置。
                ThreadPool.QueueUserWorkItem(delegate
                {
                    try
                    {
                        var buffer = new byte[1024 * 1024];// 1024:根据需要进行设置。
                        if (!stream.DataAvailable)
                        {
                            return;
                        }
                        var a    = stream.Read(buffer, 0, buffer.Length);
                        var data = System.Text.Encoding.Default.GetString(buffer, 0, a);
                        if (SpcialItrID == "ASTM")
                        {
                            if (data.Contains(((char)6).ToString()))
                            {
                                if (AckReceived != null)
                                {
                                    AckReceived(stream, null);
                                }
                                return;
                            }
                            if (data.Contains(((char)21).ToString()))
                            {
                                if (NakReceived != null)
                                {
                                    NakReceived(stream, null);
                                }
                                return;
                            }
                            if (!data.Contains(((char)04).ToString()))
                            {
                                curMsg      = curMsg + data;
                                byte[] msg1 = System.Text.Encoding.Default.GetBytes(data);
                                foreach (byte b in msg1)
                                {
                                    if (b == ASTMCommon.cETX_3 || b == ASTMCommon.cSTX_2 || b == ASTMCommon.cENQ_5)
                                    {
                                        LogRevMsg.LogText("发送", ((char)6).ToString());
                                        AsyncSendBytes(stream, System.Text.UTF8Encoding.ASCII.GetBytes(((char)6).ToString()));
                                    }
                                }
                                return;
                            }
                            else
                            {
                                LogRevMsg.LogText("接收:", data);
                                byte[] msg1 = System.Text.Encoding.Default.GetBytes(data);
                                foreach (byte b in msg1)
                                {
                                    if (b == ASTMCommon.cETX_3 || b == ASTMCommon.cSTX_2 || b == ASTMCommon.cENQ_5)
                                    {
                                        LogRevMsg.LogText("发送", ((char)6).ToString());
                                        AsyncSendBytes(stream, System.Text.UTF8Encoding.ASCII.GetBytes(((char)6).ToString()));
                                    }
                                }
                                curMsg += data;
                            }
                        }
                        else if (SpcialItrID == "HL7")
                        {
                            LogRevMsg.LogText("接收:", data);
                            var fs       = ((char)28).ToString();
                            var rpstring = data.Replace(fs, "").Trim();
                            if (rpstring.Length > 500)
                            {
                                curMsg = curMsg + data;
                                if (rpstring.Substring(rpstring.Length - 3) != "||F")
                                {
                                    return;
                                }
                            }
                            else
                            {
                                curMsg = data;
                            }
                        }
                        else if (SpcialItrID == "FS")
                        {
                            LogRevMsg.LogText("接收:", data);
                            if (!data.Contains(((char)28).ToString()) && !data.Contains(((char)0).ToString()))
                            {
                                curMsg = curMsg + data;

                                return;
                            }
                            else
                            {
                                curMsg += data;
                            }
                        }
                        else if (SpcialItrID == "EOT")
                        {
                            LogRevMsg.LogText("接收:", data);
                            if (!data.Contains(((char)04).ToString()) && !data.Contains(((char)0).ToString()))
                            {
                                curMsg = curMsg + data;

                                return;
                            }
                            else
                            {
                                curMsg += data;
                            }
                        }
                        else if (SpcialItrID == "EE" || SpcialItrID == "</TRANSMIT>" || SpcialItrID == "$")
                        {
                            if (!data.Contains(SpcialItrID))
                            {
                                curMsg = curMsg + data;

                                return;
                            }
                            else
                            {
                                curMsg += data;
                            }
                        }
                        else if (SpcialItrID == "}")
                        {
                            LogRevMsg.LogText("接收:", data);
                            if (!data.Contains(((char)28).ToString()) && !data.Contains("}"))
                            {
                                curMsg = curMsg + data;

                                return;
                            }
                            else
                            {
                                curMsg += data;
                            }
                        }
                        else
                        {
                            curMsg = data;
                        }
                        // 本地存储
                        LogRevMsg.Log("11022", curMsg);

                        byte[] msg         = System.Text.Encoding.Default.GetBytes(curMsg);
                        string TureMessage = curMsg.Replace(ASTMCommon.cSTX_2.ToString(), "").
                                             Replace(ASTMCommon.cETX_3.ToString(), "").
                                             Replace(ASTMCommon.cEOT_4.ToString(), "").
                                             Replace(ASTMCommon.cENQ_5.ToString(), "");
                        string[] str =
                            TureMessage.Split(new char[] { ASTMCommon.cLF_10, ASTMCommon.cCR_13 }, StringSplitOptions.RemoveEmptyEntries);
                        string sampleId = str[0].Split('-')[0];
                        string result   = "";
                        Random random   = new Random();
                        result          = random.Next().ToString();
                        ASTMDAO dao     = new ASTMDAO();
                        dao.UpdateOrInsertASTM(sampleId, result);
                        string ResMsg = ASTMCommon.cENQ_5.ToString() + ASTMCommon.cSTX_2.ToString() + sampleId + "-" + result + ASTMCommon.cETX_3.ToString() + ASTMCommon.cEOT_4.ToString();
                        LogRevMsg.LogText("发送:", ResMsg);
                        AsyncSendBytes(stream, UTF8Encoding.ASCII.GetBytes(ResMsg));
                        curMsg = string.Empty;

                        //callback(stream);
                    }
                    catch (Exception ex)
                    {
                        new WorkerThreadExceptionHandlerDelegate(
                            WorkerThreadExceptionHandler).BeginInvoke(ex
                                                                      , null
                                                                      , null);
                        Reset();
                    }
                });
            }
            catch (Exception ex)
            {
                Lib.LogManager.Logger.LogException(ex);
            }
        }