Go Down

Topic: Communication with PC using vbs (Read 5817 times) previous topic - next topic

90io89ui

Sep 02, 2015, 05:23 pm Last Edit: Sep 02, 2015, 06:27 pm by 90io89ui
Hi

I'm trying to have my arduino UNO communicate with my PC, sending data to the PC and store it in a text file, or read a text file from PC and send it to arduino. I found this VB script that seems to work well for everyone:

Code: [Select]
Const ForReading = 1
Const ForWriting = 2

Set fso = CreateObject("Scripting.FileSystemObject")
Set com = fso.OpenTextFile("COM6:9600,N,8,1", ForReading)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\results.txt", ForWriting, True)

MsgBox("Start to read data from COM")

Do While com.AtEndOfStream <> True
s = com.ReadLine
objFile.WriteLine(s)
WScript.Sleep(200)
Loop

objFile.Close
com.Close()


This one is for reading data from arduino and save it in to a text file, but it doesn't work for me. There are no data saved on the file at all. I checked my sketch on serial monitor and the data does get displayed on it. I use Serial.print to send my data, should I be using other command?

The second vbs is for writing data to arduino:

Code: [Select]
Const ForReading = 1
Const ForWriting = 2

'------------------------------------------------------------------------------------------------
' open USB serial port (COMx);
'
' If the serial monitor in Arduino IDE is open, you will get an "access denied" error.
' Just make sure that the serial monitor is closed (so bytes are not sent by the arduino board).
'------------------------------------------------------------------------------------------------

Set fso = CreateObject("Scripting.FileSystemObject")
Set com = fso.OpenTextFile("COM7:9600,N,8,1", ForWriting)


'---------------------------------------------
' read content of text file line by line;
' write line to COMx;
'---------------------------------------------

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\docs\quotes.txt", ForReading)

MsgBox("Ready to write file content to COM")

Do While objFile.AtEndOfStream <> True
'---------------------------------------------------------------------------------------------------------
' read 10 characters at a time; arduino serial buffer cannot take more than 32 characters;
' writing a character to eeprom takes about 11 ms (assuming that there is no serial.prints in the loop);
' therefore, after each batch of 10 chars sent to COM, we should wait no less than 110 ms;
' we use 200 to have a margin of safety;
'---------------------------------------------------------------------------------------------------------
strChars = objFile.Read(10)
com.Write(strChars)
WScript.Sleep(200)
Loop

objFile.Close
com.Close()

MsgBox("Finished writing to COM")



For this one the script gives out a error message "permission denied" when it try to open the serial port for writing. I have the serial monitor closed and I used a standard usb cable to connect my arduino to my PC, so I don't know why I don't have permission to open my serial port.

Any help is much appreciated, I realize this may not be a programming issue, so I apologize if I posted this at the wrong place


Robin2

I looked at your first program and I suspect the problem is that it does not allow time for the Arduino to reset after it opens the serial port.

Your program should open the serial port and wait for the Arduino to reset before doing anything else. If you get the Arduino to send a message from setup() such as Serial.println("Arduino is ready"); your program can wait until it receives that before sending anything.

Also, your PC program should keep the serial port open until you are completely finished with the Arduino.

This Python-Arduino demo illustrates what I am saying. If you edit it to give it the proper serial port reference it should work with Windows.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

90io89ui

I looked at your first program and I suspect the problem is that it does not allow time for the Arduino to reset after it opens the serial port.

Your program should open the serial port and wait for the Arduino to reset before doing anything else. If you get the Arduino to send a message from setup() such as Serial.println("Arduino is ready"); your program can wait until it receives that before sending anything.

Also, your PC program should keep the serial port open until you are completely finished with the Arduino.

This Python-Arduino demo illustrates what I am saying. If you edit it to give it the proper serial port reference it should work with Windows.

...R
Thank you Robin, I'll try your method on the first script. But I noticed that my Arduino doesn't reset itself when I run the script. I know that when the Serial monitor is opened from the Arduino IDE, I can see the reset on my LCD screen, but it doesn't happen for the VB script. Is that normal? or is it indicating that the COM port wasn't successfully opened at the first place?

Robin2

But I noticed that my Arduino doesn't reset itself when I run the script.
It's a long time since I used Windows. I think there is a setting for RTS or DSR that may need to be changed to cause the reset.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

8bitbacon

#4
Oct 15, 2017, 07:10 pm Last Edit: Oct 17, 2017, 02:55 am by 8bitbacon
Thanks first vbs script worked, doesn't work on all ports like COM20 but COM8 allows it to work.

If you still need the script to keep closing the serial while updating the file this is what I made, but your arduino strings need to always have a colon character at the beginning and at the end of the string it sends out to help the program make sure it grabs something useful.

here is the read_port.vbs scsript:

Const ForReading = 1
Const ForWriting = 2
Function start()
Set fso = CreateObject("Scripting.FileSystemObject")
Set com = fso.OpenTextFile("COM8:9600,N,8,1", ForReading)
s = com.ReadLine
If Ubound(split(s,":"))=2 Then
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\****\**\****\data_to_read.txt", ForWriting, True)
objFile.WriteLine(s)
objFile.Close
com.Close()
Else
com.Close()
End If
WScript.Sleep(100)
start()
End Function
start()
arduino mini woot

Go Up