您的位置:首页 > 编程语言 > C#

C#的SerialPort使用,动态创建串口连接,短信猫通信

2010-07-19 11:11 573 查看
using System;

using System.Collections.Generic;

using System.Text;

using System.IO.Ports;

 

/// <summary>

        /// 启动短信猫服务

        /// </summary>

        /// <param name="baudRate">波特率</param>

        /// <returns>0;串口初始化成功;-1:串口初始化失败</returns>

        public int initPorts(int baudRate)

        {

             

            string[] portList = System.IO.Ports.SerialPort.GetPortNames(); //系统串口字符串集合

            foreach (string port in portList)

            {

                SerialPort newPort = new SerialPort();

                newPort.PortName = port;

                newPort.BaudRate = baudRate;

                newPort.ReadTimeout = 600;

                newPort.WriteTimeout = 600;

                newPort.ReceivedBytesThreshold = 10; //接收缓冲区有10字节以上的数据时才触发回调事件

                newPort.Handshake = Handshake.RequestToSend;  //设置通信双方的握手协议

                    //串口监听器,监听收到的数据,ReceiveMessage为一个回调函数

                   // newPort.DataReceived += new SerialDataReceivedEventHandler(ReceiveMessage);

                    this.RegPorts.Add(newPort);

                    newPort.Open();

                   

               

            }

            return 0;

        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# string