dwilches / Ardity

Assets for integrating Arduino and Unity (or Unity and any hardware that communicates over a COM port)
329 stars 62 forks source link

SendSerialMessage is slow #54

Closed apaks closed 2 years ago

apaks commented 3 years ago

Hey,

Firstly, thanks for developing the package. I am trying to send strings from unity to Arduino at 60 Hz with a very small lag (ms level). However, when I use SendSerialMessage I see a lag of 0.4s.

If I type directly into the Serial Monitor the lag is not there, suggesting it is due to the communication between Unity and Arduino. I also tried native C# SerialPort class but it gives me the same lag.

I also tried wrmhl package. It works well for reading but cannot send data to the Arduino.

Do you expect to have a 0.4 s delay for unity to send data to Arduino?

The code I use:

Unity:
// value = int 0-255
serialController.SendSerialMessage(value.ToString());

Ardunio:
int out = 0;
char buf[4];

void setup() {
  // put your setup code here, to run once:
  pinMode(11, OUTPUT);
  Serial.begin(2000000);
  // stop motor during start
  analogWrite(11, 150);

}
void loop() {
  // put your main code here, to run repeatedly:
  while (Serial.available()) {
    int len = Serial.readBytesUntil('\n', buf, 4);
    out = atoi(buf);
    Serial.println(out);
    memset(buf, 0, sizeof(buf));
    analogWrite(11, out);

  delay(1);
  }
}

Thanks,

dwilches commented 3 years ago

Hello,

I hadn't measured the lag so far, but 0.4s sounds like a lot of lag. If you experienced this lag with the SerialPort class then that's likely the culprit as Ardity uses that class under the hood.

I don't see a way around it using Ardity as the SerialPort class is the heart of it.

Regards.

apaks commented 3 years ago

Thanks for a quick reply. It seems that the issue is specific to my configuration (heavy project). When I use a sample scene on a different computer it works fine. However, the MessageListener is required, otherwise it actually lags.