c# - Windows窗体应用程序从串口接收数据

标签 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();
        }        
    }
}

我可以通过上面的代码发送数据,但是对于接收,我无法从中读取数据。没有构建错误。请帮我解决这个问题。

最佳答案

您可以实现“SerialPortDataReceivedEvent”来从串口读取数据。 在使用“DataReceivedEvent”打开与串口寄存器的连接之前,里面 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);
}

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

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

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});       
}

如果您需要进一步说明,请告诉我们。

希望这有帮助..

关于c# - Windows窗体应用程序从串口接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44069749/

相关文章:

c# - 如何在 ASP.NET Web 应用程序中从 Exchange 2010 服务器下载邮件?

C# SerialPort 类用法

c# - 可以从主应用程序启动的带有 WinForms 的 DLL

c# - 如何在文本框为空时输入默认值

asp.net - 爬虫,相对路径到绝对路径

c++ - Arduino与Qt 5.7之间的双向串口通信

python - 在后台进程中捕获串行数据

c# - 在 C# 中的字符串中查找指定字符串的所有索引

c# - Xamarin.Forms。 XAML Label IsVisible 条件未按预期进行评估

c# - 如何使用 Jenkins 安排 C# 单元测试?