Newbie question about SERIN & SEROUT
Jimbo30
Posts: 129
While I am receiving data from the Basic Stamp, I would like to do a couple other things that involve sending data to the Basic Stamp so I can turn on lights and control motors.· However, I am finding that my basic stamp code is getting locked up in a SERIN command and doesn’t want to return.·· Below is the BSII code I have so far.· I am using RS485 to send and receive over a long distance so that is why pin 9 going from H to L in case you are wondering.· All the data is being sent over the 485 correctly if I take out the SERIN portion.· I am wondering if I could get a push in the right direction.
·
‘=========================================Declarations=======================
adcbits··· VAR Byte···· ·'stores bits from ADC0831 TEMP
· ·v········· VAR Byte
·· r········· VAR Byte
·· v2········ VAR Byte
·· v3········ VAR Byte
·· a········· VAR Byte
·· heading1·· VAR Byte····· 'stores first heading N,S,E,W
·· heading2·· VAR Byte····· 'stores second heading E, W
·· x········· VAR Byte····· 'stores character 120 for VB app "TEMP"
·· y········· VAR Byte····· 'stores character 121 for VB app· "Heading"
·· B········· VAR Byte····· 'Stores character 66 for VB app "Depth"
·· adcbits1·· VAR Byte····· 'Stores bits from ADC0831 DEPTH
·· vd········ VAR Byte
·· r1········ VAR Byte
·· vd1······· VAR Byte
·· vd2······· VAR Byte
·· PinState·· VAR Byte
·· PinNumber· VAR Byte
·
·
·· x = 120················· 'Defines END character, x as 120 TEMP
·· y = 121················· 'Defines END character, y as 121 HEADING
·· B = 66·················· 'Defines END character, B as 66· DEPTH
·· CS········ PIN· 0······· 'Defines pin0 as CS for temp, depth ADC0831
·· CLK······· PIN· 1······· 'Defines pin1 as CLK for TEMP MEASURE
·· DataOutput PIN· 3······· 'Defines pin3 as DataOUTPUT for temp, depth
·· CLK1······ PIN· 2······· 'Defines pin2 as CLK for DEPTH MEASURE
·· a = "."················· 'defines a as "." for VB APP.· USED for both TEMP & DEPTH.
===========Main====================================
·· DO
· ······LOW 9
······· GOSUB lightscheck
······· GOSUB ADC_DataTemp· 'gets ADC data for temp
······· GOSUB Calc_VoltsT·· 'calculates voltage at temp pin
······· GOSUB Dirreading··· 'gets compass direction
······· GOSUB ADC_DataDepth 'gets ADC Data for Depth
······· GOSUB Calc_VoltsD·· 'calculates voltage at depth pin
·· LOOP
·
lightscheck:
······· SERIN 8, 16468, [noparse][[/noparse]WAIT(255),PinNumber,PinState]
······· IF PinState = 0 THEN GOTO LightOFF
······· IF PinState = 1 THEN GOTO lightON
······· RETURN
·
········· LightOFF:
········· LOW PinNumber
········· RETURN
·
········· LightON:
········· HIGH PinNumber
········· RETURN
·
·
· ADC_DataTemp:
·
······· LOW CLK
······· LOW CS
······· PULSOUT CLK, 210
······· SHIFTIN DataOutput,CLK,MSBPOST, [noparse][[/noparse]adcbits\8]
······· HIGH 9
······· SEROUT 8, 16468, [noparse][[/noparse]DEC1 v, a, DEC2 v2, x]·· 'sends neccessary temp voltage data to VB app
······· HIGH CS
······· RETURN
· Calc_VoltsT:
······· v = 5 * adcbits / 255
······· r = 5 * adcbits // 255
······· v2 = 100 * r / 255
······· v3 = 100 * r // 255
······· v3 = 10 * v3 / 255
······· IF (v3 >= 5) THEN v2 = v2 + 1
······· IF (v2 >= 100) THEN
······· v = v + 1
······· v2 = 0
······· ENDIF
······· RETURN
· Dirreading:
··· GOSUB compass·· 'get compass data
··· HIGH 9
··· SEROUT 8, 16468, [noparse][[/noparse]heading1, heading2, y]· 'transmits both headings
··· PAUSE 2000
··· RETURN
· compass:
··· IF IN4=0 THEN compassN
··· IF IN6=0 THEN compassS
··· IF IN7=0 THEN compassW
··· IF IN5=0 THEN compassE
· compassE:
··· heading1 = "E"
··· heading2 = " "
··· RETURN
· compassN:
··· IF IN7 = 0 THEN compassNW
··· IF IN5 = 0 THEN compassNE
··· heading1· = "N"
··· heading2· = " "
··· RETURN
· compassS:
··· IF IN7 = 0 THEN compassSW
··· IF IN5 = 0 THEN compassSE
··· heading1·· = "S"
··· heading2·· = " "
··· RETURN
· compassW:
··· heading1· = "W"
··· heading2· = " "
··· RETURN
· compassNE:
··· heading1·· = "N"
··· heading2·· ="E"
··· RETURN
· compassNW:
··· heading1· = "N"
··· heading2· = "W"
··· RETURN
· compassSE:
··· heading1· = "S"
··· heading2· = "E"
··· RETURN
· compassSW:
··· heading1·· = "S"
··· heading2·· = "W"
··· RETURN
· ADC_DataDepth:
······· LOW CLK1
······· LOW CS
······· PULSOUT CLK1, 210
······· SHIFTIN DataOutput, CLK1, MSBPOST, [noparse][[/noparse]adcbits1\8]
······· HIGH 9
······· SEROUT 8, 16468, [noparse][[/noparse]DEC1 vd, a, DEC2 vd1]
······· HIGH CS
······· RETURN
· Calc_VoltsD:
······· vd· =· 5 * adcbits1 / 255
······· r1· =· 5 * adcbits1 //255
······· vd1 =· 100 * r1 / 255
······· vd2 =· 100 * r1 // 255
······· vd2 =· 10· * vd2 / 255
······· IF (vd2 >= 5) THEN vd1 = vd1 + 1
······· IF (vd2 >= 100) THEN
······· vd = vd + 1
······· vd1 = 0
······· ENDIF
···· RETURN
·
‘=========================================Declarations=======================
adcbits··· VAR Byte···· ·'stores bits from ADC0831 TEMP
· ·v········· VAR Byte
·· r········· VAR Byte
·· v2········ VAR Byte
·· v3········ VAR Byte
·· a········· VAR Byte
·· heading1·· VAR Byte····· 'stores first heading N,S,E,W
·· heading2·· VAR Byte····· 'stores second heading E, W
·· x········· VAR Byte····· 'stores character 120 for VB app "TEMP"
·· y········· VAR Byte····· 'stores character 121 for VB app· "Heading"
·· B········· VAR Byte····· 'Stores character 66 for VB app "Depth"
·· adcbits1·· VAR Byte····· 'Stores bits from ADC0831 DEPTH
·· vd········ VAR Byte
·· r1········ VAR Byte
·· vd1······· VAR Byte
·· vd2······· VAR Byte
·· PinState·· VAR Byte
·· PinNumber· VAR Byte
·
·
·· x = 120················· 'Defines END character, x as 120 TEMP
·· y = 121················· 'Defines END character, y as 121 HEADING
·· B = 66·················· 'Defines END character, B as 66· DEPTH
·· CS········ PIN· 0······· 'Defines pin0 as CS for temp, depth ADC0831
·· CLK······· PIN· 1······· 'Defines pin1 as CLK for TEMP MEASURE
·· DataOutput PIN· 3······· 'Defines pin3 as DataOUTPUT for temp, depth
·· CLK1······ PIN· 2······· 'Defines pin2 as CLK for DEPTH MEASURE
·· a = "."················· 'defines a as "." for VB APP.· USED for both TEMP & DEPTH.
===========Main====================================
·· DO
· ······LOW 9
······· GOSUB lightscheck
······· GOSUB ADC_DataTemp· 'gets ADC data for temp
······· GOSUB Calc_VoltsT·· 'calculates voltage at temp pin
······· GOSUB Dirreading··· 'gets compass direction
······· GOSUB ADC_DataDepth 'gets ADC Data for Depth
······· GOSUB Calc_VoltsD·· 'calculates voltage at depth pin
·· LOOP
·
lightscheck:
······· SERIN 8, 16468, [noparse][[/noparse]WAIT(255),PinNumber,PinState]
······· IF PinState = 0 THEN GOTO LightOFF
······· IF PinState = 1 THEN GOTO lightON
······· RETURN
·
········· LightOFF:
········· LOW PinNumber
········· RETURN
·
········· LightON:
········· HIGH PinNumber
········· RETURN
·
·
· ADC_DataTemp:
·
······· LOW CLK
······· LOW CS
······· PULSOUT CLK, 210
······· SHIFTIN DataOutput,CLK,MSBPOST, [noparse][[/noparse]adcbits\8]
······· HIGH 9
······· SEROUT 8, 16468, [noparse][[/noparse]DEC1 v, a, DEC2 v2, x]·· 'sends neccessary temp voltage data to VB app
······· HIGH CS
······· RETURN
· Calc_VoltsT:
······· v = 5 * adcbits / 255
······· r = 5 * adcbits // 255
······· v2 = 100 * r / 255
······· v3 = 100 * r // 255
······· v3 = 10 * v3 / 255
······· IF (v3 >= 5) THEN v2 = v2 + 1
······· IF (v2 >= 100) THEN
······· v = v + 1
······· v2 = 0
······· ENDIF
······· RETURN
· Dirreading:
··· GOSUB compass·· 'get compass data
··· HIGH 9
··· SEROUT 8, 16468, [noparse][[/noparse]heading1, heading2, y]· 'transmits both headings
··· PAUSE 2000
··· RETURN
· compass:
··· IF IN4=0 THEN compassN
··· IF IN6=0 THEN compassS
··· IF IN7=0 THEN compassW
··· IF IN5=0 THEN compassE
· compassE:
··· heading1 = "E"
··· heading2 = " "
··· RETURN
· compassN:
··· IF IN7 = 0 THEN compassNW
··· IF IN5 = 0 THEN compassNE
··· heading1· = "N"
··· heading2· = " "
··· RETURN
· compassS:
··· IF IN7 = 0 THEN compassSW
··· IF IN5 = 0 THEN compassSE
··· heading1·· = "S"
··· heading2·· = " "
··· RETURN
· compassW:
··· heading1· = "W"
··· heading2· = " "
··· RETURN
· compassNE:
··· heading1·· = "N"
··· heading2·· ="E"
··· RETURN
· compassNW:
··· heading1· = "N"
··· heading2· = "W"
··· RETURN
· compassSE:
··· heading1· = "S"
··· heading2· = "E"
··· RETURN
· compassSW:
··· heading1·· = "S"
··· heading2·· = "W"
··· RETURN
· ADC_DataDepth:
······· LOW CLK1
······· LOW CS
······· PULSOUT CLK1, 210
······· SHIFTIN DataOutput, CLK1, MSBPOST, [noparse][[/noparse]adcbits1\8]
······· HIGH 9
······· SEROUT 8, 16468, [noparse][[/noparse]DEC1 vd, a, DEC2 vd1]
······· HIGH CS
······· RETURN
· Calc_VoltsD:
······· vd· =· 5 * adcbits1 / 255
······· r1· =· 5 * adcbits1 //255
······· vd1 =· 100 * r1 / 255
······· vd2 =· 100 * r1 // 255
······· vd2 =· 10· * vd2 / 255
······· IF (vd2 >= 5) THEN vd1 = vd1 + 1
······· IF (vd2 >= 100) THEN
······· vd = vd + 1
······· vd1 = 0
······· ENDIF
···· RETURN
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
The only real solution would be to use some kind of external hardware buffer that would receive and hold any incoming bytes, then retransmit them when the Stamp tells it that it's ready.
Another option would be to eliminate the wait by having the Stamp become the "master" by sending data to VB whenever it has it and by sending a request for a change in pin state which would always be sent be VB immediately (or a response that says "don't change the state").
Otherwise, the external buffer idea sounds like the best solution.·
Although the third option does sound like it would work.· I will look into all three starting with option 1 & 3.
Thanks,·Mike and Franklin for the push.
Mike's option 3 really works super fast when it's set up right , I have done it succesfully in VB·Express and keep meaning to see if I can use something similar with VB6 . I will get back to you if I have anything that might be useful.
Jeff T.
Thanks UnSoundcode,· I just tested using the timeout and Tlabel and it works pretty darn well.· But then again as soon as I start the motor section of the ROV it may not work as well.· I·plan having only having 3 speed·settings on the starboard and port side of the ROV.· The up and down will have a constant speed when·active.·I will, however, keep the third option open if I have issues in the future and if you have anything to add that would be much appreciated.
SERIN 8, 16468, 2500, ADC_DataTemp, [noparse][[/noparse]WAIT(255),PinNumber,PinState]
Post Edited (Jimbo30) : 3/5/2010 9:14:58 PM GMT
The thing I·discovered and·liked was doing away with the Chr(value) format and instead using a byte array (y)·then passing·it to a·Variant (x) for serial output.
Jeff T.