program not responding when addresses arduino as file connection

Hello,

I write and read to adruino using serial port as file connection. I tried to use R and VBA. In both cases the program works fine if I first address arduino with Serial Monitor. Without using Serial Monitor both programs not responding when addressing arduino. What does Serial Monitor do to the COM port that it starts to work as expected. Before that I used mscomm32.ocx in vba and I did not have such problem.

Do you keep opening and closing the file?

(feel free to post any code or diagrams that might help us to help you)

A common problem for people writing PC programs is that they don't take account of the behaviour of the Arduino.

When a PC program opens the serial port the Arduino resets and it takes a few seconds to go through that process. The PC program must wait until that is complete before it can send data to the Arduino. Also, the PC program must keep the serial port open until it is finished using the Arduino.

A simple way to know that the Arduino has reset is to have it send a short message from setup() and to have the PC program wait until it receives that message.

This demo using Python illustrates how it can be done.

It may be a useful exercise to write a VBA or R program that does what the Python program does while retaining my working Arduino program unchanged.

...R

Here is what I do (“1” is signal to Arduino to take measurement):

  1. in VBA:
Open fCOM For Binary Access Read Write As #1 ' where fCOM is path to COM port ("\\.\COM10" in my case)
Put #1, , "1"    ' "1" is command to Arduino to make measurement in my case
Line Input #1, mBuffer   ' mBuffer is variable for result that is used by VBA

Program stops (not responding) at line with Put function

  1. In R:
system('mode COM10 9600,N,8,1)
ser <- file("\\\\.\\COM10", open="r+")
isOpen(ser)
write('1', ser)
data <- scan(ser, what = list(numeric(length=1),numeric(length=1)), sep = ",")

This code stops (not responding) at the line with scan() function

I tied as you suggested to make 2 seconds delay after COM opening - did not solve the problem.

Again, I confirm that in both cases of R and VBA my programs start working after I open Serial Monitor, send 1 to it (get reading) and close it. After this manipulation with Serial Monitor everithing works as it suppose to work.

Again, I confirm that in both cases of R and VBA my programs start working after I open Serial Monitor, send 1 to it (get reading) and close it. After this manipulation with Serial Monitor everithing works as it suppose to work.

Add DTR=ON to your mode command.

PaulS: Add DTR=ON to your mode command.

How can I do this if I address the COM port for read/write as a file?

pavelkh: I tied as you suggested to make 2 seconds delay after COM opening - did not solve the problem.

I did not suggest a 2 second delay. I suggested a very different strategy which works reliably. And I gave you working code to demonstrate it. Have you tried it? The only thing you need to change in the Python code is the reference to the serial port that is used on your PC.

...R

How can I do this if I address the COM port for read/write as a file?

That depends on HOW you are planning to "address the COM port for read/write as a file".

If you mean that you want to echo "crap" > COM10 then you need

mode COM10 9600,N,8,1 DTR=ON echo "crap" > COM10

PaulS: mode COM10 9600,N,8,1 DTR=ON echo "crap" > COM10

Thank you for pointing to the right direction. I found that DTR is set to ON automatically when the COM port is addressed. So it is not the issue.

The problem was difference in default COM settings in Windows and settings on Arduino. When I used Serial Monitor the settings are set to the right values by Serial Monitor and then VBA works fine. In VBA I did not change COM settings before addressing the port. So, now I did it and the problem was solved. I still do not know why I had the problem with R because I explicitly set COM settings there but I think it should be something similar. For now it works for me and I will mark the topic as solved.

To set COM port in VBA I used the following command: Call Shell("cmd.exe /S /C" & "COM10:9600,N,8,1", vbNormalFocus)

Thank you everyone.