简体   繁体   English

C#串行端口或USB HID连接?

[英]C# Serialport or USB HID connection?

For my project I need to communicate with an Cypress PSoC5. 对于我的项目,我需要与赛普拉斯PSoC5进行通信。 I can use a serial connection or a USB HID connection. 我可以使用串行连接或USB HID连接。

I created a C# project for sending and reading data to/from PSoC5. 我创建了一个C#项目,用于向PSoC5发送数据和从PSoC5读取数据。 Right now I'm using the ReceivedData event of the serialport to get notified if there is new data. 现在,我正在使用串行端口的ReceivedData事件来获取是否有新数据的通知。 Basically my project can either receive a datastream that should be plotted in realtime or just some settings I want to update within the GUI. 基本上,我的项目既可以接收应该实时绘制的数据流,也可以接收我想在GUI中更新的一些设置。

Right now I face the problem that the ReceivedData event fires very often (every 32 bytes), which is of course not good when there is a datastream. 现在,我面临一个问题,即ReceivedData事件非常频繁地触发(每32个字节),这在有数据流时当然不好。 Basically I receive 24000 bytes per second if I get data for the plot. 如果我得到绘图的数据,基本上我每秒接收24000字节。 I know I can adjust the ReceivedBytesThreshold, but then I will not get an event for data below the threshold. 我知道我可以调整ReceivedBytesThreshold,但随后不会收到低于阈值的数据事件。

Can anyone tell if there is an approach to handle this? 谁能说出是否有办法解决这个问题?

Would it be an advantage to use the PSoC5 as a HID device instead? 而是将PSoC5用作HID器件是否有优势?

By default SerialPort.ReadBufferSize is set to 4096 bytes. 默认情况下, SerialPort.ReadBufferSize设置为4096字节。 Slighly more info here . 性能稍微更多信息在这里 But you can easily change it to accomodate necessary amount of data. 但是您可以轻松地对其进行更改以容纳必要的数据量。 Then in DataReceived event handler do something like this 然后在DataReceived事件处理程序中执行以下操作

 static void Serial_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {

       if (Serial.BytesToRead < 24000) return;

               ... //Recieve and process your data here
    }

24000 is given here for example. 例如,此处给出24000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM