It is very simple to get VB2008 and PicBasic Pro talking to one another. Here is an example of one direction communication to get you started.
PicBasic Pro Code:
Written for a PIC16F628 running at 20MHz with 2 LEDs for diagnostics, and a MAX232 to drive the serial I/O
@ DEVICE HS_OSC, LVP_OFF, MCLR_OFF, WDT_OFF
DEFINE OSC 20
'***************************
' Alias
'***************************
LED_Pow VAR PORTB.0 'Power LED. Shows code is running
LED_Stat VAR PORTB.1 'Status LED. Blinks each time data is sent
IO_Tx VAR PORTA.3 'Serial Out to MAX232
'***************************
' Constants
'***************************
IO_Baud CON 6 'Connection parameters: 9600 8,N,1
Init:
HIGH LED_Pow 'Code is running
LOW LED_Stat
Main_Loop:
'Transmit a string roughly every two seconds.
SEROUT IO_Tx, IO_Baud, ["BLIP", 10, 13]
HIGH LED_Stat
PAUSE 250
LOW LED_Stat
PAUSE 1750
GOTO Main_Loop
This will output the string "BLIP" about every two seconds from the PIC.
Now some .NET code to retreive the data:
Start by adding a listbox to your main form and name it lstData. This will display the data as it is collected.
Add a button named butConnect. This will open your serial port to start receiving data.
Add a button named butDisconnect. This will close the serial port.
Imports System.IO
Imports System.IO.Ports
Imports System.Net
Imports System.Data
Imports System
Imports System.Windows.Forms
Imports System.Threading
Imports System.Text
Public Class mMain
Dim WithEvents serPort As New IO.Ports.SerialPort
Dim myStringBuilder As New StringBuilder
Private Sub mMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
serPort.PortName = "COM1" 'Set to whichever COM port you will be listening to
serPort.BaudRate = 9600
serPort.DataBits = 8
serPort.Parity = Parity.None
serPort.StopBits = StopBits.One
butConnect.Enabled = True
butDisConnect.Enabled = False
End Sub
Private Sub butConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butConnect.Click
If (serPort.IsOpen = False) Then
Try
serPort.Open()
lstData.Items.Clear()
myStringBuilder = New StringBuilder 'Clear any old data
butConnect.Enabled = False
butDisConnect.Enabled = True
Catch ex As Exception
'Routine to process exceptions like port already open etc.
msgbox("Exception error")
End Try
End If
End Sub
Private Sub butDisConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butDisConnect.Click
If (serPort.IsOpen = True) Then
serPort.Close()
butDisConnect.Enabled = False
butConnect.Enabled = True
End If
End Sub
Private Sub serPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles serPort.DataReceived
Try
myStringBuilder.Append(serPort.ReadExisting())
Me.Invoke(New EventHandler(AddressOf update_Data))
Catch ex As Exception
MsgBox("Data Error")
End Try
End Sub
Private Sub update_Data()
lstData.Items.Add(myStringBuilder.ToString)
myStringBuilder = New StringBuilder 'Flush the old data
End Sub
End Class
There are some imports at the top of the code that aren't really needed. They're just a generic starting point that I use. Obviously, for a proper application, there would need to be a lot more error handling added. This is just a starting point on getting going using the serial port in .NET.
From here you could add a status bar to the form and keep a track of how long you have been connected and perhaps the number of packets you've received. Then you can move on to transmitting data from the PC to the PIC.
Hope this is of use to you.
regards,
Sherm
Posted on: October 14, 2008, 11:47:08 11:47 - Automerged
generally in control systems we use Vb6 cause its easier smaller and requires no special hw or sw
unless some complicated .net application is required
... or you'll use .NET if you want menus to display nicely under Windows XP
I was once loathe to make the transition from VB6 to .NET. I've found over time that the benefits of .NET and the IDE are more than those of VB6 in most circumstances for myself. I respect that other people will have different needs, and I still prefer VB6 from a simplicity point of view. VS2008 has grown on me though