我在这里有一个小问题。 我正在制作一个小型气象站,我试图在visual studio中读取数据。
在arduino中它看起来像这样
http://oi59.tinypic.com/2uzbknm.jpg
但是当我尝试在Visual Studio中阅读时,我得到了这个
http://i59.tinypic.com/2rykccn.jpg
这是我的代码,我已经考虑过问题所在,但我不知道如何解决它..
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 WeatherStation
{
public partial class Form1 : Form
{
public delegate void PrintString(String data);
public PrintString printDelegate;
private SerialPort port;
private char[] data;
private int index;
public Form1()
{
InitializeComponent();
data = new char[128];
index = 0;
printDelegate = new PrintString(PrintData);
}
private void btnStart_Click(object sender, EventArgs e)
{
try
{
port = new SerialPort("COM3", 9600, Parity.None, 8);
port.DataReceived += DataReceivedHandler;
port.Open();
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
private void PrintData(String data)
{
txtBoxData.AppendText(" " + data + Environment.NewLine);
}
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
try
{
data[index] = (char)port.ReadByte();
if (data[index] == '\n' )
{
if (this.InvokeRequired)
{
this.Invoke(printDelegate, new object[] { new String(data) });
}
index = 0;
}
else
{
index++;
}
}
catch (Exception exc) //!!
{
MessageBox.Show(exc.Message);
}
}
private void btnStop_Click(object sender, EventArgs e)
{
port.Close();
}
}
}
我认为这个问题出现在' \ n'但正如我之前所说,我不知道如何解决它。
data[index] = (char)port.ReadByte();
if (data[index] == '\n' )
提前感谢您的帮助。