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

Posted 6b23

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RS323串口连接仪器,接收仪器信息相关的知识,希望对你有一定的参考价值。

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串口连接仪器,接收仪器信息的主要内容,如果未能解决你的问题,请参考以下文章

LabVIEW串口通信

Java实现RS485串口通信,发送和接收数据进行解析

labview怎么读取RS485转网口的数据

请问RS232-C接口的特点及典型应用,谢谢!!

小草手把手教你LabVIEW串口仪器控制—安装使用仪器现有驱动

小草手把手教你 LabVIEW 串口仪器控制——初识VISA串口