Pages: [1]   Go Down
Author Topic: USB serial laptop to Arduino  (Read 1617 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

0
Offline Offline
Edison Member
*
Karma: 7
Posts: 1411
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
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()
Logged

Offline Offline
Newbie
*
Karma: 2
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Sr. Member
****
Karma: 5
Posts: 422
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

St. Augustine, FL
Offline Offline
Newbie
*
Karma: 1
Posts: 15
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 2
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Edison Member
*
Karma: 7
Posts: 1411
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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()
Logged

Offline Offline
Newbie
*
Karma: 2
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: September 21, 2012, 05:37:26 pm by Vansen » Logged

0
Offline Offline
Edison Member
*
Karma: 7
Posts: 1411
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Edison Member
*
Karma: 7
Posts: 1411
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 2
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

my first try was this code:
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
Logged

0
Offline Offline
Edison Member
*
Karma: 7
Posts: 1411
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Newbie
*
Karma: 2
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

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

3. 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?

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

ok, i think number 3 is the best idea.
Logged

Offline Offline
Newbie
*
Karma: 2
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: September 22, 2012, 07:15:18 am by Vansen » Logged

0
Offline Offline
Edison Member
*
Karma: 7
Posts: 1411
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

According to this article (at bottom of the page)
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
try using   \\.\COM16   instead of just COM16.
Logged

Pages: [1]   Go Up
Jump to: