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