Basic Serial Port Programming Windows
Microsoft visual basic rs 232 serial port free download - Serial Basic, Windows Standard Serial Communications Library for Visual Basic, Microsoft Visual Basic, and many more programs Navigation. Serial Port Programming using Visual Basic.Net for Beginners. Visual Basic.net is an easy to learn language from Microsoft for the windows platform.One of the cool features of Visual Basic.net is the ability to whip up good looking gui components with a few lines of code.
In this tutorial we will learn How to communicate with an external device like a microcontroller board or modem using the Serial port of a windows PC(Windows XP,7). The software is written using C language and communicates with the Serial Port using Win32 API. In Windows,Serial ports are named as COM1,COM2,COM3. Serial port programming on Visual Basic with Windows API. Dim i As Long For i = 1 To rd s = s & Hex (bRead (i)) & ' '. Next i MsgBox s close_and_exit: rc = CloseHandle (h) 'In VBA, always execute this call. Or you will receive the ERROR_ACCESS_DENIED next time then opening the port 'and you will need to reload Word/Excel/Access to free the port.
I'm helping out a friend with his Electrical Engineering project. He is building a device that will use a serial port to communicate to some software. On a windows platform (Win7), how would one read and write directly to a specific pin on the serial port? Is there an API that Windows exposes for this sort of thing?
Windows Serial Port Programming Example
MPelletier2 Answers
Windows Serial Port Programming Exa…
Yes, essentially you open a serial port device with a special name, such as COM1
, and read and write to it much as you would a file. The pins used will (naturally) be the serial transmit and receive pins.
If you want to control specific pins but not necessarily in a serial way, you might be better off working with a parallel port instead. The parallel port voltages are usually more friendly to TTL level logic and can often be driven directly.
Update: If you just need to toggle one pin as per your comment, you may be able to use the DTR line for this. See the EscapeCommFunction
function documentation for how to do this.
You can use WaitCommEvent function to monitor a specific pin. Suppose the voltage change triggers CTS signal, it can be like this
There are also pistol cases for pistols and shotgun cases for shotguns. These cases are tweaked to make them slightly different from a rifle case. For example, it can keep your rifle from getting into the hands of a child.
Not the answer you're looking for? Browse other questions tagged c++cwinapiserial-port or ask your own question.
Checking the Virtual Serial Port Connection Check that device appears as a COM port on the PC by using the Device Manager. Right-click on My Computer > Properties > Hardware > Device Manager > Ports (COM & LPT). Should see something like PL2303 COM Port (COM6), which means the cable is showing up as COM port number 6. Your port number may be different. The port number will change if you plug the cable into a different USB port on your computer. For quick access to the Device Manager: Start -> Run -> devmgmt.msc The simplest way to test a serial connection is to connect to a PC terminal program. In the terminal program, set to use the COM port that connects to the adapter cable. Set the baud rate to the rate specified in the PICmicro program. For PC terminal program, use TeraTerm (ver 4.64 or later, http://en.sourceforge.jp/projects/ttssh2/releases/ ). Open TeraTerm, attach to serial port at correct baud rate, then save session into teraterm.ini.
Software Examples Example code in this document was written on the PC side in Visual Basic 2008 Express Edition and on the PIC chip microcontroller side in C using the CCS compiler.
Bare-Minimum VB.net Code The serial port functions are in the .NET System.IO.Ports library.
serial-port-vb.doc
1
2/1/2010
At top of form, preceding Module or Class statement, add: Imports System.IO.Ports
At top of form, under Class statement, create serial port instance: Private mySerialPort As New SerialPort
In code, set properties in a setup procedure that is called at form load time, like this: Private Sub CommPortSetup() With mySerialPort .PortName = 'COM10' .BaudRate = 34800 .DataBits = 8 .Parity = Parity.None .StopBits = StopBits.One .Handshake = Handshake.None End With End Sub
Open the serial port in the setup procedure and use Try/Catch handling to deal with errors: Try mySerialPort.Open() Catch ex As Exception MessageBox.Show(ex.Message) End Try
Sending data is done with the Write or the WriteLine methods. The Write method writes a string like this: Dim instance As SerialPort Dim text As String instance.Write(text)
The Write method can also write a byte array like this: Dim Dim Dim Dim
instance As SerialPort buffer As Byte() offset As Integer count As Integer
instance.Write(buffer, offset, count)
where buffer is the data array, offset is where the write should start (set to 0 to start at the beginning) and count is the number of bytes to write.
serial-port-vb.doc
2
2/1/2010
The WriteLine method writes the specified string and appends a NewLine (0Ah) value. Use like this: Dim instance As SerialPort Dim text As String instance.WriteLine(text)
More Sophisticated VB.NET Code A function to call at form load that initializes a serial port. The Try-Catch surrounds the port open function to flag system errors, for example that the port does not exist. Private Sub CommPortSetup() With mySerialPort .PortName = 'COM10' .BaudRate = 38400 .DataBits = 8 .Parity = Parity.None .StopBits = StopBits.One .Handshake = Handshake.None End With Try mySerialPort.Open() Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub
A code snippet to fill a string array with the names of the valid ports Dim myPortNames() As String myPortNames = SerialPort.GetPortNames
A procedure to send a frame in cmd,data format out the port. Private Sub SendSlave(ByVal sendCmd As Byte, ByVal sendData As Byte) 'send command to slave PICmicro in form of cmd, data Dim buffer(2) As Byte buffer(0) = sendCmd buffer(1) = sendData mySerialPort.Write(buffer, 0, 2) End Sub
To send one byte, use this. Dim buffer() As Byte = {1} mySerialPort.Write(buffer, 0, 1)
To read one line (blocking). Dim returnValue As String returnValue = mySerialPort.ReadLine
To read one byte (blocking). Dim returnValue As Integer returnValue = mySerialPort.ReadByte
serial-port-vb.doc
3
2/1/2010
To read several bytes. Buffer is where the data is stored, set offset = 0 to start at the beginning, count is the number of bytes to read, returnValue is the number of bytes read. Dim buffer As Byte() Dim offset As Integer Dim count As Integer Dim returnValue As Integer returnValue = mySerialPort.Read(buffer, offset, count)
For receiving data at unexpected times, use the DataReceived event. This is a bit tricky because it runs in a different thread and requires a handler. (Virtual Serial Port Cookbook, Chapter 9 for details.) . Create a procedure for the data received event, like this. Private Sub mySerialPort_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) 'Handles serial port data received events Dim n As Integer = mySerialPort.BytesToRead Dim comBuffer As Byte() = New Byte(n - 1) {} mySerialPort.Read(comBuffer, 0, n) Console.WriteLine(comBuffer(0)) End Sub
In the form load procedure, add a handler that points the data received event to the name of the procedure that does the work, using this line AddHandler mySerialPort.DataReceived, AddressOf mySerialPort_DataReceived
The handler runs in a different thread, which means it cannot directly access controls on the form. To get around this, use a delegate to pass data to the form. Add these lines to the public area Private Private Private Private
mySerialPort As New SerialPort comBuffer As Byte() Delegate Sub UpdateFormDelegate() UpdateFormDelegate1 As UpdateFormDelegate
Change the data received procedure to look like this Private Sub mySerialPort_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) 'Handles serial port data received events UpdateFormDelegate1 = New UpdateFormDelegate(AddressOf UpdateDisplay) Dim n As Integer = mySerialPort.BytesToRead 'find number of bytes in buf comBuffer = New Byte(n - 1) {} 're dimension storage buffer mySerialPort.Read(comBuffer, 0, n) 'read data from the buffer Me.Invoke(UpdateFormDelegate1) 'call the delegate End Sub
Here is the function that will get triggered to update the display Private Sub UpdateDisplay() Label2.Text = CStr(comBuffer(0)) End Sub
serial-port-vb.doc
4
2/1/2010
Odds and Ends • • • •
See MovingBar VB.NET code for receiving 2-byte frame See schematic files for Generic PIC Chip circuits for hardware examples and connector pinouts. When wiring to a DB-9F connector, wire to pins 2, 3 and 5 only. See the Axelson N&V, April 2008 article for retrieving names of serial ports using GetPortNames method.
References 1. 2. 3. 4. 5.
Pardue, J., Virtual Serial Port Cookbook. Axelson, J, Serial Port Complete, 2nd ed. Axelson J, Access Serial Ports with Visual Basic.NET, Nuts & Volts, April 2008, p 60. Jan Axelson’s web site: http://www.lvr.com/serport.htm Microsoft serial port class documentation: http://msdn2.microsoft.com/library/30swa673(en-us,vs.80).aspx
serial-port-vb.doc
5
2/1/2010