public void ParseResult(string strSource, ref string strResult, ref string strReserved, ref string strCmd) { char[] abc = strSource.ToCharArray(); int abcd = Convert.ToInt16(abc[0]); //数据接收完成 if (strSource.EndsWith(EOT + "") || strSource.EndsWith("test-end")) { string strTestTimeNow = DateTime.Now.ToString("yyyyMMddHHmmss"); string strTestTimeNow1 = DateTime.Now.ToString("yyyy-MM-dd"); //把最后接受的消息补充完整 strResult = strResult + strSource; //首先判断消息类型 string[] strSourceArray = strResult.Split(new char[] { LF }, StringSplitOptions.RemoveEmptyEntries); writelog.Write("strSourceArray0" + strSourceArray[0], "log"); //writelog.Write("strSourceArray1" + strSourceArray[1], "log"); //取出消息头 string[] infoTypeArray = strSourceArray[0].Split(new char[] { '|' }); if (infoTypeArray.Length >= 10) { string infoType = infoTypeArray[11]; #region//1.病人测试结果 if (infoType.Equals("SRLT")) { writelog.Write("全部数据:" + Convert.ToChar(13) + Convert.ToChar(10) + strResult, "log"); try { StreamReader sr = new StreamReader(@".\XC8001.txt"); //解析结果检验项目标识符和zlchs定义的变量对应 lis,zlchs|lis1,zlchs1 StringBuilder sBuilder = new StringBuilder(""); string correspondence = sr.ReadLine(); sr.Close(); string times = ""; string[] correspondenceArray = correspondence.Split('|'); //检验时间 strTestTime = strSourceArray[0].Split('|')[13]; times = strTestTime; strTestTime = times.Substring(0, 4) + "-" + times.Substring(4, 2) + "-" + times.Substring(6, 2); for (int i = 0; i < strSourceArray.Length; i++) { //取消息头 string header = strSourceArray[i].Split('|')[0]; if (header.EndsWith("O")) { //样本条码^样本编号^盘号^位置^是否稀释 ^1^1^1^N string OInfo = strSourceArray[i].Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries)[2]; strSampleNo = OInfo.Split('^')[1]; //if ("".Equals(strSourceArray[i].Split('|')[6])) { times = DateTime.Now.ToString("yyyyMMddHHmmss"); } //else { times = strSourceArray[i].Split('|')[6]; } //如果有条码号,取核收时间作为检验时间 string sampleBarstr = OInfo.Split('^')[0]; if (!string.IsNullOrEmpty(sampleBarstr)) { IniFile ConfigIni = new IniFile("SOLVESET.INI"); string encode = ConfigIni.IniReadValue("EQUIPMENT", "Encode"); DataSet ds = new DataSet(); string orgId = ConfigIni.IniReadValue("EQUIPMENT", "Agencies"); string sql = @"to_char(核收时间,'yyyymmddhh24miss') as 核收时间,样本序号"; string where = @" 机构id='" + orgId + "' and 样本条码='" + sampleBarstr + "'"; ds = dsHandle.GetDataSet(sql, "检验记录", where); times = ds.Tables[0].Rows[0]["核收时间"].ToString();//20160202145321 strSampleNo = ds.Tables[0].Rows[0]["样本序号"].ToString(); } strTestTime = times.Substring(0, 4) + "-" + times.Substring(4, 2) + "-" + times.Substring(6, 2); } if (header.EndsWith("R")) { string yiqi = strSourceArray[i].Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries)[2]; string yiqiValue = strSourceArray[i].Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries)[3]; for (int j = 0; j < correspondenceArray.Length; j++) { string lis = correspondenceArray[j].Split(',')[0].Trim(); string zlchs = correspondenceArray[j].Split(',')[1].Trim(); if (yiqi.Equals(lis)) { sBuilder.Append(zlchs + ',' + yiqiValue + '|'); } } } } string str = sBuilder.ToString().Remove(sBuilder.Length - 1); string[] strs = str.Split('|'); string ChannelType = ""; //0-普通结果;1-直方图;2-散点图;3-直方图界标;4-散点图界标;5-BASE64 string testItemID = ""; string TestResultValue = ""; for (int i = 0; i < strs.Length; i++) { FindRow = tItemChannel.Select("通道编码='" + strs[i].Split(',')[0] + "'"); if (FindRow.Length == 0) //无普通结果则查找图像能道,无图像通道则更新通道类型为空 { ChannelType = null; writelog.Write(strDevice, "未设置通道:" + strs[i].Split(',')[0], "log"); } else { testItemID = FindRow[0]["项目id"].ToString(); ChannelType = "0"; //普通结果 TestResultValue = TestResultValue + testItemID + "^" + strs[i].Split(',')[1].Replace("RuPT", "阴性") + "|"; } } TestResultValue = strTestTime + "|" + strSampleNo + "^" + strSampleType + "^" + strBarCode + "|" + strOperator + "|" + StrSpecimen + "|" + "|" + TestResultValue; saveResult = new SaveResult(); if (!string.IsNullOrEmpty(strSampleNo) || !string.IsNullOrEmpty(strBarCode)) { saveResult.SaveTextResult(strInstrument_id, TestResultValue, TestGraph, DrSampleNoField); if (ImmediatelyUpdate) { saveResult.UpdateData(); } } if (!ImmediatelyUpdate) { saveResult.UpdateData(); } } catch (Exception e) { writelog.Write(strDevice, "处理失败: " + e.ToString(), "log"); strResult = ""; } } #endregion #region //2.质控结果 if (infoType.Equals("QRLT")) { writelog.Write("质控数据:" + Convert.ToChar(13) + Convert.ToChar(10) + strResult, "log"); try { StreamReader sr = new StreamReader(@".\XC8001.txt"); //解析结果检验项目标识符和zlchs定义的变量对应 lis,zlchs|lis1,zlchs1 StringBuilder sBuilder = new StringBuilder(""); string correspondence = sr.ReadLine(); sr.Close(); string times = ""; string[] correspondenceArray = correspondence.Split('|'); //检验时间 strTestTime = strSourceArray[0].Split('|')[13]; times = strTestTime; strTestTime = times.Substring(0, 4) + "-" + times.Substring(4, 2) + "-" + times.Substring(6, 2); for (int i = 0; i < strSourceArray.Length; i++) { //取消息头 string header = strSourceArray[i].Split('|')[0]; if (header.EndsWith("O")) { string OInfo = strSourceArray[i].Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries)[3]; strSampleNo = "900" + OInfo.Split('^')[2]; //if ("".Equals(strSourceArray[i].Split('|')[6])) { times = DateTime.Now.ToString("yyyyMMddHHmmss"); } //else { times = strSourceArray[i].Split('|')[6]; } //如果有条码号,取核收时间作为检验时间 string sampleBarstr = OInfo.Split('^')[0]; strTestTime = times.Substring(0, 4) + "-" + times.Substring(4, 2) + "-" + times.Substring(6, 2); } if (header.EndsWith("R")) { string yiqi = strSourceArray[i].Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries)[2]; string yiqiValue = strSourceArray[i].Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries)[3]; for (int j = 0; j < correspondenceArray.Length; j++) { string lis = correspondenceArray[j].Split(',')[0].Trim(); string zlchs = correspondenceArray[j].Split(',')[1].Trim(); if (yiqi.Equals(lis)) { sBuilder.Append(zlchs + ',' + yiqiValue + '|'); } } } } string str = sBuilder.ToString().Remove(sBuilder.Length - 1); string[] strs = str.Split('|'); string ChannelType = ""; //0-普通结果;1-直方图;2-散点图;3-直方图界标;4-散点图界标;5-BASE64 string testItemID = ""; string TestResultValue = ""; for (int i = 0; i < strs.Length; i++) { FindRow = tItemChannel.Select("通道编码='" + strs[i].Split(',')[0] + "'"); if (FindRow.Length == 0) //无普通结果则查找图像能道,无图像通道则更新通道类型为空 { ChannelType = null; writelog.Write(strDevice, "未设置通道:" + strs[i].Split(',')[0], "log"); } else { testItemID = FindRow[0]["项目id"].ToString(); ChannelType = "0"; //普通结果 TestResultValue = TestResultValue + testItemID + "^" + strs[i].Split(',')[1].Replace("RuPT", "阴性") + "|"; } } TestResultValue = strTestTime + "|" + strSampleNo + "^" + strSampleType + "^" + strBarCode + "|" + strOperator + "|" + StrSpecimen + "|" + "|" + TestResultValue; saveResult = new SaveResult(); if (!string.IsNullOrEmpty(strSampleNo) || !string.IsNullOrEmpty(strBarCode)) { saveResult.SaveTextResult(strInstrument_id, TestResultValue, TestGraph, DrSampleNoField); if (ImmediatelyUpdate) { saveResult.UpdateData(); } } if (!ImmediatelyUpdate) { saveResult.UpdateData(); } } catch (Exception e) { writelog.Write(strDevice, "处理失败: " + e.ToString(), "log"); strResult = ""; } } #endregion #region //3.样本请求查询 if (infoType.Equals("QREQ")) { //主动发起请求 //取出2Q|1|55|666|||||||||Oef string[] infoTypeArrayQ = strSourceArray[1].Split(new char[] { '|' }); tiaomahao = infoTypeArrayQ[4]; yangbenhaoStart = infoTypeArrayQ[2]; yangbenhaoEnd = infoTypeArrayQ[3]; //如果是条码号申请 if (!string.IsNullOrEmpty(tiaomahao) && (tiaomahao != "0")) { Mindray may = new Mindray(); DataTable dt = may.GetSampleInfoXC8001(tiaomahao, strInstrument_id); // if (dt != null && dt.Rows.Count > 0) { //校验 //发送病人信息 ETB中间帧,ETX结束帧 // string H = @"1H|\^&|||XC8001|||||LIS||SDWN|E1394-97|strTestTimeNow"; string H = @"1H|\^&|||XC8&|001|||||LIS||SDWN|E1394-97|strTestTimeNow"; /// /// string P = "2P|1||||PatientName|||Patient_Sex||||||23^年"; //(7.1.12 取值为SRLT/SDWN/QRES 时使用)样本条码^样本编号^盘号^位置^是否稀释 //ALB\ALP\ALT\APOA1\APOB\AST\B2MG\CHE\CREA-M\CYSC\DBIL\GGT\HDL-C\LDL-C\LPA\PA\TBA\TBIL\TC\TG\TP\UA\UREA //样本 ID 样本条码^样本编号^盘号^位置^是否稀释 string O = "3O|1|strTestBar^strSampleNo^^^N||全局测试ID|R|strTestTimeNow1|||||||||0||||||||||F"; string L = "4L|1|N"; //发一个帧 string items = dt.Rows[0]["项目编号"].ToString().Replace(",", "\\");/////////////////////////////////////////////////////////////////////////// "/" -> "\\" H = H.Replace("strTestTimeNow", strTestTimeNow); P = P.Replace("Patient_Name", dt.Rows[0]["病人姓名"].ToString()); P = P.Replace("Birthdate", dt.Rows[0]["出生日期"].ToString()); if (dt.Rows[0]["性别"].ToString().Equals("男")) { P = P.Replace("Patient_Sex", "M"); } else { P = P.Replace("Patient_Sex", "F"); } string ybtm = dt.Rows[0]["样本条码"].ToString(); string ybbh = dt.Rows[0]["样本编号"].ToString(); O = O.Replace("strTestBar", ybtm); O = O.Replace("strSampleNo", ybbh); O = O.Replace("全局测试ID", items); O = O.Replace("strTestTimeNow1", strTestTimeNow1); //处理完毕 //strReserved = STX + H + CR + P + CR + O + CR + L + ETX; //string CS = this.CSValidate(strReserved); //strReserved = strReserved + CS + CR + LF + fengefu; string HInfo = H + CR + ETX; string CS = this.CSValidate(HInfo); HInfo = STX + HInfo + CS + CR + LF + fengefu; string PInfo = P + CR + ETX; CS = this.CSValidate(PInfo); PInfo = STX + PInfo + CS + CR + LF + fengefu; string OInfo = O + CR + ETB; CS = this.CSValidate(OInfo); OInfo = STX + OInfo + CS + CR + LF + fengefu; string LInfo = L + CR + ETB; CS = this.CSValidate(LInfo); LInfo = STX + LInfo + CS + CR + LF + fengefu; strReserved = HInfo + PInfo + OInfo + LInfo; strCmd = ENQ + ""; /* * string H = @"1H|\^&|||XC8&|001|||||LIS||SDWN|E1394-97|20161202163209"; * string HInfo = H + CR + ETX; * string CS = this.CSValidate(HInfo); * HInfo = STX + HInfo + CS + CR + LF + fengefu; * * string P = @"2P|1||||王小明|||F||||||30^岁"; * string PInfo = P + CR + ETX; * CS = this.CSValidate(PInfo); * PInfo = STX + PInfo + CS + CR + LF + fengefu; * * string O = @"3O|1|^2^1^2^||ALT\AST|R|2016-12-02|||||||||0||||||||||F"; * string OInfo = O + CR + ETX; * CS = this.CSValidate(OInfo); * OInfo = STX + OInfo + CS + CR + LF + fengefu; * * string L = @"4L|1|N"; * string LInfo = L + CR + ETX; * CS = this.CSValidate(LInfo); * LInfo = STX + LInfo + CS + CR + LF + fengefu; * strReserved = HInfo + PInfo + OInfo + LInfo; * * P = @"2P|1||||王小明1|||F||||||30^年"; * PInfo = P + CR + ETX; * CS = this.CSValidate(PInfo); * PInfo = STX + PInfo + CS + CR + LF + fengefu; * * O = @"3O|1|^2^2^2^||ALT\AST|R|2016-12-02|||||||||0||||||||||F"; * OInfo = O + CR + ETX; * CS = this.CSValidate(OInfo); * OInfo = STX + OInfo + CS + CR + LF + fengefu; * * strReserved = strReserved + HInfo + PInfo + OInfo + LInfo; * */ } else { string H = @"1H|\^&||||||||||QRES|E1394-97|strTestTimeNow" + CR;//+ETB+CS+CR+LF; string L = @"2L|1|I" + CR; H = H.Replace("strTestTimeNow", strTestTimeNow); strCmd = STX + H + L + ETX; string CS = this.CSValidate(strCmd); strCmd = strCmd + CS + CR + LF; //strReserved = strCmd; //strCmd = ENQ + ""; } } } #endregion //4.样本请求回应 if (infoType.Equals("QRES")) { } //5.样本查询回应 if (infoType.Equals("SDWN")) { } //处理完毕后把数据还原为空 strResult = ""; } else { // 光是结束字符 strResult = ""; strCmd = ACK + ""; } } else if (strSource.Equals(ENQ + "")) { strResult = ""; strCmd = ACK + ""; } else if (strSource.Equals(ACK + "")) { //如果收到ack请求,发送下载样本信息 if (!string.IsNullOrEmpty(strReserved)) { //当没有发现样本的时候,把strReserved="",然后在发送EOT if (strReserved.IndexOf("L|1|I") > 0) { strCmd = strReserved; strReserved = ""; } else { string[] cmdInfoArray = strReserved.Split(new string[] { fengefu }, StringSplitOptions.RemoveEmptyEntries); if (cmdInfoArray.Length > 1) { string temp = ""; strCmd = cmdInfoArray[0]; for (int i = 1; i < cmdInfoArray.Length; i++) { temp = temp + cmdInfoArray[i] + fengefu; } strReserved = temp; } else { strCmd = strReserved.Replace(fengefu, "").Replace(ETB + "", ETX + ""); strReserved = ""; } } } //直到发送完毕后,发送结束符号 else { strCmd = EOT + ""; } } //拒绝接受信息 else if (strSource.Equals(NAK + "")) { strResult = ""; strReserved = ""; strCmd = EOT + ""; } else { //用于接受全部的数据 strResult = strResult + strSource; strCmd = ACK + ""; } }
//计算数据帧开始到结束进行校验返回CS public void ParseResult(string strSource, ref string strResult, ref string strReserved, ref string strCmd) { Mindray may = new Mindray(); if (strSource.EndsWith(may.EB + "") || strSource.EndsWith("test-end") || 1 == 1) { try { writelog.Write(strDevice, "接收到数据: " + strSource, "log"); // string[] analysis = strSource.Split('|'); string strTestTime = DateTime.Now.ToString("yyyyMMddHHmmss"); string messageTime = analysis[6]; string messageID = analysis[9]; StreamReader sr = new StreamReader(@".\URIT8021A.txt", Encoding.Default); //解析结果检验项目标识符和zlchs定义的变量对应 lis,zlchs|lis1,zlchs1 StringBuilder sBuilder = new StringBuilder(""); string correspondence = sr.ReadLine(); sr.Close(); string ACK_R01_OK = may.SB + @"MSH|^~\&|||||{0}||ACK^R01|{1}|P|2.3.1||||0||ASCII|||" + may.CR + "MSA|AA|{1}|Message accepted|||0|" + may.CR + may.EB + may.CR; strCmd = string.Format(ACK_R01_OK, strTestTime, messageID); string times = ""; string[] correspondenceArray = correspondence.Split('|'); string[] strSourceArray = strSource.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < strSourceArray.Length; i++) { if (strSourceArray[i].Split(new char[] { '|' })[0].Replace("\n", "").IndexOf("MSH") > 0) { strSampleNo = strSourceArray[i].Split('|')[9]; strSampleNo = strSampleNo.Substring(8, 4); times = strSourceArray[i].Split('|')[9]; //如果有条码号,取核收时间作为检验时间 string sampleBarstr = strSourceArray[i].Split(new char[] { '|' })[9]; sampleBarstr = ""; if (!string.IsNullOrEmpty(sampleBarstr)) { IniFile ConfigIni = new IniFile("SOLVESET.INI"); string encode = ConfigIni.IniReadValue("EQUIPMENT", "Encode"); DataSet ds = new DataSet(); string orgId = ConfigIni.IniReadValue("EQUIPMENT", "Agencies"); string sql = @"to_char(核收时间,'yyyymmddhh24miss') as 核收时间,样本序号"; string where = @" 机构id='" + orgId + "' and 样本条码='" + sampleBarstr + "'"; ds = dsHandle.GetDataSet(sql, "检验记录", where); times = ds.Tables[0].Rows[0]["核收时间"].ToString();//20160202145321 strSampleNo = ds.Tables[0].Rows[0]["样本序号"].ToString(); } strTestTime = times.Substring(0, 4) + "-" + times.Substring(4, 2) + "-" + times.Substring(6, 2); } if (strSourceArray[i].Split(new char[] { '|' })[0].Replace("\n", "").Equals("OBX")) { for (int j = 0; j < correspondenceArray.Length; j++) { string lis = correspondenceArray[j].Split(',')[0]; string zlchs = correspondenceArray[j].Split(',')[1]; if (strSourceArray[i].IndexOf('|' + lis + '|') > 0) { sBuilder.Append(zlchs + ',' + strSourceArray[i].Split('|')[5] + '|'); } } } } string str = sBuilder.ToString().Remove(sBuilder.Length - 1); string[] strs = str.Split('|'); string ChannelType = ""; //0-普通结果;1-直方图;2-散点图;3-直方图界标;4-散点图界标;5-BASE64 string testItemID = ""; string TestResultValue = ""; for (int i = 0; i < strs.Length; i++) { FindRow = tItemChannel.Select("通道编码='" + strs[i].Split(',')[0] + "'"); if (FindRow.Length == 0) //无普通结果则查找图像能道,无图像通道则更新通道类型为空 { ChannelType = null; writelog.Write(strDevice, "未设置通道:" + strs[i].Split(',')[0], "log"); } else { testItemID = FindRow[0]["项目id"].ToString(); ChannelType = "0"; //普通结果 TestResultValue = TestResultValue + testItemID + "^" + strs[i].Split(',')[1].Replace("RuPT", "阴性") + "|"; } } TestResultValue = strTestTime + "|" + strSampleNo + "^" + strSampleType + "^" + strBarCode + "|" + strOperator + "|" + StrSpecimen + "|" + "|" + TestResultValue; saveResult = new SaveResult(); if (!string.IsNullOrEmpty(strSampleNo) || !string.IsNullOrEmpty(strBarCode)) { saveResult.SaveTextResult(strInstrument_id, TestResultValue, TestGraph, DrSampleNoField); if (ImmediatelyUpdate) { saveResult.UpdateData(); } } if (!ImmediatelyUpdate) { saveResult.UpdateData(); } } catch (Exception e) { writelog.Write(strDevice, "处理失败: " + e.ToString(), "log"); } } else { char[] a = strSource.ToCharArray(); int m = a[a.Length - 1]; writelog.Write(strDevice, "最后一个字符: " + m, "log"); } }