public override void HandleData(FileReadEventArgs eventarg) { string TureMessage = PrepreaMessage(eventarg.Message).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); AnswerList.Add(ASTMCommon.cSTX_2.ToString() + sampleId + "-" + result + ASTMCommon.cETX_3.ToString()); AnswerList.Add(ASTMCommon.cEOT_4.ToString()); if (AnswerList.Count > 0 || NakIndex > 0) { SendEnqCommand(0); return; } }
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); } }