February 15, 2005

its sort of ping pong code

most of my energy went to creating a system where the pics listen to both the xport and a computer serial connection at the same time sans server. no love. still some things worth exploring but I gave up for the moment to make some ping pong. 10 minutes of contemplating how to keep servos going while the serin2 keeps interupting the code was enough for now, this thing just powers leds. It starts in xport to xport mode, listens for a connection, then gives control of one serin to a computer. Using the computer you can trigger an infinite ping pong loop between two xport/pic setups. Done, no hardware inputs, it works better conceptually as a closed system..

code 1 (my xport):


' based on Tom Igoe code


' variables and constants for the serial port:
dataByte var byte
dataByte2 var byte


init Var Byte
init = 0

dirt var Byte
dirt = 0

tx var portc.6
rx var portc.7
xportRx var portc.0
xportTx var portc.1
xportDTR var portc.2
inv9600 con 16468 ' baudmode for serin2 and serout2: 9600 8-N-1 inverted
non9600 con 84 ' baudmode for serin2 and serout2: 9600 8-N-1 non-inverted


led1 VAR portC.4
led2 VAR portC.3


Pause 1000 ' Wait a second at startup


' take DTR high so Xport doesn't disconnect when we first connect:
HIGH xportDTR


low led1
High led2 'on light


main:
if init =1 then
serin2 rx, inv9600, [dataByte]
else
serin2 xportRx, non9600, [dataByte]
endif
' parse the data:
gosub checkByte


goto main

checkByte:
if init = 0 Then
if dataByte = 73 then
init = 1
serout2 tx, inv9600, ["swap should happen", 10, 13]
endif
If dataByte <49 then
dirt = 1
dataByte = 49
Else
if dataByte >58 then
dirt = 0
dataByte = 58
endif
if dirt = 0 then
dataByte = dataByte -1
else
dataByte = dataByte +1
endif
endIF
Else
if dataByte = 126 then
init = 0
dataByte = 57
serout2 tx, inv9600, ["swap back should happen", 10, 13]
Endif
endIF
if dataByte > 47 and dataByte < 58 then
' convert the ascii value of 0-9 to a numeric value
' (ascii "0" = 48)
dataByte2 = dataByte - 48


' if we get "9", turn on light.
if dataByte2 = 9 then
High led1
endif
if dataByte2 = 8 then
Low led1
endif
' if we get a 0, then disconnect
' by taking the Lantronix device DTR low:
if dataByte2 = 0 then
low xportDTR
pause 300
high xportDTR
endif
' send all bytes straight out without parsing:
serout2 xportTx, non9600, [dataByte, 13]
serout2 tx, inv9600, [dataByte, 10, 13]
endif
return

code2 (alyssa's xport)


'****************************************************************
'* Name : UNTITLED.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2005 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 2/7/2005 *
'* Version : 1.0 *
'* Notes : *
'* : *
'****************************************************************
' Serial out to PC is on pin RC6
' serial in from PC is on pin RC7
' serial out to Lantronix device is on RD1
' serial in from Lantronix device is on RD0
' Lantronix device DTR pin is on RC3
' note: Lantronix device must have DisconnectMode set to 0x80
' in order to disconnect using DTR pin.


' variables and constants for the serial port:
dataByte var byte
dataByte2 var byte
tx var portc.6
rx var portc.7
xportTx var portd.1
xportRx var portd.0
xportDTR var portc.3
inv9600 con 16468 ' baudmode for serin2 and serout2: 9600 8-N-1 inverted
non9600 con 84 ' baudmode for serin2 and serout2: 9600 8-N-1 non-inverted

TRISB = %00000000 ' set all the pins of PORTB to output

' blink an LED on startup:
high portb.0
pause 1000
low portb.0


' take DTR high so Xport doesn't disconnect when we first connect:


HIGH xportDTR


Pause 1000 ' Wait a second at startup

main:
' check for incoming serial data from the net:
serin2 xportRx, non9600, [dataByte]
' parse the data:
gosub checkByte
goto main


checkByte:
if dataByte > 47 then
' convert the ascii value of 0-9 to a numeric value
' (ascii "0" = 48)
dataByte2 = dataByte - 48


' if the number is between 1 and 8, light an LED:
if dataByte2 > 0 && dataByte2 <= 8 then
' the dcd command takea a number and
' converts it to the position of a bit in a byte:
portb = dcd (databyte2 - 1)
endif


' if we get a 0, then disconnect
' by taking the Lantronix device DTR low:
if dataByte2 = 0 then
low xportDTR
pause 300
high xportDTR
endif
endif


' send all other bytes straight out without parsing:
serout2 xportTx, non9600, [dataByte, 13]
serout2 tx, inv9600, [dataByte, 10, 13]
return


Posted by William Blaze at February 15, 2005 03:04 AM | TrackBack