Serial Port Using Visual Basic.NET and Windows



Similar documents
Serial Communications

Using Xbee in Serial Communication

How to setup a serial Bluetooth adapter Master Guide

RN-131-PICTAIL & RN-171-PICTAIL Evaluation Boards

RS-232 Communications Using BobCAD-CAM. RS-232 Introduction

PCMCIA 1 Port RS EDITION OCTOBER 1999

Programming Flash Microcontrollers through the Controller Area Network (CAN) Interface

USB Serial Adapter ICUSB232SM3. Instruction Manual. 1-Port 3ft USB to RS232 DB9 Serial Adapter Cable

1 Port FTDI USB to RS232 Null Modem Adapter

Advanced Data Capture and Control Systems

LDG Electronics External Meter Serial Communications Protocol Specification

Step-by-Step Tutorial For Using LinkSprite UART- WIFI Module Model LS_UART_WIFI

USA MOBILITY CABLE CONNECTION AND SOFTWARE DRIVER INSTALLATION INSTRUCTIONS. For. Unication M90 Device USB To RS232 Adapter TABLE OF CONTENTS

S88XPressNetLI v1.0. Installation, user and reference manual KDesign Electronics, PCB design by RoSoft

USB / Data-Acquisition Module NOW LEAD-FREE

Cypress CY7C64225 USB-to-UART Setup Guide Version 1.3

Computer Programming Software Start Guide

RS-485 Protocol Manual

Future Technology Devices International Ltd. Windows CE Installation Guide

Designing VM2 Application Boards

1-10 The USB PIC K150 microcontroller programmer Hardware version V2.0 File version V2.0 Product Image

RN-131-PICTAIL & RN-171-PICTAIL Web-Server Demo Application

Develop a Dallas 1-Wire Master Using the Z8F1680 Series of MCUs

Six-servo Robot Arm. DAGU Hi-Tech Electronic Co., LTD Six-servo Robot Arm

Installing PowerLink on Windows 7 64-bit

- 35mA Standby, mA Speaking pre-defined phrases with up to 1925 total characters.

DK40 Datasheet & Hardware manual Version 2

PL-2303 (Chip Rev H, HX, X) USB to Serial Adapter Windows Driver Installer Manual

Keep it Simple Timing

RN-WIFLY-EVAL-UM. WiFly Evaluation Kit Roving Networks. All rights reserved. RN-WIFLY-EVAL-UM Version 1.32r 10/9/2012 USER MANUAL

TCP/IP MODULE CA-ETHR-A INSTALLATION MANUAL

Installing the drivers for the Current Cost Data Cable

AN10811 Programming SPI flash on EA3131 boards Rev May 2009 Application note Document information Info Content Keywords Abstract

CENTRONICS interface and Parallel Printer Port LPT

RS232 Board datasheet

The Answer to the 14 Most Frequently Asked Modbus Questions

International Journal of Electronics and Computer Science Engineering 1588

MS830 / MS833 Barcode Reader v1.0 9-December-2014

Connecting and Setting Up Your Laptop Computer

Bluetooth HC-06 with serial port module Easy guide

Quick Note 32. Using Digi RealPort with a Digi TransPort Router. UK Support September 2012

Ocean Controls RC Servo Motor Controller

Bluetooth to serial HC-06 wireless module

Warnings: This manual is intended to guide a technicians or customers who would like to repair DBL's

RN-XV-RD2 Evaluation Board

Accurate Measurement of the Mains Electricity Frequency

Orbit PCI Mk 2 Network Card. User Manual. Part No Issue 4

Master-Touch and ValuMass. Modbus Communications. INSTRUCTION MANUAL (Rev. 2.1)

Microcontrollers and Sensors. Scott Gilliland - zeroping@gmail

EVAL-UFDC-1/UFDC-1M-16

Part 1. MAX BIT DAC with an Arduino Board. MIDI to Voltage Converter Part1

F2103 GPRS DTU USER MANUAL

WHQL Certification Approval...2 User Interface...3 SUNIX s COMLab..4

Future Technology Devices International Ltd

Fingerprint Based Biometric Attendance System

USB - FPGA MODULE (PRELIMINARY)

T3 Mux M13 Multiplexer

Programming the BetaBrite Classic LED electronic sign

Bluetooth Serial Adapter

TTL-232R-3V3 USB to TTL Serial Converter Cable

Running the R4 Software on a USB Port

Alcalde # 1822 Col. Miraflores C.P Guadalajara, Jal. Mexico MX 01 (33) y USA 001 (619) (San Diego, CA.

A+ Guide to Managing and Maintaining Your PC, 7e. Chapter 1 Introducing Hardware

Introduction: Implementation of the MVI56-MCM module for modbus communications:

LINDY ELECTRONICS LIMITED & LINDY-ELEKTRONIK GMBH - SECOND EDITION

BIT COMMANDER. Serial RS232 / RS485 to Ethernet Converter

ScanWin Installation and Windows 7-64 bit operating system

Fondamenti su strumenti di sviluppo per microcontrollori PIC

RS485 Adapters. User Manual October 26, 2008 V1.02 Copyright Light O Rama, Inc. 2006, 2007, 2008 SC485 USB485B USB485 USB485-ISO

NOW LEAD-FREE! VCCIO VCCIO RXLED TXLED DTR# 10K FTDI FT232R

USER GUIDE. Ethernet Configuration Guide (Lantronix) P/N: Rev 6

Design of Conveyor Monitoring Controller with Wireless System

Work with Arduino Hardware

Toshiba Serial Driver Help Kepware Technologies

RS-232 Baud Rate Converter CE Model 232BRC Documentation Number 232BRC-3903 (pn5104-r003)

Adafruit's Raspberry Pi Lesson 5. Using a Console Cable. Created by Simon Monk

Access Control Using Smartcard And Passcode

CAN bus board. EB018

ASSEMBLY PROGRAMMING ON A VIRTUAL COMPUTER

SVA025 Netcom2 FAQ Rev 1. Mitsubishi Netcom 2 FAQ

SUDT AccessPort TM Advanced Terminal / Monitor / Debugger Version 1.37 User Manual

USB232CONV Installation. Version Copyright IFTOOLS GbR

In-System Programmer USER MANUAL RN-ISP-UM RN-WIFLYCR-UM

ALL-USB-RS422/485. User Manual. USB to Serial Converter RS422/485. ALLNET GmbH Computersysteme Alle Rechte vorbehalten

USB to Serial Quick Installation Guide

Using a Laptop Computer with a USB or Serial Port Adapter to Communicate With the Eagle System

SAPIP GUI INSTALLATION. Table of Contents

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science Microprocessor Project Laboratory

Arduino Lab 1 - The Voltage Divider

Manual Serial PCI Cards

Disturbance Recoder SPCR 8C27. Product Guide

KTA-223 Arduino Compatible Relay Controller

Adafruit's Raspberry Pi Lesson 5. Using a Console Cable

To perform Ethernet setup and communication verification, first perform RS232 setup and communication verification:

L Firmware And FPGA Configuration Upgrade

Communication with BushingGard using computer USB port

APPLICATION PROGRAMMING INTERFACE

Serial Port Complete

Accessing I2C devices with Digi Embedded Linux 5.2 example on Digi Connect ME 9210

GPS DATABASE LOADING TECH HELP

Transcription:

Serial Port Using Visual Basic.NET and Windows Introduction The serial (COM) port is one of the simplest ways to communicate between a PC and a microcontroller circuit. Most microcontrollers have hardware serial ports and most microcontroller compilers have built-in functions to read from and write to the hardware port. Hardware serial ports with their 9-pin D connectors have disappeared from laptop and desktop computers, but are easily produced with a low-cost USB-to-serial cable adaptor. For embedded systems, a common approach is to add a FT232R USB-to-serial chip to the circuit so that the hardware connects to the PC though USB. Another option is to add a USB-to-serial module, for example the UM232R by FTDI or the USBMOD3 from DLP Design. These modules add glue circuits and a USB connector to the converter chip for an easy-to-use self contained solution. On the PC side, the USB adaptor appears as a virtual serial port that can be accessed by an application program just like a hardware COM port. On the microcontroller side, the adaptor appears as a standard serial port. If using an adaptor cable, the application circuit needs a convertor chip, for example the Dallas DS275 or the Maxim MAX233 to convert RS-232 serial levels to TTL logic levels. Circuits and code for serial port connections to a PICmicro are in the ME 8243 Boot Camp exercises. 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 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 text As String instance.write(text) The Write method can also write a byte array like this: Dim buffer As Byte() Dim offset As Integer Dim 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 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 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) 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)) 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 myserialport As New SerialPort Private combuffer As Byte() Private Delegate Sub UpdateFormDelegate() Private 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 Here is the function that will get triggered to update the display Private Sub UpdateDisplay() Label2.Text = CStr(comBuffer(0)) 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. Pardue, J., Virtual Serial Port Cookbook. 2. Axelson, J, Serial Port Complete, 2 nd ed. 3. Axelson J, Access Serial Ports with Visual Basic.NET, Nuts & Volts, April 2008, p 60. 4. Jan Axelson s web site: http://www.lvr.com/serport.htm 5. Microsoft serial port class documentation: http://msdn2.microsoft.com/library/30swa673(en-us,vs.80).aspx serial-port-vb.doc 5 2/1/2010