VB6 simple code

alexthefox

Senior Member
hi,
i need help to have a comunication pc<-->picaxe by VB6.
i saw some example under VB.net, but i whant to work under VB6.
any help pls?
thk
 

moxhamj

New Member
This code probably does more than you need, but it shows how to send out data from the PC and get back packets from a picaxe:

Picaxe code:



'
' -------
' output
' PC pin 3 D25 pin 0|28X |
' --------<----------o------- | pin 0 input
' | | |-----
' 1K | | |
' yellow ------- |
' |
' |
' |
' |
' 22k/10k download circuit |
' |
'PC pin 2 D25 |\ |\ 914 | Bus
' -->-------| >o-----| >o--o---|>|------o-------------------------------------------
' |/ |/ | 2 inverters then red led
' 1K
' yellow

' postbox 28X
' input 0 onto common bus (pin 11) reads the bus and waits for a Dump command
' output 0 to PC (pin 21)
' use a 28X as bigger data memory though could also use a 08M as program fairly short and an 18A would be the best
' protocol for common bus - 3 commands - DataOut DataIn and Dump
' DataOut is Data/T, DataIn is Data/I and Dump is Data/D
' PC sends DataOut to data output devices on the bus
' Devices that read data put DataIn data on the bus
' The PC sends a Dump request to the bus which the postbox dumps all its data to
' Each device on the bus has a unique number 0-31
' each DataIn is a max of 4 bytes
' if need more than 4 bytes use two addresses eg 17 and 18 and send 4 bytes for each - ie create a psuedo device
' DataOut is also max 4 bytes
' All data packets must be the same length eg Data+1+1+4=9 bytes
' Data/I/0-31/b3,b4,b5,b6,b12,b13 where I=b1 and 0-31=b2 eg devices that read data send serout 1,N1200,("I",b2,b3,b4,b5,b6,+checksum)
' Data/T/0-31/b3,b4,b5,b6,b12,b13 sent by PC
' Data/D/0-31/b3,b4,b5,b6,b12,b13 - dummy values in b1-b6 but put there because all the same length sent by PC
' The postbox responds to a dump with D/0 or D/255/then 128 bytes (ie only send data if data has changed)
' keep the message short back to the PC if no change, so can go back to listening to the bus (so doesn't miss anything)
' run data at 1200 baud - not sure if can go several hundred metres at faster rates
start: For b0 = 1 To 100
serout 0, N1200, ("Startup")
Next

main: serin 0, N1200, ("Data"), b1, b2, b3, b4, b5, b6, b10, b11 ' b1="D" "I" or T, b2=device, b3-6=data, b7,8=checksum LSB 1st
If b1 = "D" Then ProcessDumpPeek
If b1 = "I" Then ProcessInputPoke
GoTo main

ProcessInput: ' b9=set to 1 to indicate got some new data
Let w6 = b3
Let w6 = w6 + b4
Let w6 = w6 + b5
Let w6 = w6 + b6
If w5 <> w6 Then main ' checksum is invalid
Let b2 = b2 * 4 ' sets address
write b2,b3
Let b2 = b2 + 1
write b2,b4
Let b2 = b2 + 1
write b2,b5
Let b2 = b2 + 1
write b2,b6
Let b9 = 1
GoTo main

ProcessDump: If b9 = 0 Then NoChange ' if no inputs received then send this back
Let b1 = 255
serout 0, N1200, ("Y")
' short pause for VB to catch up
Let w5 = 0 ' checksum
pause 200
For b1 = 0 To 127
read b1, b2
serout 0, N1200, (b2)
w5 = w5 + b2
Next b1
serout 0,N1200,(b10,b11) ' checksum out
Let b9 = 0 ' reset change pointer
GoTo main

NoChange: Let b1 = 0
pause 200
serout 0, N1200, ("N")
GoTo main


ProcessInputPoke: ' rewritten as there are a limited number of writes before the emprom dies
' has less registers =80 to 127, the old code had 127 , new dump code has dummy bytes
Let w6 = b3
Let w6 = w6 + b4
Let w6 = w6 + b5
Let w6 = w6 + b6
If w5 <> w6 Then main ' checksum is invalid
Let b2 = b2 * 4 ' sets address
If b2 < 45 Then ' make sure 0-47 only otherwise wierd bugs if write to other registers
b7 = 80 ' hex $50 = start of registers
b2 = b2 + b7
poke b2, b3
b2 = b2 + 1
poke b2, b4
b2 = b2 + 1
poke b2, b5
b2 = b2 + 1
poke b2, b6
End If
Let b9 = 1
GoTo main

ProcessDumpPeek: ' replaces code above
If b9 = 0 Then NoChange ' if no inputs received then send this back
Let b1 = 255
serout 0, N1200, ("Y")
' short pause for VB to catch up
Let w5 = 0 ' checksum
pause 200
For b1 = 80 To 127
peek b1, b2
serout 0, N1200, (b2)
w5 = w5 + b2
Next b1
' sent 48 bytes, send dummy ones now
b2 = 0
For b1 = 0 To 79
serout 0, N1200, (b2)
Next
serout 0,N1200,(b10,b11) ' checksum out
Let b9 = 0 ' reset change pointer
GoTo main


And VB.6 code:

Sub ReadDataFromPostbox(BaudRate As Integer, CommPort As Integer, Timeout As Integer, ReturnFlag As String)
' use baudrate 1200 commport=2 and timeout=5
Dim i As Integer
Dim ReadString As String
Dim LineOfText As String
Dim StartTime As String
Dim RequestString As String
Dim AnyNewData As String
Dim AsciiChr As String
Dim Checksum As Long
Dim DataValue As Integer
Dim ReadChecksum As Long
Dim DeviceNo As Integer
Dim ByteNo As Integer
On Error GoTo ReadDataError
DeviceNo = 0: ByteNo = 0
MSComm1.CommPort = Str(CommPort)
MSComm1.Settings = Trim(Str(BaudRate)) + ",n,8,1"
MSComm1.RThreshold = 1
MSComm1.InputLen = 1
MSComm1.PortOpen = True
StartTime = Now
ReadString = ""
RequestString = "DataD2345678": ' Data+D plus dummy bytes
MSComm1.Output = RequestString
Do
If MSComm1.InBufferCount >= 1 Then Exit Do: ' got enough bytes
i = DateDiff("s", StartTime, Now)
If i > Timeout Then ReturnFlag = "Timeout": Exit Do
DoEvents
Loop
' sends back "Y" or "N" to indicate if any data
AnyNewData = MSComm1.Input
If AnyNewData = "Y" Then
'now read 128 bytes plus two checksum bytes
Do
If MSComm1.InBufferCount >= 130 Then Exit Do: ' got enough bytes
i = DateDiff("s", StartTime, Now)
If i > Timeout Then ReturnFlag = "Timeout": Exit Do
Loop
Checksum = 0
For i = 0 To 129
AsciiChr = MSComm1.Input
DataValue = Val(Asc(AsciiChr))
Select Case i
Case Is <= 127: Checksum = Checksum + DataValue
PostboxValues(DeviceNo, ByteNo) = DataValue
ByteNo = ByteNo + 1
If ByteNo = 4 Then ByteNo = 0: DeviceNo = DeviceNo + 1
Label54.Caption = Str(PostboxValues(1, 0)) + " " + Str(PostboxValues(1, 1)) + " " + Str(PostboxValues(1, 2)) + " " + Str(PostboxValues(1, 3))
Label25.Caption = Str(PostboxValues(5, 0)) + " " + Str(PostboxValues(5, 1)) + " " + Str(PostboxValues(5, 2)) + " " + Str(PostboxValues(5, 3))
Case 128: ReadChecksum = DataValue
Case 129: ReadChecksum = ReadChecksum + DataValue * 256
End Select
If ReadChecksum = Checksum Then ReturnFlag = "Checksum Valid" Else ReturnFlag = "Checksum Invalid"
Next
Else
' no data to read
If ReturnFlag <> "Timeout" Then ReturnFlag = "No new data"
End If
MSComm1.PortOpen = False
Exit Sub
ReadDataError: If MSComm1.PortOpen = True Then MSComm1.PortOpen = False: ReturnFlag = "Error"
End Sub
 

alexthefox

Senior Member
This code probably does more than you need, but it shows how to send out data from the PC and get back packets from a picaxe:

Picaxe code:



'
' -------
' output
' PC pin 3 D25 pin 0|28X |
' --------<----------o------- | pin 0 input
' | | |-----
' 1K | | |
' yellow ------- |
' |
' |
' |
' |
' 22k/10k download circuit |
' |
'PC pin 2 D25 |\ |\ 914 | Bus
' -->-------| >o-----| >o--o---|>|------o-------------------------------------------
' |/ |/ | 2 inverters then red led
' 1K
' yellow

' postbox 28X
' input 0 onto common bus (pin 11) reads the bus and waits for a Dump command
' output 0 to PC (pin 21)
' use a 28X as bigger data memory though could also use a 08M as program fairly short and an 18A would be the best
' protocol for common bus - 3 commands - DataOut DataIn and Dump
' DataOut is Data/T, DataIn is Data/I and Dump is Data/D
' PC sends DataOut to data output devices on the bus
' Devices that read data put DataIn data on the bus
' The PC sends a Dump request to the bus which the postbox dumps all its data to
' Each device on the bus has a unique number 0-31
' each DataIn is a max of 4 bytes
' if need more than 4 bytes use two addresses eg 17 and 18 and send 4 bytes for each - ie create a psuedo device
' DataOut is also max 4 bytes
' All data packets must be the same length eg Data+1+1+4=9 bytes
' Data/I/0-31/b3,b4,b5,b6,b12,b13 where I=b1 and 0-31=b2 eg devices that read data send serout 1,N1200,("I",b2,b3,b4,b5,b6,+checksum)
' Data/T/0-31/b3,b4,b5,b6,b12,b13 sent by PC
' Data/D/0-31/b3,b4,b5,b6,b12,b13 - dummy values in b1-b6 but put there because all the same length sent by PC
' The postbox responds to a dump with D/0 or D/255/then 128 bytes (ie only send data if data has changed)
' keep the message short back to the PC if no change, so can go back to listening to the bus (so doesn't miss anything)
' run data at 1200 baud - not sure if can go several hundred metres at faster rates
start: For b0 = 1 To 100
serout 0, N1200, ("Startup")
Next

main: serin 0, N1200, ("Data"), b1, b2, b3, b4, b5, b6, b10, b11 ' b1="D" "I" or T, b2=device, b3-6=data, b7,8=checksum LSB 1st
If b1 = "D" Then ProcessDumpPeek
If b1 = "I" Then ProcessInputPoke
GoTo main

ProcessInput: ' b9=set to 1 to indicate got some new data
Let w6 = b3
Let w6 = w6 + b4
Let w6 = w6 + b5
Let w6 = w6 + b6
If w5 <> w6 Then main ' checksum is invalid
Let b2 = b2 * 4 ' sets address
write b2,b3
Let b2 = b2 + 1
write b2,b4
Let b2 = b2 + 1
write b2,b5
Let b2 = b2 + 1
write b2,b6
Let b9 = 1
GoTo main

ProcessDump: If b9 = 0 Then NoChange ' if no inputs received then send this back
Let b1 = 255
serout 0, N1200, ("Y")
' short pause for VB to catch up
Let w5 = 0 ' checksum
pause 200
For b1 = 0 To 127
read b1, b2
serout 0, N1200, (b2)
w5 = w5 + b2
Next b1
serout 0,N1200,(b10,b11) ' checksum out
Let b9 = 0 ' reset change pointer
GoTo main

NoChange: Let b1 = 0
pause 200
serout 0, N1200, ("N")
GoTo main


ProcessInputPoke: ' rewritten as there are a limited number of writes before the emprom dies
' has less registers =80 to 127, the old code had 127 , new dump code has dummy bytes
Let w6 = b3
Let w6 = w6 + b4
Let w6 = w6 + b5
Let w6 = w6 + b6
If w5 <> w6 Then main ' checksum is invalid
Let b2 = b2 * 4 ' sets address
If b2 < 45 Then ' make sure 0-47 only otherwise wierd bugs if write to other registers
b7 = 80 ' hex $50 = start of registers
b2 = b2 + b7
poke b2, b3
b2 = b2 + 1
poke b2, b4
b2 = b2 + 1
poke b2, b5
b2 = b2 + 1
poke b2, b6
End If
Let b9 = 1
GoTo main

ProcessDumpPeek: ' replaces code above
If b9 = 0 Then NoChange ' if no inputs received then send this back
Let b1 = 255
serout 0, N1200, ("Y")
' short pause for VB to catch up
Let w5 = 0 ' checksum
pause 200
For b1 = 80 To 127
peek b1, b2
serout 0, N1200, (b2)
w5 = w5 + b2
Next b1
' sent 48 bytes, send dummy ones now
b2 = 0
For b1 = 0 To 79
serout 0, N1200, (b2)
Next
serout 0,N1200,(b10,b11) ' checksum out
Let b9 = 0 ' reset change pointer
GoTo main


And VB.6 code:

Sub ReadDataFromPostbox(BaudRate As Integer, CommPort As Integer, Timeout As Integer, ReturnFlag As String)
' use baudrate 1200 commport=2 and timeout=5
Dim i As Integer
Dim ReadString As String
Dim LineOfText As String
Dim StartTime As String
Dim RequestString As String
Dim AnyNewData As String
Dim AsciiChr As String
Dim Checksum As Long
Dim DataValue As Integer
Dim ReadChecksum As Long
Dim DeviceNo As Integer
Dim ByteNo As Integer
On Error GoTo ReadDataError
DeviceNo = 0: ByteNo = 0
MSComm1.CommPort = Str(CommPort)
MSComm1.Settings = Trim(Str(BaudRate)) + ",n,8,1"
MSComm1.RThreshold = 1
MSComm1.InputLen = 1
MSComm1.PortOpen = True
StartTime = Now
ReadString = ""
RequestString = "DataD2345678": ' Data+D plus dummy bytes
MSComm1.Output = RequestString
Do
If MSComm1.InBufferCount >= 1 Then Exit Do: ' got enough bytes
i = DateDiff("s", StartTime, Now)
If i > Timeout Then ReturnFlag = "Timeout": Exit Do
DoEvents
Loop
' sends back "Y" or "N" to indicate if any data
AnyNewData = MSComm1.Input
If AnyNewData = "Y" Then
'now read 128 bytes plus two checksum bytes
Do
If MSComm1.InBufferCount >= 130 Then Exit Do: ' got enough bytes
i = DateDiff("s", StartTime, Now)
If i > Timeout Then ReturnFlag = "Timeout": Exit Do
Loop
Checksum = 0
For i = 0 To 129
AsciiChr = MSComm1.Input
DataValue = Val(Asc(AsciiChr))
Select Case i
Case Is <= 127: Checksum = Checksum + DataValue
PostboxValues(DeviceNo, ByteNo) = DataValue
ByteNo = ByteNo + 1
If ByteNo = 4 Then ByteNo = 0: DeviceNo = DeviceNo + 1
Label54.Caption = Str(PostboxValues(1, 0)) + " " + Str(PostboxValues(1, 1)) + " " + Str(PostboxValues(1, 2)) + " " + Str(PostboxValues(1, 3))
Label25.Caption = Str(PostboxValues(5, 0)) + " " + Str(PostboxValues(5, 1)) + " " + Str(PostboxValues(5, 2)) + " " + Str(PostboxValues(5, 3))
Case 128: ReadChecksum = DataValue
Case 129: ReadChecksum = ReadChecksum + DataValue * 256
End Select
If ReadChecksum = Checksum Then ReturnFlag = "Checksum Valid" Else ReturnFlag = "Checksum Invalid"
Next
Else
' no data to read
If ReturnFlag <> "Timeout" Then ReturnFlag = "No new data"
End If
MSComm1.PortOpen = False
Exit Sub
ReadDataError: If MSComm1.PortOpen = True Then MSComm1.PortOpen = False: ReturnFlag = "Error"
End Sub

vb code dont work... maybe i did some mistake.
can you give me more help???
 

hippy

Ex-Staff (retired)
Can you give more than "VB Code don't work" ? ;-)

Have you created a form with all the necessary controls ? It's really just an example of how to rather than 'this is what you need'.

It's not really easy to give detailed code for serial use as every project needs something slightly different. It would be possible to wrap serial code in something which would allow data to be read and sent as character, string or number ( much like SerIn and SerOut ) but it's a reasonable amount of work and no one's put the effort into that.
 

moxhamj

New Member
As Hippy says, you need to add forms, start a project, put the code in the right place, have declares etc at the beginning of the form. At the very least you need to put a comm control on the form. Go to Project/Components... and scroll down to Microsoft Comm control and check the box. Then a little telephone will appear on the left side of the screen. Drag this onto the form. If it still doesn't work, please give more information about which part doesn't work, what error messages appear etc.

I am assuming that because you want to use VB6 you know how to code in VB6. If you know how to code in VB6 you will know how to add controls to forms.

But if you do not know how to code in VB6, I would very much recommend that you learn VB.Net. Many things have changed from VB6 to VB.Net - it is almost a different language see http://www.pharfruminsain.com/Teach/VB.NET.aspx

So if you learn VB6 you will have to unlearn it soon and change to VB.Net. Much better to learn VB.Net first.

This tute takes you through serial port step by step in VB.Net http://www.instructables.com/id/Control-real-world-devices-with-your-PC/

I didn't write it for VB6 simply because the first page would have started with "Go out and spend hundreds of dollars on this software". No-one would then have done the tute! VB.Net is free. Grab it while you can!
 

alexthefox

Senior Member
http://msdn.microsoft.com/en-us/express/aa718406.aspx is the site for downloads.

I am currently moving my vb6 programs over to vb.net. Lots of people on this forum have expertise so any problems - please ask for help!
im looking on your website, and find vb.net simple, that is ok for move my first step on vb---picaxe project. just i dont know why i cant do .exe file from vb.net 2008. it dont work on other pc, but if i do the same program on vb.net 2005 il work fine... maybe i make some mistake . i install in 2 pc vb 2008, so they have same components, but when i run the installation pack of my simple (sorry your simple ;) ) it give me some error in installation. i dont know!!!
 

hippy

Ex-Staff (retired)
No idea why VB 2008 Express won't create .EXE files, unless it's a Beta or not meant to. Haven't tried 2008 since Beta so cannot help; waiting for the 1GB download to finish :-(

As to the error during installation; exactly what error message and exactly what are you trying to install ?

It could be that VB 2005 and VB 2008 use different .Net Frameworks and I recall running into some ridiculous problem with Packaging Signatures / Digital Certificates with VB 2005.
 

hippy

Ex-Staff (retired)
Oh well. I tried running the setup.exe but I wasn't going to let it make a connection through my Firewall to a raw IP address as I don't know why it wants to do that and the furthest my Italian stretches is to identifying which is the Cancel button.

Other than that, VB 2008 installed okay for me - and it's as horrible as ever after VB6 but seems to work okay. Created a simple application and got a .EXE file so no problem there.
 
Last edited:

jwhooper

Senior Member
If you want to work in VB6, which is way easier than .NET, just drop a MSCOMM, MSCHART, and a button onto a form and use this code. Set the comm control to 4800 baud. This is assuming you want to do a real-time line graph, like I am doing. This simple program can be modified to do about anything you want.

Oh, and this program assume you are writing out comma delimited values, like this:

===picaxe08M program===
Start:

Main:
Count 2, 100, w1
sertxd(" ,", #w1)
goto main
===end===

===vb6 program===
Private Sub Command1_Click()
On Error Resume Next

' Buffer to hold input string
'Dim data As String
Dim ln As Integer
' Use COM1.
MSComm1.CommPort = 1
' 4800 baud, no parity, 8 data, and 1 stop bit.
MSComm1.Settings = "4800,N,8,1"
' Tell the control to read entire buffer when Input
MSComm1.InputMode = 0
' Open the port.
MSComm1.PortOpen = True
' Send the attention command to the modem.
'MSComm1.Output = "ATV1Q0" & Chr$(13) ' Ensure that
' the modem responds with "OK".
r = 0

Do
Do
buffer$ = buffer$ & MSComm1.Input
DEL5
Loop Until Len(buffer$) > 6
ln = Len(buffer$)
Data = buffer$
a = Split(Data, ",")
For Each s In a
If IsNumeric(s) Then
If r > 99 Then
For i = 1 To 99
MSChart1.Row = i + 1
rd = MSChart1.Data
MSChart1.Row = i
MSChart1.Data = rd
Next
MSChart1.Row = 100
MSChart1.Data = s
Else
r = r + 1
MSChart1.Row = r
MSChart1.Data = s
End If
End If
Next

' Open "C:\DATA.TXT" For Append As #1
' 'Write #1, Data
' Close #1
buffer$ = ""
Loop Until False 'loop forever

MSComm1.PortOpen = False
End Sub

Private Sub DEL5()
PauseTime = 2 ' Set duration.
Start = Timer ' Set start time.
Do While Timer < Start + PauseTime
DoEvents ' Yield to other processes.
Loop
End Sub
===end===
 

moxhamj

New Member
As jwhooper says, have you got a form with a mscomm control on it?

I couldn't get your link working - it came up with some ladies who need to put on more clothes ;)

Can you compile a simple vb.net program? Start vb.net, start a new project, put a single button on the form, double click on the button, write "end" in the code, then go to build/publish application, go through all the steps (just note where it puts the application as it will hide it in a very obscure directory), go to that directory (for me it was C:\Documents and Settings\Administrator\Local Settings\Application Data\Temporary Projects\WindowsApplication1\publish), run the setup program and it should install your little program. Now run it from the shortcut in Start/Programs/WindowsApplication1

Can you please tell us where the error message appears?
 
Last edited:

alexthefox

Senior Member
As jwhooper says, have you got a form with a mscomm control on it?

I couldn't get your link working - it came up with some ladies who need to put on more clothes ;)

Can you compile a simple vb.net program? Start vb.net, start a new project, put a single button on the form, double click on the button, write "end" in the code, then go to build/publish application, go through all the steps (just note where it puts the application as it will hide it in a very obscure directory), go to that directory (for me it was C:\Documents and Settings\Administrator\Local Settings\Application Data\Temporary Projects\WindowsApplication1\publish), run the setup program and it should install your little program. Now run it from the shortcut in Start/Programs/WindowsApplication1

Can you please tell us where the error message appears?
yes i know about the woman... but its ok.. it is publicity...
i try to do lot of test, just a button with a label, i do step by step what you say, but i can install only in the pc where i compile the project. i solve the problem with VB2005 (it is in pack of visual studio 2005) with that work fine, and i test your simple and it work, i try to install in other pc, and i dont get any problem.
i dont undestand why vb 2008 ( i download it from your link) wasnt work.
 

hippy

Ex-Staff (retired)
Unfortunately, without the actual error message we'll all be second-guessing what the problem may be, especially as I suspect there's even less experience with VB 2008 on this forum than there is with VB 2005.

Sounds to me as if something like the prerequisite .Net Famework is missing on the PC you're trying to install to or some Digital Certificate is missing. But it could be any of a number of things. So many it's not worth guessing any further.
 
Top