One of the frequently asked question on the Arduino forum is "how can Arduino save data to a file?" (see
this thread for example). The quick answer is this: Arduino sends data over the serial port to the PC, which, in turn, is running an executable that reads the serial port. Every byte of data (or ASCII character) received is then saved to a file. So, this mechanism has two parts:
1. a piece of the sketch, running on Arduino, will send data using the function Serial.print();
2. a piece of code (executable, script etc) running on PC, will receive data from the serial port.
An example of VBscript that does just that is shown below. The characters read from COM6 (baud rate 9600, 8 bits, no parity, 1 stop bit) are saved to file "C:\results.txt".
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()
A similar approach can be used for reading characters from a file.
The following is an example VBscript that reads the characters from a text file ("C:\docs\quotes.txt") and sends them on port COM7(9600,N,8,1) to Arduino. I use it to load text messages (quotations) to an external EEPROM (24LC256).
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")
In order to use the above scripts, copy the code, paste it into a text file and name it with the extension "vbs". Just by double clicking on the file, Windows will invoke wscript.exe, which will then execute the VBscript code.
NB: The above code was formatted by http://formatmysourcecode.blogspot.com/