Problem with multiple module communication
sonny
Posts: 10
Hi,
I have a problem somewhere in my project, and cant figure out where.
I have 4 xbee modules. One connected directly to my PC via the USB adapter, and the other three are connected to their own BS2 via the 5 volt adapter board, with serial comminucation on pins 14 and 15. Outputs 0-8 are used for outputs based on a byte that is received from the PC. The data packet that the PC sends contains an address nibble in the first byte. All three stamps are running the exact same code, with the exception of the address part:
' {$STAMP BS2}
' {$PBASIC 2.5}
SerData VAR Byte(11)
CheckSum VAR Word
lp:
TOGGLE 13
SERIN 15,84,2000,lp,[STR SerData\10]
checksum = SerData(0)+SerData(1)+SerData(2)+SerData(3)+SerData(4)+SerData(5)+SerData(6)+SerData(7)+SerData(8)
IF CheckSum & $FF <> SerData(9) THEN lp
IF SerData(0) & $f0 <> $30 THEN lp 'MSB of SerData(0) is address.
IF SerData(1) & $01 = $01 THEN HIGH 0 ELSE LOW 0
IF SerData(1) & $02 = $02 THEN HIGH 1 ELSE LOW 1
IF SerData(1) & $04 = $04 THEN HIGH 2 ELSE LOW 2
IF SerData(1) & $08 = $08 THEN HIGH 3 ELSE LOW 3
IF SerData(1) & $10 = $10 THEN HIGH 4 ELSE LOW 4
IF SerData(1) & $20 = $20 THEN HIGH 5 ELSE LOW 5
IF SerData(1) & $40 = $40 THEN HIGH 6 ELSE LOW 6
IF SerData(1) & $80 = $80 THEN HIGH 7 ELSE LOW 7
SerData(9)=SerData(0)+SerData(1)+SerData(2)+SerData(3)+SerData(4)+SerData(5)+SerData(6)+SerData(7)+SerData(8) 'build simple checksum
SEROUT 14,84,[STR SerData\10]
GOTO lp
My VB.Net app is running the following code (simplified for testing purposes):
[PublicClassForm1
Dim checksum As Int32
Dim ctr As Int32
Dim outstring(11) AsByte
Dim instring(11) AsByte
Dim sw AsNew System.Diagnostics.Stopwatch
Dim TimeOutTimer AsNew System.Diagnostics.Stopwatch
Dim skipread AsBoolean
PrivateSub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Timer1.Enabled = True
If SerialPort1.IsOpen = FalseThen
SerialPort1.Open()
SerialPort1.DiscardInBuffer()
SerialPort1.DiscardOutBuffer()
EndIf
'**********Assemble Output Data For Module 1
outstring(0) = &H10' Address
outstring(1) = &HAA 'Test Data
outstring(2) = 0 ' future use
outstring(3) = 0 ' future use
outstring(4) = 0 ' future use
outstring(5) = 0 ' future use
outstring(6) = 0 ' future use
outstring(7) = 0 ' future use
outstring(8) = 0 ' future use
OutPutData()
'**********Assemble Output Data For Module 2
outstring(0) = &H20
outstring(1) = &HAA 'Test Data
outstring(2) = 0 ' future use
outstring(3) = 0 ' future use
outstring(4) = 0 ' future use
outstring(5) = 0 ' future use
outstring(6) = 0 ' future use
outstring(7) = 0 ' future use
outstring(8) = 0 ' future use
OutPutData()
'**********Assemble Output Data For Module 3
outstring(0) = &H30
outstring(1) = &HAA 'Test Data
outstring(2) = 0 ' future use
outstring(3) = 0 ' future use
outstring(4) = 0 ' future use
outstring(5) = 0 ' future use
outstring(6) = 0 ' future use
outstring(7) = 0 ' future use
outstring(8) = 0 ' future use
'OutPutData()
EndSub
PrivateSub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
Button1_Click(Button1, System.EventArgs.Empty)
EndSub
PrivateSub OutPutData()
'**********Write Output Data to textbox1 for debug
checksum = 0
ForMe.ctr = 0 To 8
checksum = checksum + outstring(ctr)
Next
outstring(9) = checksum
And &HFF
ForMe.ctr = 0 To 9
TextBox1.AppendText(Convert.ToInt32(outstring(ctr)) & ",") 'display data sent
Next
TextBox1.AppendText(vbNewLine)
'**********Write Data to Serial Port
SerialPort1.Write(outstring, 0, 10)
'**********Read Serial Port and Display Data in textbox2
TimeOutTimer.Restart()
'start timer for timeout
DoUntil SerialPort1.BytesToRead >= 10 Or TimeOutTimer.ElapsedMilliseconds > 500
Loop
If TimeOutTimer.ElapsedMilliseconds > 499 Then TextBox2.AppendText("Timeout" & vbNewLine) : skipread = True
If skipread = FalseThen
SerialPort1.Read(instring, 0, 10)
ForMe.ctr = 0 To 9
TextBox2.AppendText(Convert.ToInt32(instring(ctr)) & ",") 'display data received
Next
TextBox2.AppendText(vbNewLine)
EndIf
EndSub
End
Class
Sorry the formatting is off a bit...
Basically every second, the application assembles output data, sends it out the com port via the xbees to the other xbees then to the stamp. Each time it sends the data, it waits to hear to bytes of data back. If it doesnt after 1/2 second, it moves on. This works perfectly with any one remote module (stamp/xbee combo). If I add 1 by uncommenting the outdata() line, then I get about a 50% timeout rate. If I uncoment all three outdata(), then I get about 60% failure rate. Is my error in my VB code or the BS2 code? I know this isnt a VB forum, but thought somebody might be able to help.
Thanks
Sonny
I have a problem somewhere in my project, and cant figure out where.
I have 4 xbee modules. One connected directly to my PC via the USB adapter, and the other three are connected to their own BS2 via the 5 volt adapter board, with serial comminucation on pins 14 and 15. Outputs 0-8 are used for outputs based on a byte that is received from the PC. The data packet that the PC sends contains an address nibble in the first byte. All three stamps are running the exact same code, with the exception of the address part:
' {$STAMP BS2}
' {$PBASIC 2.5}
SerData VAR Byte(11)
CheckSum VAR Word
lp:
TOGGLE 13
SERIN 15,84,2000,lp,[STR SerData\10]
checksum = SerData(0)+SerData(1)+SerData(2)+SerData(3)+SerData(4)+SerData(5)+SerData(6)+SerData(7)+SerData(8)
IF CheckSum & $FF <> SerData(9) THEN lp
IF SerData(0) & $f0 <> $30 THEN lp 'MSB of SerData(0) is address.
IF SerData(1) & $01 = $01 THEN HIGH 0 ELSE LOW 0
IF SerData(1) & $02 = $02 THEN HIGH 1 ELSE LOW 1
IF SerData(1) & $04 = $04 THEN HIGH 2 ELSE LOW 2
IF SerData(1) & $08 = $08 THEN HIGH 3 ELSE LOW 3
IF SerData(1) & $10 = $10 THEN HIGH 4 ELSE LOW 4
IF SerData(1) & $20 = $20 THEN HIGH 5 ELSE LOW 5
IF SerData(1) & $40 = $40 THEN HIGH 6 ELSE LOW 6
IF SerData(1) & $80 = $80 THEN HIGH 7 ELSE LOW 7
SerData(9)=SerData(0)+SerData(1)+SerData(2)+SerData(3)+SerData(4)+SerData(5)+SerData(6)+SerData(7)+SerData(8) 'build simple checksum
SEROUT 14,84,[STR SerData\10]
GOTO lp
My VB.Net app is running the following code (simplified for testing purposes):
[PublicClassForm1
Dim checksum As Int32
Dim ctr As Int32
Dim outstring(11) AsByte
Dim instring(11) AsByte
Dim sw AsNew System.Diagnostics.Stopwatch
Dim TimeOutTimer AsNew System.Diagnostics.Stopwatch
Dim skipread AsBoolean
PrivateSub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Timer1.Enabled = True
If SerialPort1.IsOpen = FalseThen
SerialPort1.Open()
SerialPort1.DiscardInBuffer()
SerialPort1.DiscardOutBuffer()
EndIf
'**********Assemble Output Data For Module 1
outstring(0) = &H10' Address
outstring(1) = &HAA 'Test Data
outstring(2) = 0 ' future use
outstring(3) = 0 ' future use
outstring(4) = 0 ' future use
outstring(5) = 0 ' future use
outstring(6) = 0 ' future use
outstring(7) = 0 ' future use
outstring(8) = 0 ' future use
OutPutData()
'**********Assemble Output Data For Module 2
outstring(0) = &H20
outstring(1) = &HAA 'Test Data
outstring(2) = 0 ' future use
outstring(3) = 0 ' future use
outstring(4) = 0 ' future use
outstring(5) = 0 ' future use
outstring(6) = 0 ' future use
outstring(7) = 0 ' future use
outstring(8) = 0 ' future use
OutPutData()
'**********Assemble Output Data For Module 3
outstring(0) = &H30
outstring(1) = &HAA 'Test Data
outstring(2) = 0 ' future use
outstring(3) = 0 ' future use
outstring(4) = 0 ' future use
outstring(5) = 0 ' future use
outstring(6) = 0 ' future use
outstring(7) = 0 ' future use
outstring(8) = 0 ' future use
'OutPutData()
EndSub
PrivateSub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
Button1_Click(Button1, System.EventArgs.Empty)
EndSub
PrivateSub OutPutData()
'**********Write Output Data to textbox1 for debug
checksum = 0
ForMe.ctr = 0 To 8
checksum = checksum + outstring(ctr)
Next
outstring(9) = checksum
And &HFF
ForMe.ctr = 0 To 9
TextBox1.AppendText(Convert.ToInt32(outstring(ctr)) & ",") 'display data sent
Next
TextBox1.AppendText(vbNewLine)
'**********Write Data to Serial Port
SerialPort1.Write(outstring, 0, 10)
'**********Read Serial Port and Display Data in textbox2
TimeOutTimer.Restart()
'start timer for timeout
DoUntil SerialPort1.BytesToRead >= 10 Or TimeOutTimer.ElapsedMilliseconds > 500
Loop
If TimeOutTimer.ElapsedMilliseconds > 499 Then TextBox2.AppendText("Timeout" & vbNewLine) : skipread = True
If skipread = FalseThen
SerialPort1.Read(instring, 0, 10)
ForMe.ctr = 0 To 9
TextBox2.AppendText(Convert.ToInt32(instring(ctr)) & ",") 'display data received
Next
TextBox2.AppendText(vbNewLine)
EndIf
EndSub
End
Class
Sorry the formatting is off a bit...
Basically every second, the application assembles output data, sends it out the com port via the xbees to the other xbees then to the stamp. Each time it sends the data, it waits to hear to bytes of data back. If it doesnt after 1/2 second, it moves on. This works perfectly with any one remote module (stamp/xbee combo). If I add 1 by uncommenting the outdata() line, then I get about a 50% timeout rate. If I uncoment all three outdata(), then I get about 60% failure rate. Is my error in my VB code or the BS2 code? I know this isnt a VB forum, but thought somebody might be able to help.
Thanks
Sonny
Comments
I suspect you may have a problem with the timing in your VB program. When you add the 2nd and 3rd outdata() statements, it appears they run immediately after you get the previous BS2 response. But that response is heard by all the other BS2s, and they are busy processing the checksum and comparing the address of the response and may not be able to get back to the SERIN statement in time to catch the first few bytes of your next message. Because the PC is much faster than the BS2, try inserting a delay after each outdata() statement and see if that helps.
Sonny