码迷,mamicode.com
首页 > 其他好文 > 详细

RS323串口连接仪器,接收仪器信息

时间:2020-01-17 13:42:36      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:convert   each   substring   icon   its   hand   i++   sel   receive   

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);
            }
        }

RS323串口连接仪器,接收仪器信息

标签:convert   each   substring   icon   its   hand   i++   sel   receive   

原文地址:https://www.cnblogs.com/6B23/p/12205140.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!