我想用SerialPort类从COM口读数据,谁有这方面代码,能不能给参考下,谢谢!
QQ:94210717

解决方案 »

  1.   

    to vs2003
    http://www.codeproject.com/dotnet/DotNetComPorts.aspto vs2005
    http://www.codeproject.com/csharp/serialcommunication.asp
      

  2.   

    to vs2005
    http://www.codeproject.com/csharp/serialcommunication.asp这个我看了,照搬上去以后,出现操作超时异常,这是为什么啊?不捕捉异常的话提示:由于线程退出或应用程序请求,已放弃I/O操作。
    无奈中...
      

  3.   

    to 这个我看了,照搬上去以后,出现操作超时异常,这是为什么啊?不捕捉异常的话提示:由于线程退出或应用程序请求,已放弃I/O操作。你的程序是什么类型
      

  4.   

    什么类型是什么意思?
    我是用c#.net做的一个应用程序。
      

  5.   

    to 我是用c#.net做的一个应用程序。普通的winapp?
    用了线程没有?
      

  6.   

    代码就是你给的那个地址里的代码,出错的地方就是sp.readline();出现异常。using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO.Ports;namespace test
    {
        public partial class Form1 : Form
        {
            //create instance of property page
            //property page is used to set values for stop bits and 
            //baud rate        PropertyPage pp = new PropertyPage();        //create an Serial Port object
            SerialPort sp = new SerialPort();        public Form1()
            {
                InitializeComponent();
            }        private void propertyButton_Click(object sender, EventArgs e)
            {
                //show property dialog
                pp.ShowDialog();            propertyButton.Hide();
            }        private void sendButton_Click(object sender, EventArgs e)
            {
                try
                {
                    //write line to serial port
                    sp.WriteLine(textBox.Text);
                    //clear the text box
                    textBox.Text = "";
                }
                catch (System.Exception ex)
                {
                    baudRatelLabel.Text = ex.Message;
                }        }        private void readButton_Click(object sender, EventArgs e)
            {
                try
                {
                    //clear the text box
                    textBox.Text = "";
                    //read serial port and displayed the data in text box
                    textBox.Text = sp.ReadLine();
                }
                catch (System.Exception ex)
                {
                    baudRatelLabel.Text = ex.Message;
                }
            }        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                MessageBox.Show("Do u want to Close the App");
                sp.Close();
            }        private void saveStatusButton_Click(object sender, EventArgs e)
            {
                //display values
                //if no property is set the default values
                if (pp.bRate == "" && pp.sBits == "")
                {
                    dataBitLabel.Text = "BaudRate = " + sp.BaudRate.ToString();
                    readTimeOutLabel.Text = "StopBits = " + sp.StopBits.ToString();
                }
                else
                {
                    dataBitLabel.Text = "BaudRate = " + pp.bRate;
                    readTimeOutLabel.Text = "StopBits = " + pp.sBits;
                }            parityLabel.Text = "DataBits = " + sp.DataBits.ToString();
                stopBitLabel.Text = "Parity = " + sp.Parity.ToString();
                readTimeOutLabel.Text = "ReadTimeout = " +
                          sp.ReadTimeout.ToString();            if (propertyButton.Visible == true)
                    propertyButton.Hide();
                saveStatusButton.Hide();
                startCommButton.Show();            try
                {
                    //open serial port
                    sp.Open();
                    //set read time out to 500 ms
                    sp.ReadTimeout = 500;
                }
                catch (System.Exception ex)
                {
                    baudRatelLabel.Text = ex.Message;
                }
            }        private void startCommButton_Click(object sender, EventArgs e)
            {
                startCommButton.Hide();
                sendButton.Show();
                readButton.Show();
                textBox.Show();
            }
        }
    }
      

  7.   

    net2.0有现成的类SerialPorts超级好用
                   StringBuilder sb = new StringBuilder();
                    sb.Append((char)27);
                    sb.Append((char)118);
                    SerialPort sp = null;
                    int i = -1;
                    try
                    {
                        sp = new SerialPort(sPort);
                        sp.Open();
                        sp.WriteLine(sb.ToString());
                        sp.ReadTimeout = 3000;
                        i = sp.ReadByte();
                        sp.Close();
                    }
                    catch 
                    {
                        if (sp != null) sp.Close();
                        return i;
                    }
                    return i;
    读取热敏打印机状态
      

  8.   

    发现用sp.read()不会出现这个问题,可以读出数据