立即发送通过SerialPortUSB VCP接收到的后续字节

问题描述

我正在尝试在具有USB通讯的电子设备上测试闪存芯片。使用SerialPort(USB VCP)。

我正坐在for循环中,依次读取存储器地址,每次读取后都有很小的延迟。但是我想摆脱延迟,并按如下方式运行:

  1. C#App将字节命令发送到即读取地址0并返回32个字节(地址0至31)
  2. 当C#应用接收到32个字节后,立即发送地址31等的请求
  3. 重复...

那么我该如何以这种快速的方式进行操作,而又没有延迟给串行端口时间来接收字节,而是在收到前一个请求结果之后立即发送下一个请求?

更新#2

async private void buttonMemoryTest_Click(object sender,EventArgs e)
{
    byte[] bytes = new byte[6];
    memoryAddress = 0x00000000;

    bytes[0] = 0x90;
    bytes[1] = 0x01;

    try
    {
        sendStopWatch.Stop();
        sendStopWatch.Reset();
        sendStopWatch.Start();

        //2097152
        for (UInt32 counter = 0; counter < 100; counter++)
        {                    
            bytes[2] = (byte)(memoryAddress >> 24);
            bytes[3] = (byte)(memoryAddress >> 16);
            bytes[4] = (byte)(memoryAddress >> 8);
            bytes[5] = (byte)(memoryAddress);

            serialPortComms.Write(bytes,6);

            await Task.Delay(1);

            memoryAddress += 32;

            time = sendStopWatch.Elapsed;
            textBoxMemoryTestTime.Text = Math.Round(time.TotalSeconds,2) + "s";
            textBoxPageCounter.Text = Convert.ToString(counter);
        }
    }
    catch (InvalidOperationException)
    {
    }
    catch (TimeoutException)
    {
    }
}

void serialPortComms_DataReceived(object sender,SerialDataReceivedEventArgs e)
{
    int dataLength = serialPortComms.BytesToRead;
    byte[] data = new byte[dataLength];
    int nbrDataRead = serialPortComms.Read(data,dataLength);
    if (nbrDataRead == 0)
        return;

    string newStr = "";

    //See if there was data send back
    try
    {
        if (selfTestClicked == 1)
        {
            if (selfTestResponseASCII == 1)
            {
                newStr = Encoding.ASCII.GetString(data,data.Length);
            }
            else
            {
                newStr = BitConverter.ToString(data).Replace("-"," ");
            }
            textBoxReceiveByte_AppendText(newStr);
        }
        else
        {
            //
            if (checkBoxASCII.Checked)
            {
                //newStr = Encoding.UTF8.GetString(data,data.Length);
                newStr = Encoding.ASCII.GetString(data," ");
            }

            if (checkBoxCR_LF.Checked)
            {
                newStr += "\r\n";
            }
            textBoxReceiveByte_AppendText(newStr);
            //textBoxReceiveByte_ChangeText(newStr);
            //processRxData(data);
        }
    }
    catch (IndexOutOfRangeException)
    {
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)