none
我想在串口通信中用MSCOMM控件,可否给说下怎样用它.? RRS feed

答案

  • 其实MSCOMM控件就是最普通的COM组件,你直接引用即可,在我的blog有相关应用,你可以参考一下

    VB控件在C#中的使用: http://blog.csdn.net/yefanqiu/archive/2006/07/04/875605.aspx

     

    此外在.net下可以直接用SerialPort类,我觉得它比MSCOMM要好用和方便,你可以参考我blog上的这篇文章

    .Net 平台SerialPort类内部实现探秘: http://blog.csdn.net/yefanqiu/archive/2007/04/05/1552651.aspx

     

    SerialPort类相关使用的样例:

    程序块

     public class PortData
        {
            public event PortDataReceivedEventHandle Received;
            public event SerialErrorReceivedEventHandler Error;
            public SerialPort port;
            public volatile bool ReceiveEventFlag = false;  //接收事件是否有效 false表示有效

            public PortData(string sPortName, int baudrate, Parity parity)
            {
                port = new SerialPort(sPortName, baudrate, parity, 8, StopBits.One);
                port.WriteBufferSize = 4096;
                port.RtsEnable = true;
                port.ReadTimeout = 3000;
                port.DataReceived += new SerialDataReceivedEventHandler(DataReceived);
                port.ErrorReceived += new SerialErrorReceivedEventHandler(ErrorEvent);
            }

            ~PortData()
            {
                Close();
            }
            public void Open()
            {
                if (!port.IsOpen)
                {
                    port.Open();
                }
            }

            public void Close()
            {
                if (port.IsOpen)
                {
                    port.Close();
                }
            }
            //数据发送
            public void SendData(byte[] data)
            {
                if (port.IsOpen)
                {
                    port.Write(data, 0, data.Length);
                }
            }
            public void SendData(byte[] data, int offset, int count)
            {
                if (port.IsOpen)
                {
                    port.Write(data, offset, count);
                }
            }
            //发送命令
            public int SendCommand(byte[] SendData, ref  byte[] ReceiveData, int Overtime)
            {
                if (port.IsOpen)
                {
                    ReceiveEventFlag = true;        //关闭接收事件
                    port.DiscardInBuffer();         //清空接收缓冲区 
                    port.DiscardOutBuffer();        //清空发送缓冲区

                    port.Write(SendData, 0, SendData.Length);
                    int num = 0, ret = 0;
                   
                    while (num++ < Overtime)
                    {
                        if (port.BytesToRead >= ReceiveData.Length) break;                   
                        System.Threading.Thread.Sleep(1);
                    }

                    if (port.BytesToRead >= ReceiveData.Length)
                        ret = port.Read(ReceiveData, 0, ReceiveData.Length);
                    ReceiveEventFlag = false;       //打开事件
                    return ret;
                }
                return -1;
            }

            public void ErrorEvent(object sender, SerialErrorReceivedEventArgs e)
            {
                if (Error != null) Error(sender, e);
            }
            //数据接收
            public void DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                //禁止接收事件时直接退出
                if (ReceiveEventFlag) return;

                byte[] data = new byte[port.BytesToRead];
                port.Read(data, 0, data.Length);
                if (Received != null) Received(sender, new PortDataReciveEventArgs(data));
            }

            public bool IsOpen()
            {
                return port.IsOpen;
            }
        }
        public delegate void PortDataReceivedEventHandle(object sender, PortDataReciveEventArgs e);
        public class PortDataReciveEventArgs : EventArgs
        {
            public PortDataReciveEventArgs()
            {
                this.data = null;
            }

            public PortDataReciveEventArgs(byte[] data)
            {
                this.data = data;
            }

            private byte[] data;

            public byte[] Data
            {
                get { return data; }
                set { data = value; }
            }
        }

     

     

    2007年10月11日 8:14
    版主

全部回复

  • MSCOMM是VB6的控件,或说是一个com组件,不能在Embeded System中使用

    2007年7月26日 1:13
  •  

    如果使用.net,建议你用System.IO.Ports
    2007年9月13日 18:25
  • 其实MSCOMM控件就是最普通的COM组件,你直接引用即可,在我的blog有相关应用,你可以参考一下

    VB控件在C#中的使用: http://blog.csdn.net/yefanqiu/archive/2006/07/04/875605.aspx

     

    此外在.net下可以直接用SerialPort类,我觉得它比MSCOMM要好用和方便,你可以参考我blog上的这篇文章

    .Net 平台SerialPort类内部实现探秘: http://blog.csdn.net/yefanqiu/archive/2007/04/05/1552651.aspx

     

    SerialPort类相关使用的样例:

    程序块

     public class PortData
        {
            public event PortDataReceivedEventHandle Received;
            public event SerialErrorReceivedEventHandler Error;
            public SerialPort port;
            public volatile bool ReceiveEventFlag = false;  //接收事件是否有效 false表示有效

            public PortData(string sPortName, int baudrate, Parity parity)
            {
                port = new SerialPort(sPortName, baudrate, parity, 8, StopBits.One);
                port.WriteBufferSize = 4096;
                port.RtsEnable = true;
                port.ReadTimeout = 3000;
                port.DataReceived += new SerialDataReceivedEventHandler(DataReceived);
                port.ErrorReceived += new SerialErrorReceivedEventHandler(ErrorEvent);
            }

            ~PortData()
            {
                Close();
            }
            public void Open()
            {
                if (!port.IsOpen)
                {
                    port.Open();
                }
            }

            public void Close()
            {
                if (port.IsOpen)
                {
                    port.Close();
                }
            }
            //数据发送
            public void SendData(byte[] data)
            {
                if (port.IsOpen)
                {
                    port.Write(data, 0, data.Length);
                }
            }
            public void SendData(byte[] data, int offset, int count)
            {
                if (port.IsOpen)
                {
                    port.Write(data, offset, count);
                }
            }
            //发送命令
            public int SendCommand(byte[] SendData, ref  byte[] ReceiveData, int Overtime)
            {
                if (port.IsOpen)
                {
                    ReceiveEventFlag = true;        //关闭接收事件
                    port.DiscardInBuffer();         //清空接收缓冲区 
                    port.DiscardOutBuffer();        //清空发送缓冲区

                    port.Write(SendData, 0, SendData.Length);
                    int num = 0, ret = 0;
                   
                    while (num++ < Overtime)
                    {
                        if (port.BytesToRead >= ReceiveData.Length) break;                   
                        System.Threading.Thread.Sleep(1);
                    }

                    if (port.BytesToRead >= ReceiveData.Length)
                        ret = port.Read(ReceiveData, 0, ReceiveData.Length);
                    ReceiveEventFlag = false;       //打开事件
                    return ret;
                }
                return -1;
            }

            public void ErrorEvent(object sender, SerialErrorReceivedEventArgs e)
            {
                if (Error != null) Error(sender, e);
            }
            //数据接收
            public void DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                //禁止接收事件时直接退出
                if (ReceiveEventFlag) return;

                byte[] data = new byte[port.BytesToRead];
                port.Read(data, 0, data.Length);
                if (Received != null) Received(sender, new PortDataReciveEventArgs(data));
            }

            public bool IsOpen()
            {
                return port.IsOpen;
            }
        }
        public delegate void PortDataReceivedEventHandle(object sender, PortDataReciveEventArgs e);
        public class PortDataReciveEventArgs : EventArgs
        {
            public PortDataReciveEventArgs()
            {
                this.data = null;
            }

            public PortDataReciveEventArgs(byte[] data)
            {
                this.data = data;
            }

            private byte[] data;

            public byte[] Data
            {
                get { return data; }
                set { data = value; }
            }
        }

     

     

    2007年10月11日 8:14
    版主
  • 你好,能不能详细讲解下在VS中用C++串口通信中的MSCOMM控件的步骤?谢谢。我根本不知道怎么才能到你博客上的那个页面。
    2010年7月12日 14:44