SerialPort sp1 = new SerialPort(); getBloodPressur(); public void getBloodPressur() { try { string[] str = SerialPort.GetPortNames(); if (str == null) { MessageBox.Show("仪器未连接,请确认连接是否正常!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error); } sp1.DataReceived += new SerialDataReceivedEventHandler(sp1_DataReceived); //准备就绪 sp1.DtrEnable = true; sp1.RtsEnable = true; //设置数据读取超时为1秒 sp1.ReadTimeout = 2000; foreach (string s in System.IO.Ports.SerialPort.GetPortNames())//获取有多少个COM口 { sp1.PortName = s; } sp1.BaudRate = System.Convert.ToInt32("2400"); //波特率 sp1.DataBits = System.Convert.ToInt32("7"); //数据位 sp1.StopBits = StopBits.One;//停止位 sp1.Parity = Parity.Even;//校验位 sp1.Close(); if (sp1.IsOpen == true)//如果打开状态,则先关闭一下 { sp1.Close(); } sp1.Open(); } catch (Exception ex) { throw ex; } } void sp1_DataReceived(object sender, SerialDataReceivedEventArgs e) { if (sp1.IsOpen) { byte[] byteRead = new byte[sp1.BytesToRead]; //BytesToRead:sp1接收的字符个数 try { Byte[] receivedData = new Byte[sp1.BytesToRead]; //创建接收字节数组 sp1.Read(receivedData, 0, receivedData.Length); //读取数据 sp1.DiscardInBuffer(); //清空SerialPort控件的Buffer string strRcv = null; for (int i = 0; i < receivedData.Length; i++) { strRcv += ((char)System.Convert.ToInt32(receivedData[i])); } Thread.Sleep(500); if (strRcv.Split(‘ ‘).Length > 3) { strBloodPressur = strRcv.Substring(strRcv.Length - 13, 11); if (dicBloodPressur.ContainsKey("7") && dicBloodPressur.ContainsKey("8") && dicBloodPressur.ContainsKey("9")) { if (dtExamCopy.Select("EXAM_ID = ‘" + dicBloodPressur["7"].Split(‘,‘)[0] + "‘ AND EXAM_DETAIL_ID = ‘" + dicBloodPressur["7"].Split(‘,‘)[1] + "‘")[0]["RESULT_VALUE"].ToString().Trim().Length == 0) { dtExamCopy.Select("EXAM_ID = ‘" + dicBloodPressur["7"].Split(‘,‘)[0] + "‘ AND EXAM_DETAIL_ID = ‘" + dicBloodPressur["7"].Split(‘,‘)[1] + "‘")[0]["RESULT_VALUE"] = System.Convert.ToInt32(strBloodPressur.Split(‘ ‘)[0]).ToString() + "/" + System.Convert.ToInt32(strBloodPressur.Split(‘ ‘)[1]).ToString(); checkExamItemAbnormal(dtExamCopy.Rows.IndexOf(dtExamCopy.Select("EXAM_ID = ‘" + dicBloodPressur["7"].Split(‘,‘)[0] + "‘ AND EXAM_DETAIL_ID = ‘" + dicBloodPressur["7"].Split(‘,‘)[1] + "‘")[0])); } else { dtExamCopy.Select("EXAM_ID = ‘" + dicBloodPressur["8"].Split(‘,‘)[0] + "‘ AND EXAM_DETAIL_ID = ‘" + dicBloodPressur["8"].Split(‘,‘)[1] + "‘")[0]["RESULT_VALUE"] = System.Convert.ToInt32(strBloodPressur.Split(‘ ‘)[0]).ToString() + "/" + System.Convert.ToInt32(strBloodPressur.Split(‘ ‘)[1]).ToString(); checkExamItemAbnormal(dtExamCopy.Rows.IndexOf(dtExamCopy.Select("EXAM_ID = ‘" + dicBloodPressur["8"].Split(‘,‘)[0] + "‘ AND EXAM_DETAIL_ID = ‘" + dicBloodPressur["8"].Split(‘,‘)[1] + "‘")[0])); } dtExamCopy.Select("EXAM_ID = ‘" + dicBloodPressur["9"].Split(‘,‘)[0] + "‘ AND EXAM_DETAIL_ID = ‘" + dicBloodPressur["9"].Split(‘,‘)[1] + "‘")[0]["RESULT_VALUE"] = System.Convert.ToInt32(strBloodPressur.Split(‘ ‘)[2]).ToString(); checkExamItemAbnormal(dtExamCopy.Rows.IndexOf(dtExamCopy.Select("EXAM_ID = ‘" + dicBloodPressur["9"].Split(‘,‘)[0] + "‘ AND EXAM_DETAIL_ID = ‘" + dicBloodPressur["9"].Split(‘,‘)[1] + "‘")[0])); } } } catch (System.Exception ex) { MessageBox.Show(ex.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("仪器未连接,请确认仪器连接是否正常!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
原文地址:https://www.cnblogs.com/6B23/p/12205140.html
时间: 2024-10-11 16:49:08