How to write a script to send to windows serial ports

Written by jon guenther
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to write a script to send to windows serial ports
A serial port card for a computer. (serial port image by jimcox40 from Fotolia.com)

You can easily communicate in two ways with serial ports in the Windows operating system: VBScript or C#.NET. The VBScript method is most easily implemented utilising a third-party tool, but you can write the script yourself depending on your communication goals. Using C# has the advantage of utilising classes built into the .NET Framework, making scripts you write more powerful and secure.

Skill level:
Easy

Other People Are Reading

Things you need

  • Microsoft Visual C# 2010 Express Edition

Show MoreHide

Instructions

  1. 1

    Click the "Start" menu and then navigate to "All Programs."

  2. 2

    Navigate to the "Accessories" menu and click the "Notepad" icon.

  3. 3

    Type the following code in the text editor:

    Dim comp Name

    Do

    comp Name = inputbox("Enter the computer name:", "Prompt")

    Loop until comp Name <> ""

    ListPorts (compName)

    ' Subroutine to display info

    Sub ListPorts( comp Name )

    Dim objWMIService

    Dim collection

    Set objWmiService = GetObject("winmgmts:\" & comp Name & "\root\cimv2")

    Set collection = objWMIService.ExecQuery("SELECT * FROM Win32_SerialPort",,48)

    For Each objItem in collection

    Wscript.Echo "DeviceID: " & objItem.Description

    Wscript.Echo "MaxBaudRate: " & objItem.MaxBaudRate

    Wscript.Echo "ProviderType: " & objItem.ProviderType

    Next

    End Sub

  4. 4

    Save the file with the name "GetPorts.vbs" on your computer hard drive.

  5. 5

    Double-click the file to display in a message box the device description, max baud rate and provider type for each serial port on your computer.

  1. 1

    Click the "Start" menu and navigate to "All Programs."

  2. 2

    Navigate to "Microsoft Visual Studio 2010 Express" and click "Microsoft Visual C# 2010 Express."

  3. 3

    Click the "New Project" link on the Start Page and then click to highlight "Console Application."

  4. 4

    Enter a name of your choice in the text box provided and click "OK."

  1. 1

    Type the following at the top of the "Program.cs" file:

    using System.IO.Ports;

  2. 2

    Type the following code between the "{" and "}" brackets of the Main method:

    using (SerialPort sp = new SerialPort("COM1"))

    {

    sp.Open();

    Console.WriteLine("Baud rate: " + sp.BaudRate.ToString());

    Console.WriteLine("Parity: " + sp.Parity.ToString());

    Console.WriteLine("Port name: " + sp.PortName.ToString());

    Console.WriteLine("Stop bit: " + sp.StopBits.ToString());

    Console.ReadKey();

    }

  3. 3

    Click the "Save All" icon

  4. 4

    Click the green arrow button to run the program. The script should return a message similar to the following:

    Baud rate: 9600

    Parity: None

    Port name: COM1

    Stop bit: One

Tips and warnings

  • You can write Windows programs, as well, that use forms to manage serial ports, and send and receive messages from them.
  • For a third-party tool that can implement the VBScript method, see Resources.
  • Using VBScript to communicate with serial ports is much less secure than utilising managed code such as C# or C++.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.