NullReferenceException 串行通信接收数据 C#

NullReferenceException Serial Communication Recieve Data C#

我需要帮助。我通过串行通信从我的 Arduino 接收数据。但是当我尝试读取数据时,我总是得到 NullReferenceException。我的系统是一个简单的计数器。在屏幕中的数字递增之前,它需要查看从 Arduino 按下按钮的次数并将其发送到 Visual Studio。我不知道我做错了什么。

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;
using WMPLib;

namespace ComputerToArduino
{
    public partial class Form2 : Form
    {
        static SerialPort myPort;
        int CECSTnumber = 0;//number on screen
        int CEBMnumber = 0;
        int CECSTLastTicket = 0;//Amount of numbers this button was pressed
        int CEBMLastTicket = 0;//Amount of numbers this button was pressed
        string LastButtonPressed = "";//what button was last pressed?

        public Form2(SerialPort port)
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
        }

        private void Form2_Load(object sender, EventArgs e) { }

        public void CheckLastButton()//Check how many times the buttons were pressed
        {
            LastButtonPressed = myPort.ReadExisting();//NullReferenceException points here

            if (LastButtonPressed == "a")//if CECST button was pressed
            {
                CECSTLastTicket++;
            }
            else if (LastButtonPressed == "b")//if CEBM
            {
                CEBMLastTicket++;
            }
        }

        private void button3_Click(object sender, EventArgs e)//play movie
        {
            axWindowsMediaPlayer1.URL = @"D:\Movies\Movie.mp4";
        }

        private void axWindowsMediaPlayer1_Enter(object sender, EventArgs e) { }

        private void axWindowsMediaPlayer1_ErrorEvent(object sender, EventArgs e) { }

        private void axWindowsMediaPlayer1_MediaError(object sender, AxWMPLib._WMPOCXEvents_MediaErrorEvent e)
        {
            try
            // If the Player encounters a corrupt or missing file, 
            // show the hexadecimal error code and URL.
            {
                IWMPMedia2 errSource = e.pMediaObject as IWMPMedia2;
                IWMPErrorItem errorItem = errSource.Error;
                MessageBox.Show("Error " + errorItem.errorCode.ToString("X") + " in " + errSource.sourceURL);
            }
            catch (InvalidCastException)
            // In case pMediaObject is not an IWMPMedia item.
            {
                MessageBox.Show("Error.");
            }
        }

        private void button2_Click(object sender, EventArgs e) //CECSTincrement
        {
            CheckLastButton();
            if (CECSTnumber > CECSTLastTicket)
            {
                //do nothing
            }
            else
            {
                //CECSTnumber++;
                //CECSTlabel.Text = CECSTnumber.ToString("D3");
                CECSTlabel.Text = Convert.ToString(CECSTLastTicket);
            }
        }

        private void CEBMbutton_Click(object sender, EventArgs e) //CEBMincrement
        {
            CheckLastButton();
            if (CEBMLastTicket > CEBMnumber)
            {
                //do nothing
            }
            else
            {
                CEBMnumber++;
                CEBMlabel.Text = CEBMnumber.ToString("D3");
            }
        }

        public void setSerialPort(SerialPort port)
        {
            myPort = port;
        }
    }
}

如果我的问题不清楚,我会尽快回复。提前谢谢你。

myPort 好像是空的。它永远不会被初始化。它只是在 setSerialPort() 中初始化的,从未被调用过。只需确保您已使用正确的端口实例化 myPort,然后重试。

如果您想了解串口通信: 您需要了解以下有关您正在寻找与之通信的串口的信息: - 端口名称(如 COM1、COM2、COM3 等) - 波特率(也称为每秒位数)、奇偶校验、数据位、停止位。 等等

在哪里可以找到此信息(仅限 Windows): 你可以通过打开 "Device Manager" 然后展开 "Ports (COM & LPT)" 节点来获取这些信息,在它下面你会找到你想要与之通信的设备,假设它是 "Communications Port (COM1)"。右键单击它和 select 属性。然后转到 "Port Settings" 选项卡。在那里您可以看到所有需要的信息。

用法示例: 在我的例子中,端口名称是 COM1,buad 速率是 9600,数据位 8,奇偶校验 None,停止位 1。所以我会这样做:myPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);

然后用它来获取数据。

希望这能解决您的问题。