Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Visual Basic 2010 Express help needed

Status
Not open for further replies.

Embedded_Geek

Full Member level 6
Joined
Jul 5, 2010
Messages
340
Helped
58
Reputation
116
Reaction score
56
Trophy points
1,318
Location
Germany
Activity points
2,948
Can anyone tell me how to setup serial communication using Visual Basic 2010 Express.


Thanks in advance
Ajish
 

if you look in the toolbox under Components there should be a SerialPort object. You can set up baudrate etc and transmit and receive characters (use a thread for receiving)
 

Hi,

This is my standard code you can use it for other application..
Hope this help...

Code:
Public Class Form1
    Dim port_name As String()
    Dim i As Integer
    Dim j As Integer

    Dim send_data As String
    Dim input As Integer = 0
    Dim receive_data As String
    Dim Ctr As Control
    Dim BRx(256) As Byte

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Set up serialport 1

        SerialPort1.DataBits = 8
        SerialPort1.StopBits = IO.Ports.StopBits.One
        SerialPort1.Parity = IO.Ports.Parity.None

        'Set up Combobox comport 1
        cbb_comport.DropDownStyle = ComboBoxStyle.DropDownList
        cbb_baudrate.DropDownStyle = ComboBoxStyle.DropDownList
        cbb_stopbit.DropDownStyle = ComboBoxStyle.DropDownList
        cbb_parity.DropDownStyle = ComboBoxStyle.DropDownList
        cbb_databit.DropDownStyle = ComboBoxStyle.DropDownList
        cbb_flow.DropDownStyle = ComboBoxStyle.DropDownList

        cbb_comport.Sorted = True
        port_name = IO.Ports.SerialPort.GetPortNames()
        For Each comp1 In port_name
            cbb_comport.Items.Add(comp1)
        Next
        cbb_comport.SelectedIndex = 0
        'Set up combobox baudrate 1
        cbb_baudrate.Items.Add(2400)
        cbb_baudrate.Items.Add(4800)
        cbb_baudrate.Items.Add(9600)
        cbb_baudrate.Items.Add(14400)
        cbb_baudrate.Items.Add(19200)
        cbb_baudrate.Items.Add(38400)
        cbb_baudrate.Items.Add(56000)
        cbb_baudrate.Items.Add(57600)
        cbb_baudrate.Items.Add(115200)
        cbb_baudrate.Items.Add(128000)
        cbb_baudrate.Items.Add(256000)

        cbb_baudrate.SelectedIndex = 2
        'Set up combobox databit 1

        cbb_databit.SelectedIndex = 1




        btn_DisConnect.Enabled = False

    End Sub

    Private Sub btn_Connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Connect.Click
        Timer1.Enabled = True
        Timer1.Interval = 500
        SerialPort1.PortName = cbb_comport.SelectedItem
        SerialPort1.BaudRate = cbb_baudrate.SelectedItem
        SerialPort1.DataBits = cbb_databit.SelectedItem


        SerialPort1.Open()
        Me.SerialPort1.DiscardOutBuffer()           'clear output buffer
        Me.SerialPort1.DiscardInBuffer()            'clear input buffer
        Me.SerialPort1.RtsEnable = False
        Me.SerialPort1.DtrEnable = False

        btn_DisConnect.Enabled = True
        btn_Connect.Enabled = False

        '----------------------------------------

        Me.Text = "Serial Communication" & "    " & "(Connected)"
        Panel1.BackColor = Color.Lime


        cbb_comport.Cursor = Cursors.No

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        For Me.i = 0 To 255
            BRx(i) = 0
        Next
        receive_data = SerialPort1.BytesToRead
        If receive_data = 0 Then Exit Sub
        If receive_data > 256 Then
            receive_data = 256
        End If
        For Me.j = 1 To receive_data
            BRx(input) = SerialPort1.ReadByte
            txt_receive.Text = txt_receive.Text & Chr(BRx(input))
            input = input + 1
        Next
        input = 0
        Timer1.Enabled = False


        For Me.i = 0 To 255
            BRx(i) = 0
        Next
        Timer1.Interval = 500
        Timer1.Enabled = True

    End Sub

    Public Sub delay(ByVal wait As Long)
        Dim i As Long
        For i = 0 To wait
        Next i
    End Sub

    Private Sub btn_DisConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_DisConnect.Click
        SerialPort1.Close()
        Timer1.Stop()
        btn_DisConnect.Enabled = False
        btn_Connect.Enabled = True

        Panel1.BackColor = Color.Transparent

        btn_Connect.Enabled = True
        btn_DisConnect.Enabled = False
        cbb_comport.Enabled = True
        cbb_baudrate.Enabled = True
        cbb_databit.Enabled = True
        Me.Text = "MCU Firmware CheckSum" & "    " & "(Disconnected)"
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        txt_receive.Clear()
    End Sub

    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click

        send_data = 0
        For Me.i = 0 To Len(txtSend.Text) - 1
            send_data = Mid(txtSend.Text, 1 + i, 1)
            Me.SerialPort1.Write(send_data)
            Call delay(90000)
        Next i
    End Sub
End Class

Example VB program:

**broken link removed**
 

Attachments

  • Serial Communication.rar
    119.4 KB · Views: 158
Last edited:
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top