NullReferenceException串行通信接收数据C#

时间:2019-01-17 09:55:16

标签: c# arduino serial-communication

我需要帮助。我的表格通过串行通信从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;
        }
    }
}

如果我的问题不清楚,我将尽快答复。预先谢谢你。

1 个答案:

答案 0 :(得分:0)

似乎myPort为空。它永远不会被初始化。它只是在setSerialPort()中初始化的,永远不会调用。只需确保已使用正确的端口实例化myPort,然后重试即可。

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

在何处查找此信息(仅Windows): 您可以通过打开“设备管理器”,然后展开“端口(COM和LPT)”节点来获取此信息,在该节点下,您将找到要与之通信的设备,可以说它是“通信端口(COM1)”。右键单击它,然后选择属性。然后转到“端口设置”​​标签。在这里,您可以看到所有必需的信息。

用法示例: 在我的情况下,端口名称为COM1,波特率为9600,数据位8,奇偶校验为无,停止位为1。所以我应该这样做:myPort = new SerialPort(“ COM1”,9600,Parity.None,8,StopBits。一个);

然后使用它来获取数据。

希望这可以解决您的问题。