Serial communication with visual basic

Hi,
i have an arduino UNO whitch needs to communicate to visualbasic.
So i wrote a code to send a serial command to visual basic, then if visual basic answers with another serialcode then a variable changes so that arduino goes to a function, when i shutdown the visualbasic software it sends a serialcode to the arduino then the arduino answers with again another serial code and changes his variable back and leaves the function.

Everything works fine, my only problem is that it goes realy slow, like a serial command / seccond. Can you guys find out why ?

Thanks.

testcode2.ino (509 Bytes)

Hi,
i just found out that it is the
received = Serial.parseInt();
which is slowing everything down.

Is there a way of speeding this up to normal speed ?

atoi() or similar function

never heard of that, how would my code look then ?

Is there a way of speeding this up to normal speed ?

The parseInt() function is waiting for something to indicate that the end of the int has arrived. So, send something. 12 will be a lot faster to parse than 12.

parseint() looks for an integer until either it sees a non-digit or it times out. The default timeout is one second. You can change the timeout (in milliseconds) using SetTimeout(). It might help if you reduce it to 100ms (or less). See https://www.arduino.cc/en/Serial/ParseInt and https://www.arduino.cc/en/Serial/SetTimeout

Pete

ok, so i added a non-digit to the end but then i still had the problem that it was slow because it was looking for serial data while i wasn't sending.

So i changed my code to if (Serial.read() != -1) { Serial.println(Serial.read()); received = Serial.parseInt(); }

now everything is at the good speed again.

Thanks to all of you guys

if (Serial.read() != -1)

Is a poor way to test that there is serial data available to read. There is a Serial.available() function, for a reason.

yeah i know but it goes into that function even when i don't send any serial data.

well, turns out that the solution that i found speeded everything up again but for some reason the it stoped responding to the serial data.

So how can i solve that ?

msg #5

well i don't know how to use the method mensioned in msg#5

But everything is ok now after a few changes.

Put this statement after Serial.begin() in the setup() function.

  Serial.setTimeout(100);

Pete