Aros
Aros

Reputation: 181

Win API serial port need to wait after initialization

I have a following problem. I have a serial port device that is supposed to communicate with a computer. In fact it is Arduino Due board but i don't think it is related.

I use CreateFile to open the port, and then set the parameters using GetCommState()&SetCommState() and GetCommTimeouts()&SetCommTimeouts().

The port is opened correctly - no problem there. But at this point I want to check whether the device is connected. So I send a specific message. The device is supposed to respond in a certain way so that I know it is connected.

Now to the problem: It only works if put Sleep(1000) after Creating the port (before sending the handshake request). It looks as if the WinAPI needs some time before it can begin to use the port. Because the Sleep solution is not generally usable I need to find some alternative...

By it doesn't work I mean ReadFile times out. It times out even if the timeout is set to something like 5 seconds - note that the Sleep interval is only one second. So it looks like the handshake request is not even sent. If I set timeout to 1 second and Sleep interval to one second, it works. If I set timeout to 5 seconds but there's no Sleep it doesn't work. See the problem?

I am going to try some NetworkMonitor, but I'm kinda sure the problem is not with the device...

Upvotes: 0

Views: 222

Answers (1)

Aros
Aros

Reputation: 181

OK, I might have searched a little more before posting this question.

The thing is that Arduino restarts itself when you open a connection from your PC.

When you use a terminal you connect first and write a few seconds later so that the Arduino board has enough time to boot up and you won't notice the thing. Which is what confused me enough to write the question.

There are 3 solutions to this, only 2 of which it makes sense to mention at all:

1) the solution I used without knowing all this (you wait about a second for the board to boot up again...) 2) you disable auto-reset by modifying your Arduino board

Both of them are stupid if you ask me, there should be a switch or a flash variable to do this...

Upvotes: 0

Related Questions