none
求求天上的神仙给个用c#写的能在wince下跑的串口类,实在是感激不尽 RRS feed

答案

  •  

    这个是串口类

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO.Ports;

    namespace DeviceApplication15
    {

        public partial class Serial
        {
          public   SerialPort myport = null;
            public Serial()
            {
                myport = new SerialPort("COM1:");
                myport.BaudRate = 9600;
                myport.DataBits = 8;
            }

            public bool open()
            {
                try
                {
                    if(myport!=null)
                    myport.Open();
                    return true;
                }
                catch (Exception e)
                {
                    return false;
                }
            }

            public void close()
            {
               
                    if (myport != null)
                    {
                        if (myport.IsOpen)
                            myport.Close();
                    }
            }

            public string read()
            {
                try
                {
                    string myString = "";
                    byte[] recvBuff = new byte[1024];
                    int count=0;
                    if (myport.IsOpen)
                    {
                        count = myport.Read(recvBuff, 0, 1024);
                        myString = Encoding.Unicode.GetString(recvBuff, 0, count);
                    }
                    return myString;
                }
                catch(Exception e)
                {
                    return "";
                }
            }

            public bool write(string myString)
            {
                try
                {
                    byte[] writeBuff = Encoding.Unicode.GetBytes(myString);
                    if (myport.IsOpen)
                    {
                        myport.Write(writeBuff, 0, writeBuff.Length);
                        return true;
                    }
                    return false ;
                }
                catch (Exception e)
                {
                    return false;
                }
            }
        }
    }

     

     

    下面这个是页面上的操作

    Serial mySerial = new Serial();
            public Form1()
            {
                InitializeComponent();
                mySerial.myport.DataReceived += new  SerialDataReceivedEventHandler(receiveData);
            }
            private void receiveData(object sender, SerialDataReceivedEventArgs e)
            {

                string mystring=mySerial.read();
            }

     

     

    代码是现写的

    没有CE串口平台

    没有经过测试什么的

    大概就是这样样子

    你自己在真实机上测试调整下

    2008年9月17日 14:49

全部回复

  • 要什么版本的?

    API还是C#的SerialPort?

    2008年9月17日 6:49
  • C#的SerialPort,能读串口就行,谢谢了哈
    2008年9月17日 9:51
  •  

    这个是串口类

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO.Ports;

    namespace DeviceApplication15
    {

        public partial class Serial
        {
          public   SerialPort myport = null;
            public Serial()
            {
                myport = new SerialPort("COM1:");
                myport.BaudRate = 9600;
                myport.DataBits = 8;
            }

            public bool open()
            {
                try
                {
                    if(myport!=null)
                    myport.Open();
                    return true;
                }
                catch (Exception e)
                {
                    return false;
                }
            }

            public void close()
            {
               
                    if (myport != null)
                    {
                        if (myport.IsOpen)
                            myport.Close();
                    }
            }

            public string read()
            {
                try
                {
                    string myString = "";
                    byte[] recvBuff = new byte[1024];
                    int count=0;
                    if (myport.IsOpen)
                    {
                        count = myport.Read(recvBuff, 0, 1024);
                        myString = Encoding.Unicode.GetString(recvBuff, 0, count);
                    }
                    return myString;
                }
                catch(Exception e)
                {
                    return "";
                }
            }

            public bool write(string myString)
            {
                try
                {
                    byte[] writeBuff = Encoding.Unicode.GetBytes(myString);
                    if (myport.IsOpen)
                    {
                        myport.Write(writeBuff, 0, writeBuff.Length);
                        return true;
                    }
                    return false ;
                }
                catch (Exception e)
                {
                    return false;
                }
            }
        }
    }

     

     

    下面这个是页面上的操作

    Serial mySerial = new Serial();
            public Form1()
            {
                InitializeComponent();
                mySerial.myport.DataReceived += new  SerialDataReceivedEventHandler(receiveData);
            }
            private void receiveData(object sender, SerialDataReceivedEventArgs e)
            {

                string mystring=mySerial.read();
            }

     

     

    代码是现写的

    没有CE串口平台

    没有经过测试什么的

    大概就是这样样子

    你自己在真实机上测试调整下

    2008年9月17日 14:49
  • 谢谢天上的神仙
    2008年9月23日 1:10