vbs read COM port

Hey everyone,

As this is my first post I’ll try to keep it as short as possible to be as simple as possible.

I’m basically using the following code to read from the COM port:

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()

As for the arduino sketch I’m running something that outputs “Hello” every 2 seconds

void setup()
{
}

void loop()
{
 Serial.begin(9600); 
 delay(2000);
 Serial.println("hello");
 Serial.end();
}

But the problem is the vbs code gets stuck at the line

s = com.ReadLine

and just keeps reading the line, as if the line is infinitely long…

Any ideas?

and just keeps reading the line, as if the line is infinitely long...

Do you mean "hello" continues indefinately?

No what I mean is that the script simply keeps reading port and never executes the next line of code. Meaning that if I were to put MsgBox s after the line s = com.ReadLine I would never get a message box popping up since the ReadLine never ends…

Try this on the Arduino...

void setup()
{
 Serial.begin(9600); 
}

void loop()
{
 delay(2000);
 Serial.println("hello");
}

I tried that but it didn't make a difference. I think the problem is that vbs doesn't recognize where the line ends and simply thinks it's one big string. I even tried sending a CrLf at the end of a message but it makes no difference.

By the way I know that arduino is sending serial data because I checked with the hyperterminal

Instead of working from a FileSystemObject object, you might get better results from SerialPort object...

http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.readline.aspx

Or you could use tinkerproxy(serproxy) and create a socket connection, might work better.

I was able to do both COM and socket in VB.NET in VS Studio 2010.

Unless something has changed recently, VBS cannot read from a serial port.

Yeah if you can you can grab Studio express for free and make it in VB.Net. You can do way more.

Can VBS do socket connections? Probably not as its more for scripting.

Yeah if you can you can grab Studio express for free and make it in VB.Net.

Is it easier in .NET or VB6 with the MSCOMM object?

A while ago I used something called NETCommOCX to get comm with VBS. Not sure about it's current status, but might be worth a google.

larry_said:

Yeah if you can you can grab Studio express for free and make it in VB.Net.

Is it easier in .NET or VB6 with the MSCOMM object?

It super easy in VB.net to do serial, just google COM port in vb.net 2010. You just create the object and then you can send recieve. It doesn’t use MSCOMM anymore i think.

I’m also having problems with this VB Script under Windows 7.
It seems it never gets past the line

Do While com.AtEndOfStream <> True

As neither msgbox is executed in the following script.

Do While com.AtEndOfStream <> True
   MsgBox("In loop")  
  s = com.ReadLine
   objFile.WriteLine(s)
  WScript.Sleep(200)
Loop
MsgBox("Past Loop")

I know there are other ways of getting the serial, but would like to know what is wrong with this script.
Has anyone used it sucessfully.