从Windows窗体应用程序中的串口接收数据

时间:2017-05-19 12:10:15

标签: c# winforms serial-port

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;

namespace WindowsFormsApp7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            getavaialbleports();
        }

        void getavaialbleports()
        {
            String[] ports = SerialPort.GetPortNames();
            comboBox1.Items.AddRange(ports);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (comboBox1.Text == "" || comboBox2.Text == "")
                {
                    textBox2.Text = "Please select port settings";
                }
                else
                {
                    serialPort1.PortName = comboBox1.Text;
                    serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);
                    serialPort1.Open();
                    button1.Enabled = false;
                }
            }

            catch(UnauthorizedAccessException)
            {
                textBox2.Text = "Unauthorised Access";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            serialPort1.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine(textBox1.Text);
            textBox1.Text = "";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = serialPort1.ReadLine();
        }        
    }
}

我能够从上面的代码发送数据,但是为了接收,我无法从中读取数据。没有构建错误。请帮我解决这个问题。

2 个答案:

答案 0 :(得分:2)

您可以实现“SerialPortDataReceivedEvent”以从串行端口读取数据。 在打开与“DataReceivedEvent”,Inside之间的串口寄存器连接之前 button1_Click事件添加以下代码。

serialPort1.DataReceived += new SerialDataReceivedEventHandler(mySerialPort_DataReceived);

然后添加以下事件处理程序

private static void mySerialPort_DataReceived(
                    object sender,
                    SerialDataReceivedEventArgs e)
{
    SerialPort sp = (SerialPort)sender;
    string indata = sp.ReadExisting();
    //data received on serial port is asssigned to "indata" string
    //Console.WriteLine("Received data:");
    //Console.Write(indata);
}

此外,尝试配置Parity,StopBits,DataBits等其他属性,类似于另一端的设备(您尝试与之通信)。

更新UI上的数据: 你需要的是一个委托方法,它使用给定的字符串设置文本框的Text属性。然后,通过mySerialPort_DataReceived方法从TextBox.Invoke()处理程序中调用该委托。像这样:

public delegate void AddDataDelegate(String myString);
public AddDataDelegate myDelegate;

private void Form1_Load(object sender, EventArgs e)
{
  //...
  this.myDelegate = new AddDataDelegate(AddDataMethod);
}

public void AddDataMethod(String myString)
{
 textbox1.AppendText(myString);
}

private void mySerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
 SerialPort sp = (SerialPort)sender;
 string indata = sp.ReadExisting();

 textbox1.Invoke(this.myDelegate, new Object[] {indata});       
}

如果您需要进一步澄清,请告诉我们。

希望这会有所帮助..

答案 1 :(得分:0)

在我看来,首先尝试测试,当启动串口添加

serialPort1.DataReceived += new 
SerialDataReceivedEventHandler(port_DataReceived);

private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    int bytesToRead = serialPort1.BytesToRead;
    System.Diagnostics.Debug.WriteLine(bytesToRead);
}

之后在“输出”窗口中,您将看到海港中是否存在任何数据