Technology Software

AVR Visual Basic Source Code Language Tutorial

    • 1). Connect an AVR development board to the PC via a serial cable. Make sure you have the AVR board powered up and running a program on the microcontroller to handle receiving or transmitting serial data.

    • 2). Instantiate a serial port object in VB. You'll need to instantiate the serial port with communication parameters prior to opening it and receiving or transmitting data. The following source code can be used to instantiate a serial port object using COM1 and 9600 baud:

      Imports System
      Imports System.IO.Ports

      Public Class SerialExampleForm
      Dim WithEvents Port As SerialPort = _
      New SerialPort("COM1", 9600, Parity.None, 8, StopBits.One)

    • 3). Transmit data to the AVR. The following subroutine transmits the text from a text box named "txtTransmitData" when a button named "btnTransmitData" is clicked. Make sure you have placed the textbox and the button on your form.

      Private Sub btnTransmitData_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles btnTransmitData.Click
      Port.Open()
      Port.Write(txtTransmitData.Text)
      Port.Close()
      End Sub

    • 4). Receive data from the AVR in VB. The form load subroutine ensures the serial port is open on the form load. The DataReceived subroutine takes a character read from the serial port and displays it in a text box called "TextBox1." Be sure your visual basic form includes a text box control called Texbox1. Also, to receive serial data, remember that the port must be open.

      Private Sub SerialExampleForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      CheckForIllegalCrossThreadCalls = False
      If Port.IsOpen = False Then Port.Open()
      End Sub

      Private Sub Port_DataReceived(ByVal sender As Object, ByVal e As
      System.IO.Ports.SerialDataReceivedEventArgs) Handles Port.DataReceived
      TextBox1.Text = ""
      TextBox1.Text = Port.ReadChar()
      End Sub

Leave a reply