USB serial laptop to Arduino

Hi everyone,

I want to access data from my laptop through USB and then send it to my Arduino board.
For example, I want to get the ASCII whenever I press on the keyboard.

How can I do this?

If you want to send characters (ASCII) from your laptop to Arduino through serial port, you will need to write a program, in your language of choice, that runs on the laptop and streams out the characters.

Here is an example, in VBscript, of a program that reads from a text file and sends to COM/serial port:

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)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\TEXTFILE.TXT", ForReading)

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

   Do While objFile.AtEndOfStream <> True
      strChars = objFile.Read(10)
      com.Write(strChars)
      WScript.Sleep(200)
   Loop

objFile.Close
com.Close()

hello florinc,
i only want sent one character (for example "1") to serial port to switch a LED on or off. i have no idea of VBScript, is their a possibility not to work with a textfile and use a shorter, a more simply script. But i need a VBScript.

Thx a lot
Vansen

You could always use windows hyperterminal or similar program to send the characters you want.

Even better, just use the "Serial Monitor" built into the Arduino IDE

hello guys,

no, i need that way. i want to use windows speech recognition macro (with VBScript) to switch an LED on or off.
I know the serial monitor or putty, but i have to use VBScript.

Pls, if you know how, tell me the way to send only one charakter in a simple way to the serial port without using serial monitor or putty.

thx a lot

I modified the above VBscript for you, to send just one character, "1":

Const ForWriting = 2
Set fso = CreateObject("Scripting.FileSystemObject")
Set com = fso.OpenTextFile("COM7:9600,N,8,1", ForWriting)
MsgBox("Ready to write a character to COM")
com.Write("1")
com.Close()

that was a fast help ;9 thx BUT...

it didnt work. do i make a mistake? i tell you my way. i make a new txt-file, copy your code, and rename the file in serial.vbs

then i can start the file whereever i want? but i get an error. he cant open file in coulum 3, but i change the port to com16 - my port.

what is the mistake?

errorcode: 800A0035

Yes, use your port instead of the COM7 I used as example.
If you get an error ("cannot open" or such), try a "traditional" COM port (e.g. COM1), listed in the hardware profile.
Also, make sure the COM port you use is not already in use, by Hyperterminal, serial monitor or other terminal app.

I tested it on my Win XP machine and it seems to be working fine.

my first try was this code:

Sub TestCOMPort
  Const ForWriting = 2, TriStateFalse = 0
  Dim fso, f

  Set fso = CreateObject("Scripting.FileSystemObject")
  Set f = fso.OpenTextFile("COM16:9600,N,8,1", ForWriting, False, TriStateFalse)
'  Set f = fso.OpenTextFile("COM16:9600,N,8,1", 2)
'   Set f = fso.OpenTextFile("COM16:9600,N,8,1", true)

  ' Write data to the port
  f.Write Chr(49)
  f.Write "1"
  f.Write (1)
  f.Write ("1")
  f.Write Chr(32)
  f.Write Chr(27)
  f.Close
End Sub

i start the file by pressing enter, i got no error, but nothing happen on the serial port. its the same you wrote, it think. i didnt understand that.

errorcode is 800A0035

This is what the error means:
http://support.microsoft.com/kb/276011
Probably your COM16 does not exist. Can you find it listed in Device manager?

If I remove COM7 on my machine, I get the same error 0x800A0035 ("File not found").

As I said, as a syntax test, try COM1 instead of your COM16.

And why do you have COM16? Are you using a Bluetooth USB module?

ok, i have a lot of ideas i have to check:

first, i have a lenovo s205 laptop with win7 64 bit SP1. My Comport is 16. Dont no why, serial monitor say 16, a connection with PUTTY on Com16 ist successfully. i can switch an led on and of if i enter a "1" in the putty or serial monitor console.

  1. i wrote com1 in the file, the same error

  2. behind your link i found a reason (maybe):
    "FileSystemObject is not looking for the file in the folder that you had expected. By default, if you do not specify a path, FileSystemObject searches the WinNT\System32 folder."

-> your script works with winXp. is it possible, there are changes in win7 64bit? maybe the method to open a COMport isnt in System32 folder?

  1. i try the script now on my second win7 pc.

ok, i think number 3 is the best idea.

argh, on my second pc i have COMport 3, works fine with Serial Monitor and Putty.

but if i start the same script, i got error 800a0046. Access denied.

i tried it as admin and closed putty and serial monitor. mhhh.


now im back on my lenovo. i changed the com settings to COMport 2. Com 3 -15 ared still used.
i started the script again. now i got error 800a0046 too, but not 800a0035 anymore.

any ideas? pls help me

According to this article (at bottom of the page)

try using \.\COM16 instead of just COM16.

Hello, I am bumping the topic because it is exactly my problem.
I modified COM port to COM4 which I am using.
I used a .vbs script from this topic from EEPROM to outside file - Syntax & Programs - Arduino Forum
It successfully starts, prints the "start to read data from COM" message, creates results.txt file. But the file is empty...
If I turn the script with serial monitor on, I get a message of "Permission denied" which is correct as I saw in that read-only topic.
Serial print in my case prints data in the loop() function.

What can I do?