View Full Version : Serial Port in VBScript

07-31-2008, 11:50 AM
Hello again,

I'd like to know how to use VBScript to communicate through a serial port, say COM1, to move servos connected to an SSC-32 board. I saw that RoboRealm was recommended, but I want to go one step at a time: just to move a simple servo with VBScript.

Thanks! ;)

07-31-2008, 11:56 AM
I have a vague recollection of not being able to use the MSCOMM object in VBscript / VBA but you can substitute NETCOMM.ocx for this.. can anyone confirm this?

Sorry judge, I want to get this cleared before I'd jump in and try to help you out with something that doesn't work :happy:

07-31-2008, 01:28 PM
You could probably do something like:

Sub MyFunction()
Set MSComm1=CreateObject("MSCOMMLib.MSComm")
MSComm1.Settings = "9600,n,8,1"
MSComm1.CommPort = 1
MSComm1.InBufferCount = 0
MSComm1.PortOpen = True
If Err Then
MsgBox "COM" & MSComm1.CommPort & ": not opened, Sorry!"
Exit Sub
End If
MSComm1.Output="#1 P1500" & CHR(13)
MSComm1.PortOpen = False
Set MSComm1 = Nothing
End Sub

That's a snippet to point you in the right direction. It's not function-complete. Once the function is completed, that should center the servo on port 1 for an SSC with a 9600baud setting. The & CHR(13) is important. You must terminate commands to the SSC-32 with a <cr> character.

Does that help? I don't have time to write and test it at the moment, but is it enough of a pointer to get you going?

[edit] That should be code-complete. You could try that without too much risk. Change the baudrate as appropriate, I have mine set to 9600 for wireless use...

[edit again]
added simple error checking and closed comport afterwards...

07-31-2008, 01:39 PM
Since multiple people are in this thread, I'm adding this bump that I'll delete later. I edited the code, please refresh.

03-27-2009, 06:25 PM
I need to print whatever is on the screen of a kiosk when a button is pressed.

I was hoping to be able to do this in VBSCRIPT -- the code for printing what is on the screen is not the issue, it is "trigger" to launch the vbscript.

I assume I could somehow wire a button from radio shack up to an DB9 RS232 port and provide input that way? But I would have no clue where to begin...

any suggestions?

03-27-2009, 07:13 PM
Welcome to the forum!

Eeek! UARTS! I'd suggest, given it's a PC, that you save some extra headache and use the [dumdumDUM!] printer port if you have an extra available. :)

I'd do something like the circuit enclosed (but then, I've been building parallel port toys for close to thirty years(!)...)


I'm not vbScript expert by any stretch, but I think the code given here (http://www.activexperts.com/activmonitor/windowsmanagement/scripts/printing/printerport/#MonitorPrinterStatus.htm): should at least tell you what signal you want to read, and how to get at it through the printer driver. Otherwise, you need to twiddle bits directly.

Displays current status for all printers on a computer.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer")

For Each objPrinter in colInstalledPrinters
Wscript.Echo "Name: " & objPrinter.Name
Wscript.Echo "Location: " & objPrinter.Location
Select Case objPrinter.PrinterStatus
Case 1
strPrinterStatus = "Other"
Case 2
strPrinterStatus = "Unknown"
Case 3
strPrinterStatus = "Idle"
Case 4
strPrinterStatus = "Printing"
Case 5
strPrinterStatus = "Warmup"
End Select
Wscript.Echo "Printer Status: " & strPrinterStatus
Wscript.Echo "Server Name: " & objPrinter.ServerName
Wscript.Echo "Share Name: " & objPrinter.ShareName

If you *really* wanna use the serial port, than we can start talking about the Max232 chip and interfacing.

03-30-2009, 01:31 PM
With regards to Judge's original post, I am trying to accomplish the same thing. Output serial data through selected comport to SSC. I whipped up this form in VB Express 2008.

What I envision, are a series of buttons that will correspond to pre-made sequences courtesy of Lynxmotion Visual SEQ. Once I have that basic functionality working, I would like to be able to map the buttons to keyboard, gamepad or joystick. Included is a web browser window to connect to IP-cam and damage readout corresponding to hits registered from impact sensors (10 total or 10% till elimination). I also have a series of status leds whose purpose is still undetermined at this time.

I found a helpful post on Lynxmotion's forum from a user doin the same thing.
But, I'm having some problems adapting his code to my prog. Compile errors. So, while I muddle through, I thought I'd present this to anyone interested, for thoughts, critiques, or help. Feel free to adapt to your own purposes, as I release this into GPL.

Oh, btw, the Eject button triggers my death sequence. You know, just in case...:tongue:

04-03-2009, 10:54 PM
Using 'Beave's code as a starting point, I started writing a VB program to control my 8DOF mech CLYDE. I have successfully moved one leg on Button_4 using the code provided, but it only sends the first line of servo positions. I am not clear if the 'Sleep' command is a VB statement or a SSC command. Also, when I connect a second button control (Button6_Click) to the same serial port control, nothing is sent. (No blinky on SSC-32):(

Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub

Public Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
'Walk Forward LH, LT, LK, LA, speed
SerialPort1.Write("#28 P1500 #29 P1500 #30 P1500 #31 P1500 T1000" + Chr(13))
SerialPort1.Write("#28 P1573 29 P1292 #30 P1292 #31 P1427 T1000" + Chr(13))
SerialPort1.Write("#28 P1573 #29 P1708 #30 P1708 #31 P1427 T1000" + Chr(13))
SerialPort1.Write("#28 P750 #29 P750 #30 P750 #31 P750 T1000" + Chr(13))
End Sub

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
'Walk Backward LH, LT, LK, LA, speed
SerialPort1.Write("#28 P1500 #29 P1500 #30 P1500 #31 P1500 T1000" + Chr(13))
SerialPort1.Write("#28 P1573 29 P1292 #30 P1292 #31 P1427 T1000" + Chr(13))
SerialPort1.Write("#28 P1573 #29 P1708 #30 P1708 #31 P1427 T1000" + Chr(13))
SerialPort1.Write("#28 P750 #29 P750 #30 P750 #31 P750 T1000" + Chr(13))
End Sub
End Class Can anybody figure out what I'm doing wrong?

04-04-2009, 12:41 AM
Sleep is a VB command.. Causes a delay.. and it's set to 900. Which is 9/10 of a second.. You might try increasing it a tad since you have the speed set to 1000. (maybe 1100)

Thanks, Connor

04-04-2009, 03:12 AM
Hi there,

As Connor alluded to - sleep(900) is telling VB to sleep for 900ms. The T1000 command is telling the SSC32 to take 1000ms to complete the move you've requested. Is there a good reason for asking for every move to take 1000ms?

It doesn't look like you're looping, I don't think the event handler code is there (did you write the event handler?) - with as much as we can see, I suspect you press the button, good things happen, you fall out of the subroutine and that's it. Is that what you're observing?

01-04-2010, 05:56 AM
Thank you for the helpful info.

01-04-2010, 08:12 AM
Below is a simple way to write bytes to the com port using VBscript. VBcript operates somewhat like a batch file in that the comport is opened, the bytes are sent, and the port is closed. You generally can't receive bytes from the port using VBscript. Methods using MSCOMMLib would require it to be registered somewhere on the computer, usually by VB or similar.

Set fs=CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("COM1:",True)
a.write chr(255)
a.write chr(0)
a.write chr(127)