Send following byte received via SerialPort (USB VCP) without delay

0

I am trying to test a flash memory chip on an electronic device having USB comms. Using SerialPort (USB VCP).

I am sitting in a for loop scanning through the memory addresses in sequence with a small delay following each read. However I want to get rid of the delay and have this run as follows:

  1. C# App sends byte command to i.e. read address 0 and return 32 bytes (address 0 to 31)
  2. When C# app receives the 32 bytes then immediately send request for address 31 etc
  3. repeat...

So how do I do it in this fast manner and not having to have a delay giving the serial port time to receive the bytes but rather have the next request send immediately following receipt of previous request results ?

Update #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, 0, 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, 0, 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, 0, data.Length);
            }
            else
            {
                newStr = BitConverter.ToString(data).Replace("-", " ");
            }
            textBoxReceiveByte_AppendText(newStr);
        }
        else
        {
            //
            if (checkBoxASCII.Checked)
            {
                //newStr = Encoding.UTF8.GetString(data, 0, data.Length);
                newStr = Encoding.ASCII.GetString(data, 0, data.Length);
            }
            else
            {
                newStr = BitConverter.ToString(data).Replace("-", " ");
            }

            if (checkBoxCR_LF.Checked)
            {
                newStr += "\r\n";
            }
            textBoxReceiveByte_AppendText(newStr);
            //textBoxReceiveByte_ChangeText(newStr);
            //processRxData(data);
        }
    }
    catch (IndexOutOfRangeException)
    {
    }
}
c#
serial-port
usb
asked on Stack Overflow Sep 3, 2020 by user10552007 • edited Sep 5, 2020 by Baddack

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0