我正在尝试创建一个小应用程序来收集从连接到COM10的外部传感器接收的数据.我已成功创建了一个小型C#控制台对象和应用程序,它使用for循环打开端口并将数据流传输到文件一段固定的时间.
我想转换此应用程序以使用dataReceived事件来代替流.阅读前5个SerialPort提示后,我似乎仍然无法工作,也不知道我缺少什么.我重写了控制台应用程序,以便所有代码都在Main中并粘贴在下面.有人可以帮助启发我,为什么事件处理程序port_OnReceiveDatazz没有被调用,即使我知道硬件有数据被发送到端口?
谢谢
阿齐姆
PS:感谢@Gabe,@ Jason Down和@abatishchev的所有建议.我很难过,似乎无法让事件处理程序工作.也许它与设备有关.我只能在线程中读取端口并将数据直接传输到文件中.
码
namespace serialPortCollection
{ class Program
{
static void Main(string[] args)
{
const int bufSize = 2048;
Byte[] buf = new Byte[bufSize]; //To store the received data.
SerialPort sp = new SerialPort("COM10", 115200);
sp.DataReceived += port_OnReceiveDatazz; // Add DataReceived Event Handler
sp.Open();
sp.WriteLine("$"); //Command to start Data Stream
// Wait for data or user input to continue.
Console.ReadLine();
sp.WriteLine("!"); //Stop Data Stream Command
sp.Close();
} …Run Code Online (Sandbox Code Playgroud)