Ever Happen to you - Hey we need a program to send AT commands to a modem through the computer's serial port and store the responses to a text file and by the way we perfer a non-compiled language that will run on any windows computer.
So then I thought I know we will use Windows Scripting Host, anybody with a windows computer can run a .vbs file. Did some research, not much documentationon using serial port in a vbs file just some stuff about the MSComm32 - but you have register it ( just seemed annoying). Came across ActiveComport - now this is great. By installing ActiveComport, serial port programing becomes downright simple. So I write out the program and install ActiveComport - bam works perfect. And then discover you have to pay to use ActiveComport, of course there is no money to do that, so back to the drawing board.
Discovered this website http://www.hardandsoftware.net/, which has the NETCommOCX which is an ActiveX control that wraps the functionality of MSComm32.ocx. By the way check out the other cool stuff on the site, there are some neat thins there. NETCommOCX works perfect, and it is a free download with no restrictions. It does need to be installed on the computer you intend on using the program on.
So FYI, here is the program written in vbs, basically it needs two text files command.txt and out.txt already in the directory of the program. Command.txt contains one AT command per line, so the script loads each one into an array, then writes each command to the serial port. The program then waits 300ms for a response and writes the response to the out.txt file and to the screen. The commands are issued in an infite loop until the progam is stopped (Ctrl-c). The time to wait shoud be changed based on the device. There are more advanced ways to control the flow of the program, instead of waiting for specified time it can poll the serial port for data and pause until it recieves data.
program code, should be pretty self-explanatory (hopefully). The program is meant to be run with cscript - c:\cscript program.vbs
---------------------------------------------------
Set objComport = CreateObject( "NETCommOCX.NETComm" )
objComport.CommPort = 1
objComport.Settings = "9600,N,8,1"
objComport.InputLen = 0
objComport.PortOpen = True
wscript.echo "Port is open and ready"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("command.txt", 1)
i = 1
Do Until objTextFile.AtEndOfStream
Redim Preserve arrdata(i)
arrData(i) = objtextfile.ReadLine
i = i + 1
Loop
objTextFile.Close
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("out.txt", 2)
do while (1) ' wscript.stdin.atendofline
for j = 1 to (i-2)
str1 = arrdata(j)
wscript.echo str1
objComport.output = (arrdata(j)) & VBCR
wscript.sleep(300)
str = objComport.InputData
wscript.echo str
wscript.echo "------------"
objTextFile.Writeline ("command: " & arrdata(j))
objTextFile.Writeline (str)
objTextFile.WriteLine ("-------------------")
next
loop
objTextFile.close
objComport.PortOpen = False ' Close the port
Friday, November 2, 2007
Serial Port in WSH (vbs)
Posted by
ecore
at
4:11 PM
Labels: serial port, visual basic scripting vbs, windows scripting host wsh
Subscribe to:
Post Comments (Atom)
1 comments:
Hi, I was using the ActiveComport and it worked just fine for 30 day and I saw that NETCommocx nice thing and I was thinking if it would work with c++ Builder 6? if so, how?
ok, for what i saw I think it is not possible but I hope you can correct what I think.
Regards and nice blog. Truly helpful.
Post a Comment