【发布时间】:2020-03-03 16:47:04
【问题描述】:
我正在尝试通过串行端口将实时传感器数据从 FTDI 读取到 C#。我已经执行了这段代码,但我没有得到任何数据。我已经设置了串口的参数:
- 波特率:115200
- 数据位:8
- 停止位:1
- RtsEnable:真
- DtrEnable:真
是否需要添加FTDI库才能读取数据?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using System.Timers;
using System.IO.Ports;
using System.Text.RegularExpressions;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
double[] a = new double[30];
public Form1()
{
InitializeComponent();
serialPort1.PortName = "COM5";
serialPort1.DataReceived += serialPort1_DataReceived;
}
public void configrations()
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void button1_Click(object sender, EventArgs e)
{
serialPort1.Open();
}
int x = 0;
int y = 0;
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
try
{
int dataLength = serialPort1.BytesToRead;
byte[] dataRecevied = new byte[dataLength];
int nbytes = serialPort1.Read(dataRecevied, 0, dataLength);
string line1 = Convert.ToString(nbytes * 10000);
this.BeginInvoke(new LineReceivedEvent(LineReceived), line1);
}
catch { }
}
private delegate void LineReceivedEvent(string line1);
private void LineReceived(string line1)
{
label1.Text = line1;
}
private void button2_Click(object sender, EventArgs e)
{
serialPort1.Close();
}
}
}
【问题讨论】:
标签: visual-studio ftdi