简体   繁体   English

从串行端口读取和存储字节

[英]Read and Store Bytes from Serial Port

I am trying to create a RS232 application that reads incoming data and stores it in a buffer. 我正在尝试创建一个RS232应用程序,它读取传入的数据并将其存储在缓冲区中。 I found the following code in an RS232 example but I am not sure how to use it 我在RS232示例中找到了以下代码,但我不确定如何使用它

*RS232 Example port_DataReceived* * RS232示例port_DataReceived *

    private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        if (!comport.IsOpen) return;

        if (CurrentDataMode == DataMode.Text)
        {
            string data = comport.ReadExisting();

            LogIncoming(LogMsgType.Incoming, data + "\n");
        }
        else
        {
            int bytes = comport.BytesToRead;

            byte[] buffer = new byte[bytes];

            comport.Read(buffer, 0, bytes);

            LogIncoming(LogMsgType.Incoming, ByteArrayToHexString(buffer) + "\n");

        }
    }

I am trying to write another method that takes an incoming byte array and combines it with another array ... see below: 我正在尝试编写另一个接收传入字节数组并将其与另一个数组相结合的方法...请参阅下文:

private void ReadStoreArray()
{
   //Read response and store in buffer
   int bytes = comport.BytesToRead;
   byte[] respBuffer = new byte[bytes];
   comport.Read(respBuffer, 0, bytes);   

   //I want to take what is in the buffer and combine it with another array
   byte AddOn = {0x01, 0x02}
   byte Combo = {AddOn[1], AddOn[2], respBuffer[0], ...};
}

I currently have both methods in my code as I am confused how to read and store the incoming bytes to the port. 我目前在我的代码中有两种方法,因为我很困惑如何读取和存储传入的字节到端口。 Can I use the "port_DataReceived" method in my "ReadStoreArray" method? 我可以在“ReadStoreArray”方法中使用“port_DataReceived”方法吗? Do I need to modify my "ReadStoreArray" method? 我是否需要修改“ReadStoreArray”方法? Or should I just start over? 或者我应该重新开始?

When you create your SerialPort: 创建SerialPort时:

SerialPort comport = new SerialPort("COM1");
comport.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);

private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    // Shortened and error checking removed for brevity...
    if (!comport.IsOpen) return;
    int bytes = comport.BytesToRead;
    byte[] buffer = new byte[bytes];
    comport.Read(buffer, 0, bytes);
    HandleSerialData(buffer);
}

//private void ReadStoreArray(byte[] respBuffer)
private void HandleSerialData(byte[] respBuffer)
{
   //I want to take what is in the buffer and combine it with another array
   byte [] AddOn = {0x01, 0x02}
   byte [] Combo = {AddOn[1], AddOn[2], respBuffer[0], ...};
}

不要费心使用DataRecieve处理程序,这是非常不准确的,你最好开始一个不断读取串口并抓住每个字节的线程。

You can't read the same data from the port twice. 您无法从端口读取相同的数据两次。 You'll need to read it once into a buffer, then either share the buffer (pass as a function parameter) or clone it to give each function its own copy. 您需要将其读入缓冲区一次,然后共享缓冲区(作为函数参数传递)或克隆它以为每个函数提供自己的副本。

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

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