Serial latency (again)...on Mega 2560

i'm sending binary data.

With oscilloscope i saw the low latency of the usb dongle displaying TX3 and RX3: the protocol i implemented between PC<->Arduino has been made so that when the PC send a 8bit number to the Arduino, it responde soon with a proper answer, checked by the PC; in VB code i repeated this step in a loop, and so i see the very fast answers between the two devices (with usb dongle).

When i try to do the same without the usb-dongle, the process is very slow (i see it because when Arduino receives the number, it change some pins status). Tomorrow i'll try to post some screenshots of the scope (i'll try also for the TX1/RX1, without the dongle).

I post here the sketch i'm using:

/*
 My serial protocol
 Language: Wiring/Arduino
 
 Implement a serial proptocol to read/write the digital pins of ArduinoMega 

 the format for the byte to send to set the n pin as output with specified value is: XXXXXX0V
 where XXXXXX is the ID of the pin (6 bit -> max 64 pins), and V is the value to be set for the pin (0-LOW / 1-HIGH)
         
 the format for the byte to send to read the n pin is: XXXXXX10
 where XXXXXX is the ID of the pin (6 bit -> max 64 pins)
        
 the byte to send back for the response is in the form XXXXXXYV
                
 */
 
int inByte = 0;         // incoming serial byte
int pinNum;
int op;
int pinValue;
int outByte;

void setup()
{
  // start serial port at 9600 bps:
  Serial.begin(115200);
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(13, OUTPUT); 
  digitalWrite(13, LOW);
}

void loop()
{
  // if we get a valid byte, read analog ins:
  if (Serial.available() > 0) {
    // get incoming byte:    
    inByte = Serial.read();
    outByte = inByte;
    pinNum = inByte >> 2;
    op = inByte & 2; // the operation I/O is stored in bit 1
    if (op==0) { // OUTPUT operation
       pinMode(pinNum, OUTPUT);
       pinValue = inByte & 1; // the value to set for the pin is specified at bit 0
       digitalWrite(pinNum, pinValue);
    } else {    // INPUT operation
        pinMode(pinNum, INPUT);
        pinValue = digitalRead(pinNum);
        outByte = outByte | pinValue; // set the value read as LSB 
    }
    Serial.write(outByte);               
    
  }
}

And here is the main VB function i'm using:

Function writeArd(ByVal pinID As Byte, ByVal valueToWrite As Byte) As Byte
        'the format for the byte to send to set the n pin as output with specified value is: XXXXXX0V ...
        ' ... where XXXXXX is the ID of the pin (6 bit -> max 64 pins), and V is the value to be set for the pin (0-LOW / 1-HIGH)
        Dim byteTosend As Byte = (pinID << 2) + valueToWrite
        Dim byteRead As Byte
        Try
            s.Write(New Byte() {byteTosend}, 0, 1)
            While s.BytesToRead() <= 0
            End While
            byteRead = s.ReadByte()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        If byteTosend = byteRead Then
            Return 1
        Else : Throw (New System.Exception("Error reading Arduino pin; byteToSend = " + IntToBin(byteTosend) + ", byteRead = " + IntToBin(byteRead)))
        End If
    End Function

I'll try to write some code to replicate the issue in some easier way....meanwhile every suggestion is appreciated :slight_smile: