PDA

View Full Version : Newbie question about SERIN & SEROUT



Jimbo30
03-06-2010, 02:25 AM
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.
·
‘=========================================Declarat ions=======================
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, [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, [adcbits\8]
······· HIGH 9
······· SEROUT 8, 16468, [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, [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, [adcbits1\8]
······· HIGH 9
······· SEROUT 8, 16468, [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

Franklin
03-06-2010, 02:38 AM
Take a look at the reference for serin for the timeout command.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen

Mike Green
03-06-2010, 02:44 AM
A Stamp can only do one thing at a time as you've noticed. That means that, when it's waiting for serial input data, it can't do anything else. You could use a timeout on the SERIN (read the BASIC Stamp Syntax and Reference Manual for details), but then the Stamp would miss any received characters that come in while it's doing "something else".

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").

Jimbo30
03-06-2010, 03:44 AM
I do have the reference manual so·will definitely look into the timeout and what you mentioned about the "timeout" could cause issues.· This is a challenge for me since this is the first time I have ever built something of this magnitude.· Probably a breeze you you guys, lol.
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.

Unsoundcode
03-06-2010, 04:36 AM
Hi Jimbo30 , how do you have the Stamp interfaced to the PC , I see you using the same pin for tx / rx .

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.

Jimbo30
03-06-2010, 04:53 AM
That "timeout" and "Tlabel" works miracles. I figured it was something silly. On rare occasions I have to click on the "LIGHT ON" & "LIGHT OFF" command buttons a couple times to get the intended action, but I should be able to put a slight delay within the VB command button to so the stamp has time to see the data.

Jimbo30
03-06-2010, 05:06 AM
Sorry forgot to answer one of your questions...I have an RS232 to RS485 auto-tx/rx switchover on the laptop and just a MAX485 on the stamp end.

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. http://forums.parallax.com/images/smilies/roll.gif

SERIN 8, 16468, 2500, ADC_DataTemp, [WAIT(255),PinNumber,PinState]






Unsoundcode said...

Hi Jimbo30 , how do you have the Stamp interfaced to the PC , I see you using the same pin for tx / rx .

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.



Post Edited (Jimbo30) : 3/5/2010 9:14:58 PM GMT

Mike Green
03-06-2010, 05:16 AM
You might design the interaction between VB and the Stamp to assume that a command will be sent multiple times and the Stamp may miss once or twice. That way, if the Stamp times out on a SERIN, does something else, then tries a SERIN again, it may get the command sent from the PC. If the command sets a status like I/O pin on or I/O pin off, it won't matter how many times it's received. A command to "do something" isn't really something that makes sense to repeat unless you want to repeat the action more than once.

Unsoundcode
03-07-2010, 10:07 AM
Hi Jimbo30 , I came up with this example , the BS2 file and vb text work together . The vb is just 2 combo boxes ,1 button and a comm control. The list for combo1 should be filled with the pin list and combo2 with 1 and zero for on and off respectively. I think there is room for improvement but its pretty quick. The on comm event looks for the header and transmits the data.

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.

Jimbo30
03-08-2010, 09:47 AM
Thanks Unsoundcode, I will check it out.