Embedded_Geek
Full Member level 6
Can anyone tell me how to setup serial communication using Visual Basic 2010 Express.
Thanks in advance
Ajish
Thanks in advance
Ajish
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
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
Wow, I got this working to transmit and receive with my PIC. Thanks very much..Hi,
This is my standard code you can use it for other application..
Hope this help...