PDA

View Full Version : Assistance Required with VB.net



malc-c
- 15th August 2010, 18:44
Guys, I'm looking for someone who has good knowledge of VB.net (2008 Express version). I've downloaded VB Express 2008 edition from MS but haven't really got a clue how to get an application to read and write via the RS232 port.

Designing a form was simples (the boxes are simple typed values on how I would like the end result to look)
http://micro-heli.co.uk/app.jpg

I've managed to send the values from the list of variables to a terminal app in MCS via the EasyPIC5 serial port, but can't get my head round any example from the web on serial communications.

If anyone can point me to some easy to understand tutorials, or would be willing to work with me via e-mail on this part of my project please feel free to drop me a PM.

mackrackit
- 15th August 2010, 18:59
This came up the other day and Darrel pointed to this
http://www.rentron.com/VisualBasic.htm
Maybe it works the same with VB Net?

malc-c
- 15th August 2010, 19:44
Thanks Dave,

I'll have a look at it in some detail in a moment, but on first glance it looks like VB6 rather than the (dot)net version. From my googling sessions it seems that earlier versions of VB had a serial comms module or connection to variables that is somewhat missing in the dotnet version !

amgen
- 15th August 2010, 20:01
nice interface you made.
just for general discussion,
I have made a vb.net form to rcv assorted stuff from pic, you may know more about microsoft .net than me, I have just read books and watched on-line learning vids.
as a start, you use a serial port object, a stream object, I used a timer to check for rcvd chars 10 or 50 times/sec then looped through data for a key-value situation. then displayed to appropiate box. was able to use a blue-tooth module set up as virtual com port to rcv just like a direct connect.
also, with serial port set up, it is easy to send stuff back to my PIC ckt as settings or commands. i have further ideas floating in my head (mabye sinking in my head) web stuff etc.
not sure if the forum wants this subject matter here but my initial interest was and is PIC interfacing .
don f
[email protected]

mackrackit
- 15th August 2010, 20:18
I moved this thread to the "Off Topic" area.
As long as this discussion is about interfacing to a MCU I think it is "OK" to discuss VB here.

tenaja
- 15th August 2010, 21:02
I moved this thread to the "Off Topic" area.
As long as this discussion is about interfacing to a MCU I think it is "OK" to discuss VB here.

Are there limits to the "off topic" topics?

malc-c
- 15th August 2010, 22:12
I could understand if I was after help with writing a VB application to read a database or something equally obscure, but this is related to sending and receiving data to and from a PIC, or more importantly how to formulate VB code and PBP code so that the two work together.

Don, what you have mentioned has given me some idea as to what I need. If you have any links to those tutorials it would be appreciated

mackrackit
- 15th August 2010, 22:31
Are there limits to the "off topic" topics?
It has always been my understanding that the "Off Topic" area is for forum "things", electronics questions, and any other "thing" not specific to PB or PBP but in a round about way related to PB or PBP or the forum.

gadelhas
- 15th August 2010, 23:06
Hi;

I think i can help, however you need to be more clear about your issue.
I I'll put here some code,

VB2008 Net Code;
First you need to add the serilport class to your project.


Public Class Form1
Dim WithEvents SerialPort1 As New IO.Ports.SerialPort

Then, at Form Load, you need to configure and open your SerialPort;


Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

SerialPort1.PortName = "COM1"
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.BaudRate = 19200
SerialPort1.DataBits = 8
SerialPort1.StopBits = 1
SerialPort1.Open()
SerialPort1.Write(Chr(13) & "Hugo Oliveira" & Chr(13))
End Sub

This handles the recetion from the buffer of the com port, and then i have a If statment to do the actions that need to be perfomed acording with the received data.


Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim recepcao As String

recepcao = SerialPort1.ReadExisting

If recepcao = "%TE" Then
GetCursorPos(rect)
SetCursorPos(rect.X - 1, rect.Y)
ElseIf recepcao = "%TD" Then
GetCursorPos(rect)
SetCursorPos(rect.X + 1, rect.Y)
ElseIf recepcao = "%TC" Then
GetCursorPos(rect)
SetCursorPos(rect.X, rect.Y - 1)
ElseIf recepcao = "%TB" Then
GetCursorPos(rect)
SetCursorPos(rect.X, rect.Y + 1)
ElseIf recepcao = "%TE1" Then
GetCursorPos(rect)
SetCursorPos(rect.X - 10, rect.Y)
ElseIf recepcao = "%TD1" Then
GetCursorPos(rect)
SetCursorPos(rect.X + 10, rect.Y)
ElseIf recepcao = "%TC1" Then
GetCursorPos(rect)
SetCursorPos(rect.X, rect.Y - 10)
ElseIf recepcao = "%TB1" Then
GetCursorPos(rect)
SetCursorPos(rect.X, rect.Y + 10)
ElseIf recepcao = "%TE2" Then
GetCursorPos(rect)
SetCursorPos(rect.X - 20, rect.Y)
ElseIf recepcao = "%TD2" Then
GetCursorPos(rect)
SetCursorPos(rect.X + 20, rect.Y)
ElseIf recepcao = "%TC2" Then
GetCursorPos(rect)
SetCursorPos(rect.X, rect.Y - 20)
ElseIf recepcao = "%TB2" Then
GetCursorPos(rect)
SetCursorPos(rect.X, rect.Y + 20)
End If
End Sub


With this piece of code in VB, you must with 100% sure, receive data from Com Port.

Where is a part of code, from sendinf the data out of the pic, in PBP. I use the Debug command, but you can use Hserout, if you are using te Uart of the PIC, or Serout, Serout2 command.


Main:
if JX>70 and JX<=100 then
DEBUG "%TE"
pause 20
endif

if JX>0 and JX<=70 then
DEBUG "%TE1"
pause 20
endif

if Jx>180 and Jx<210 then
DEBUG "%TD"
pause 20
endif

if Jx>=210 then
DEBUG "%TD1"
pause 20
endif
' JoyStick Eixo Y
' ================================================== ==================
if JY>70 and JY<=100 then
DEBUG "%TB"
pause 20
endif

if JY>0 and JY<=70 then
DEBUG "%TB1"
pause 20
endif

if Jy>180 and Jy<210 then
DEBUG "%TC"
pause 20
endif

if Jy>=210 then
DEBUG "%TC1"
pause 20
endif
' Axis - Eixo X
' ================================================== ==================
if Ax<100 then
DEBUG "%TE2"
pause 20
endif

if Ax>140 then
DEBUG "%TD2"
pause 20
endif
' Axis - Eixo Y
' ================================================== ==================
if Ay<110 then
DEBUG "%TB2"
pause 20
endif

if Ay>140 then
DEBUG "%TC2"
pause 20
endif
' Botões
' ================================================== ==================
IF BE = 0 then
DEBUG "%RL1"
pause 80
endif

IF BD = 0 then
DEBUG "%RD1"
pause 80
endif
goto Main



Hope it can hep.

sayzer
- 16th August 2010, 08:23
Here are two additions from me:



ComboBox1.Items.AddRange(SerialPort1.GetPortNames( ))


This way, you can get the available comports (port list) from your PC hardware directly into a combo box.

Then,


SerialPort1.PortName = ComboBox1.SelectedItem

This way, you can select a com port (if there is one).

Try all of these operations in try/catch so that if a comport is not available, you can prompt a message to the user.
Also, if there is comport but not selected in combobox, you can also catch it and prompt to the user.


----------------

malc-c
- 16th August 2010, 14:30
Guys, thanks for the assistance, I'll have a play when I get home from work

Much appreciate

LEDave
- 27th August 2010, 16:04
Hi,

I'm trying to send data from PIC - PC using the program mackrackit pointed to earlier in this thread.

http://www.rentron.com/receiving_data.htm

When I compile it I have this error message: Name 'comEvReceive' is not declared.

Does anyone have an idea of a fix (with as much of an explaination as you can spare please, I'm new to this). I've done lots of Googling with various answer from 'Select Case' to it can't be done in the 2008 VB-Version.


Private Sub MSComm1_OnComm()
Dim sData As String ' Holds our incoming data
Dim lHighByte As Long ' Holds HighByte value
Dim lLowByte As Long ' Holds LowByte value
Dim lWord As Long ' Holds the Word result

' If comEvReceive Event then get data and display
If MSComm1.CommEvent = comEvReceive Then

sData = MSComm1.Input ' Get data (2 bytes)
lHighByte = Asc(Mid$(sData, 1, 1)) ' get 1st byte
lLowByte = Asc(Mid$(sData, 2, 1)) ' Get 2nd byte

' Combine bytes into a word
lWord = (lHighByte * &H100) Or lLowByte

' Convert value to a string and display
lblRCTime.Caption = CStr(lWord)

End If
End Sub

I guess I should add I'm using Visual Basic 2008 Express Edition.

Any help much appreciated.

Dave

gadelhas
- 27th August 2010, 16:27
Hi,

I guess I should add I'm using Visual Basic 2008 Express Edition.

Dave

Hi,

The code posted is VB6 and you are using VB2008.
Try the code in the post 9, of this thread!

LEDave
- 9th September 2010, 00:19
Here's my attempt to modify the code from gadelhas's post above. My FORM has two push buttons, one opens the COM1 PORT the other closes it. I can't get it to show any data in the ListBox1 though from the PIC. The PIC code (shown below) outputs a RANDOM BYTE.

Any Ideas? Anyone?


Public Class Form1
Dim WithEvents SerialPort1 As New IO.Ports.SerialPort
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

SerialPort1.PortName = "COM1"
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.BaudRate = 2400
SerialPort1.DataBits = 8
SerialPort1.StopBits = 1
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If SerialPort1.IsOpen = False Then SerialPort1.Open()
If SerialPort1.IsOpen = True Then MsgBox("com1 port opened sucessfully")
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If SerialPort1.IsOpen = True Then SerialPort1.Close()
If SerialPort1.IsOpen = False Then MsgBox("com1 port closed sucessfully")

End Sub

Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim recepcao As String

recepcao = SerialPort1.ReadExisting



End Sub


Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
SerialPort1.Write(ListBox1.Text)

PIC code:


A VAR WORD
X VAR WORD
Y VAR WORD
Z VAR BYTE

main:
Random A
Y = A * 100
X = DIV32 25700
LET Z = X

SEROUT2 PORTC.3, 16780, [DEC z, 10, 13]
pause 4000
GOTO MAIN

LEDave
- 9th September 2010, 00:27
Here's another program which does receive the same data from the PIC. Only it displays it vertically instead of in BYTES across the ListBox. Anyone know how to modify this to display the BYTES properly?

This is how the data appears in the ListBox v's the serial Communicator which displays them properly.


VB has the numbers
1
3
5
*
*
2
0
3
*
And Serial Communicator has them
135
203

Here's the program, this maybe the one to adapt / work on as it's nearly there:


Imports System
Imports System.ComponentModel
Imports System.Threading
Imports System.Windows.Forms


Public Class Form1


'Buffer for receievd data***

Private Delegate Sub AddListBoxItemInvoker(ByVal item As Object)


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort1.PortName = "COM1"
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.BaudRate = 2400
SerialPort1.DataBits = 8
SerialPort1.StopBits = 1

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

If SerialPort1.IsOpen = False Then SerialPort1.Open()
If SerialPort1.IsOpen = True Then MsgBox("com1 port opened sucessfully")

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If SerialPort1.IsOpen = True Then SerialPort1.Close()

If SerialPort1.IsOpen = False Then MsgBox("com1 port closed sucessfully")

End Sub



Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

Me.AddListBoxItem(SerialPort1.ReadExisting)


End Sub

Private Sub AddListBoxItem(ByVal item As Object)

If Me.ListBox1.InvokeRequired Then
'We are on a secondary thread so delegation is required.
Me.ListBox1.Invoke(New AddListBoxItemInvoker(AddressOf AddListBoxItem), (item))

Else
'We are on the primary thread so add the item.
Me.ListBox1.Items.Add(item)
End If
End Sub
End Class

Thanks for any help.

Dave

HenrikOlsson
- 9th September 2010, 06:41
Hi Dave,
When you use something like this (and z is 135):

SEROUT2 PORTC.3, 16780, [DEC z, 10, 13]
The PIC actually sends 5 bytes, not one (or three). It sends "1", "3", "5", (CR), (LF) that's why your VB program displays them one by one (because you read them from the serial buffer one by one).

What is being sent is the ASCII codes for the digits 1,3,5 so what is actually being transmitted is 49, 51, 53, 10, 13 where 49 means the digit 1, 51 means the digit 3 and so on.

If you remove the DEC modifer what is being sent is three bytes ie. 135,10,13 but now it's likely the VB program will fill the listbox with a little c with a hook below it (the character that corresponds to ASCII code 135).

If you want the number (135) to be a "value" in VB (and not one or more text strings) then you need to read it as such - or convert it. My VB isn't up to speed (never has been) but I think there's something like


DIM myValue as Integer
myValue = SerialPort1.ReadByte
' OR
myValue = SerialPort1.ReadChar
Then you populate your listbox with something like

Me.AddListBoxItem(myValue.ToString)
In this case the ToString basically does for VB what the DEC modifer does for PBP, ie. it converts the value of 135 (a single byte) into the ASCII representation of each digit, "1", "3", "5".

My VB syntax may not be correct in this example but I think you get the gist of it.

/Henrik.

LEDave
- 9th September 2010, 09:09
Hi Henrik,

Thanks for that as ever.

Really good explanation there. I'll have a look at it this evening and try and get it working properly.

Again many thanks.

Dave

LEDave
- 9th September 2010, 22:49
Hi Henrik,

Success, but..........!

First of all the success. I changed the PIC code to:


SEROUT2 PORTC.3, 16780, [ z]

Then changed this line in the VB program:


Me.AddListBoxItem(SerialPort1.ReadExisting)

To:


Me.AddListBoxItem(SerialPort1.ReadByte)

And away it went reading and displaying Random Bytes, brilliant:)

Now for the but part. I actually want the VB program to read a WORD sent from the PIC (a short in VB?) however for the AddListBoxItem(SerialPort1.Readxxxx there is:

ReadByte
ReadChar
ReadExisting
ReadLine
ReadTo

No ReadShort:(

Now I'm thinking somehow it's send a High Byte from the PIC then a Low Byte and join them together in VB, unless there's a 'ReadShort' option.

Dave

HenrikOlsson
- 10th September 2010, 06:09
Hi Dave,

Nope, a word is two bytes and you have to treat like that.

The serial port object has a RecevieThreshold property that you can use to fire the Receive event only when so many characters has arrived. If you set it to 2 it will fire every time there's two bytes in the buffer, then you can do something like:

myHighByte = SerialPort1.ReadByte
myLowByte = SerialPort1.ReadByte

Then you have to put them back together with something like

Me.AddListBoxItem((myHighByte * 256 + myLowByte).ToString)

That's ONE way and it'll probably work if "all" your doing is sendning a word over at certain intervals.

/Henrik.

LEDave
- 10th September 2010, 08:51
Hi Henrik,

Thanks for that, Ill get on it tonight.

Good to get the program receiving and displaying Bytes correctly though.

Dave

LEDave
- 10th September 2010, 16:55
Hi Henrik,

Quick question:


The serial port object has a RecevieThreshold property

In VB6 it does but does it in VB2008?

Dave

HenrikOlsson
- 10th September 2010, 17:07
Hi,Mine does....but it's called ReceivedBytesThershold, by default it's set to 1.

I told you my syntax might be off ;-)

/Henrik.

LEDave
- 10th September 2010, 18:35
So does mine and I've set it to two:)

How did you copy yours and make it an attachment?

Dave

mackrackit
- 10th September 2010, 18:40
I like this
http://sourceforge.net/projects/greenshot/

LEDave
- 10th September 2010, 19:32
Me too :)

A really useful little program :)

Dave

LEDave
- 10th September 2010, 19:33
Not sure what happened then. I went to edit and ended up posting twice, sorry.

Dave

LEDave
- 10th September 2010, 23:27
Well the VB program now displays WORD size output, see attachment:)

I'm not sure about what I'm going to type next (nothing new there then they all shout;) ) But I'm outputting a Random WORD from the PIC, however do I need to send the WORD out as HighByte - LowByte so the VB program reads it correctly? Or is the WORD sent in the right order from the PIC (Byte-wise) for the VB program to read it properly.

As ever thanks for your help you two.

Almost forgot, my 'Greenshot' seems to have gone a bit mad on the resolution front, any ideas? (Correction the resolution on my pc has gone mad, it looks fine on here).


Dave

HenrikOlsson
- 11th September 2010, 08:28
Hi Dave,
You must send the WORD one byte at the time. Doing HSEROUT [myWORD] will only send the low byte.

If you send the high or the low byte first doesn't really matter - it's up to you - as long as you write the VB code to match.

As for the screenshot thing, Win7 have screencapture tool built in, you just drag a box around the area you want to capture and you're done.

LEDave
- 11th September 2010, 12:36
Hi Henrik,


If you send the high or the low byte first doesn't really matter - it's up to you - as long as you write the VB code to match.

That'll be an interesting exercise to do it both ways, Ill have a go.


As for the screenshot thing, Win7 have screencapture tool built in, you just drag a box around the area you want to capture and you're done.

I'm still on Xp. Interesting point / question, does PBP only work on a 32bit machine? Al ot of new laptops run 64bit so maybe PBP wouldn't work on one anyway?

Dave

mackrackit
- 11th September 2010, 12:39
One of my machines is Win7 64 bit.
No problems here.

HenrikOlsson
- 11th September 2010, 12:44
Hi Dave,
I don't think you should have any trouble figuring out how reverse the order of the bytes...

PBP runs fine on 64bit versions of Windows but you must use MPASM as the assembler, PM does not work. MicroCodeStudio needed a little convincing before it ran here, Charles Leo and Darrel helped me figure out what was going on and I see that Darrel wrote up a nice Wiki article about it too!

/Henrik.

LEDave
- 11th September 2010, 13:27
Hi Henrik,


PBP runs fine on 64bit versions of Windows but you must use MPASM as the assembler, PM does not work. MicroCodeStudio needed a little convincing before it ran here, Charles Leo and Darrel helped me figure out what was going on and I see that Darrel wrote up a nice Wiki article about it too!

Good to know. Ill have to track the article down.

Win7 and 64bit.....Mmm...I like the sound of that....One day;)

LEDave
- 11th September 2010, 18:26
Well I'll stick my neck out ans say it works:D

I made a word - Byte test program (code below) to test for a word and byte being sent from the PIC and received by the VB program.


DEFINE OSC 4

X VAR WORD
Y VAR WORD
Z VAR WORD
A var byte

main

For a = 1 to 10

let x = 65000
let y = 101
let z = 255


Serout2 PORTC.3,16780,[ x.HighByte, x.LowByte]
PAUSE 50
Serout2 PORTC.3,16780,[ Y.HighByte, Y.LowByte]
PAUSE 50
Serout2 PORTC.3,16780,[ z.HighByte, Z.LowByte]
pause 1500
next a
STOP

Here's what the VB Form received, what do you think?: