Shop OBEX P1 Docs P2 Docs Learn Events
Stamp Serial 2way communications project — Parallax Forums

Stamp Serial 2way communications project

RackleRackle Posts: 17
edited 2005-01-30 03:41 in BASIC Stamp
heres the general idea, I want to write an application in VB5 to verify·username and password to then control pins going high and low on the stamp (i'm using BOE), at the moment these commands just turn on and off LEDS but will do more in the future.· Then after the user has changed the pin to high or low in vb5, the next operation to be carried out is for the stamp to report back the status of certain pins.· As output pins change their state due to user input, the input pins (currently connected to switches) will then change their state to high or low.

The problem is that the string I send out to the stamp comes back into Visual basic in the mscomm1.input string.· The string·contains the data that is supposed to be returned from the stamp on the end of the string that was sent out.· I did some reading on the forum here and saw that its due to an electrical issue on how the stamp gains power through the serial cable, and that this echo is normal and unavoidable - short of modifing hardware on the stamp chip (which I'm not prepared to do).· I saw on the post that the information I'm sending out could be filtered from the input, the only way I've accomplished this is using the mid$(input, 1, 1) command.· Is there a better way?

My bigger problem, that i can't figure out how to work around, is the input is only so big.· So I can't "pick" the digit in the string I want to use (using the mid or left or right function-to pick the 5 or 6th digit out of 8), because if the string is over 8 digits it just scrolls, and each digit changes its position every second.·

I have the input/output function set on a timer at a quick interval because I want the final product to run in real time, not have to push a button to make it refresh its numbers.

I'd appreciate any advice or tips on how to correct this, or correct my code in general.· This is a project I've been working on in my head for a while and starting reading up on VB5 and stamp enough to get a workign knowledge.· Unfortunatly this is one of the common applications they teach in the books.

Included is my VB5 code and Pbasic code:

Dim inputbuf As String
Dim pin1 As Integer
Dim pin0 As Integer

Private Sub Form_Load()
'Fire RX Event every * bytes
MSComm1.RThreshold = 1
'When Inputting data, input * bytes at a time
MSComm1.InputLen = 0
'2400 Baud, no parity, 8 data bits, 1 stop
MSComm1.Settings = "2400,N,8,1"
'Open Comm1
MSComm1.CommPort = 1
MSComm1.PortOpen = True

End Sub

Private Sub MSComm1_OnComm()
If MSComm1.CommEvent = comEvReceive Then
··· inputbuf = MSComm1.Input
End If
' View what is coming in
Label1(2) = inputbuf
End Sub
Private Sub Timer1_Timer()
MSComm1.Output = "start"
pin0 = Check1(0).Value
pin1 = Check1(2).Value
If pin0 = 0 Then MSComm1.Output = "0"
If pin0 = 1 Then MSComm1.Output = "1"
If pin1 = 0 Then MSComm1.Output = "0"
If pin1 = 1 Then MSComm1.Output = "1"
Label2(0) = pin0
Label2(1) = pin1


End Sub


'{$STAMP BS2}

'{$PORT COM1}



Good0 var byte

Good1 var byte

bad0 var byte

bad1 var byte



input 0

input 1

output 2

output 3

output 4

Main:

Serin 16,16780,[noparse][[/noparse]wait("start"),Good0, Good1]

out2 = Good0

out3 = Good1

if in0 = 0 then go1

if in0 = 1 then go2

go1:

Serout 16,16780,[noparse][[/noparse]"1"]

goto main:

go2:

Serout 16,16780,[noparse][[/noparse]"0"]

goto main:





Thanks

Comments

  • allanlane5allanlane5 Posts: 3,815
    edited 2005-01-26 13:54
    This looks OK. One thing -- you are sending the "1" and "0" as a String. You are currently having the BS2 recieve them as a byte. You probably want:

    SERIN 16, 16780, [noparse][[/noparse]WAIT "start", DEC Good1, DEC Good2]

    The 'DEC' modifier will tell the BS2 to recieve the byte, and convert it into its decimal equivalent. Thus, you'll send the byte Ascii "1" (which is ASCII Character 49, I believe) and the DEC modifier makes the result a 1 value in its variable.

    You might want to add a 'command complete' character (like a CR) on the BS2 output. Then have your On_Comm event collect characters until it recieves the 'command complete'. Then return the entire string, remove the 'start' echo, and there you are.

    You ARE currently sending from the BS2 as an ASCII String already, so that part is good.
  • BorisBoris Posts: 81
    edited 2005-01-26 13:58
    My bigger problem, that i can't figure out how to work around, is the input is only so big.· So I can't "pick" the digit in the string I want to use (using the mid or left or right function-to pick the 5 or 6th digit out of 8), because if the string is over 8 digits it just scrolls, and each digit changes its position every second.·

    I have the input/output function set on a timer at a quick interval because I want the final product to run in real time, not have to push a button to make it refresh its numbers.
    Hello, i am currently working on something simmilar. Have a small vb6 application that communicates with basic stamp through serial port. My knowledge of VB6 is not that great but maybe we can figure something out here.
    Can you please clarify what you meant in the statement above. Is "input" input from PC to Stamp or the other way around? Is the PC receiving a 5to8 digit number and needs to analyzie it? So you have a timer in VB and on every timer event some info is sent to Basic Stamp?
  • steve_bsteve_b Posts: 1,563
    edited 2005-01-26 13:58
    I'm unsure about your VB issues....I'm just not savvy in that language yet!

    But so far as your communications issue....if I'm understanding your problem right....

    You are getting your transmitted code echoed back to you?!·

    YEs, if you are using the 'programming port' on the BOE then you will get this.· Note also that you can use any of the other pins on the stamp as a serial port as well!

    Reading rs232 in to the stamp is very easy....simply put a 22kohm resistor in series with the line (to prevent overloading the stamp pin) and when constructing your serin line for that pin, be sure to select an 'inverted' baud code.

    As far as sending rs232....some pc's will SEE the signal straight from a stamp (even though it's 0-5volts--not rs232 levels)...but if you do this, then you also have to serout in an 'inverted' baud mode.

    Likely, you may have to use a level converter for your line going back to the PC.· MAX232 chips are the most common.· I use the MAX232CPE which is a 5volt powered IC that you have to add 5 capacitors to in order to achieve the +/-12V levels.· NOW, if you go with the level converter, your serout baud mode will have to be true!

    Just a quick note on true vs inverted.· Normal RS232 levels are usually +12Volts for a binary 0 and -12Volts for a binary 1.· This is backwards (or inverted) from TTL levels of +5V = 1 and 0V = 0.·

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·

    Steve
    http://members.rogers.com/steve.brady
    "Inside each and every one of us is our one, true authentic swing. Something we was born with. Something that's ours and ours alone. Something that can't be learned... something that's got to be remembered."
  • RackleRackle Posts: 17
    edited 2005-01-26 22:19
    It just seems so much more convienent to use the built in programming port, no additional contruction, plus when my projects done its going to be run from a computer terminal.
  • Beau SchwabeBeau Schwabe Posts: 6,557
    edited 2005-01-26 23:01
    Perhaps if you send your data in "packets" where the first byte is a SYNC byte, the second byte is a function byte, and the remaining bytes are Data bytes.
    Doing this you could get around your "echoing" problem by having a Different SYNC byte originating from the PC than you do from the Stamp.
    In addition, the echoing could actually be useful in determining if your device is plugged in or not.


    Here is a Simple Packet Structure which will accommodate for variable packet lengths:

    'Sync Byte' 'Function Byte' 'Data Byte...'


    Sync Byte:
    11110011 - Sync Byte from PC
    11111100 - Sync Byte from Stamp


    Function Byte:
    00000000 - Null
    00000001 - Read Memory
    00000010 - Write Memory
    00000011 - Set Time
    00000100 - Read Time
    (....and so on up to 256 functions)

    Note: Each function sets the following number of Data bytes to be read or written

    For Example: (to set time from PC to 12:30:00)

    '11110011' '00000011' '0001100' '00011110' '00000000'

    1) The Stamp looks for the SYNC byte
    2) The Stamp determines the function
    3) The Stamp reads-in the remaining Data bytes based on the function
    4) The Stamp performs the function after Data is received

    For Example: (to read time from the Stamp into the PC)

    '11111100' '00000100' 'Byte1' 'Byte2' 'Byte3'

    1) The PC looks for the SYNC byte
    2) The PC determines the function
    3) The PC reads-in the remaining Data bytes based on the function
    4) The PC performs the function after Data is received

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe - Mask Designer III

    National Semiconductor Corporation
    (Communication Interface Division)
    500 Pinnacle Court, Suite 525
    Mail Stop GA1
    Norcross,GA 30071

    Post Edited (Beau Schwabe) : 1/27/2005 3:00:27 PM GMT
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-01-26 23:03
    Perhaps this article will help out:··http://www.parallax.com/dl/docs/cols/nv/vol3/col/nv89.pdf

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA
  • RackleRackle Posts: 17
    edited 2005-01-26 23:44
    I can't send in more than 8 bits at a time? I see

    00000000 Null
    00000001 read
    etc.

    Whats happening to me is that I'm sending in 10 bits and when VB receives those 10 bits ( I have label1 setup to visually display to me what its inputting) onces that 9th bit comes in it pushes the 2nd bit to the 1 spot and the bit in the 1 spot out. when the 10th comes in it pushes out the digit that was in the second spot to begin with that got pushed to the 1 spot. What I end up with is a stream of numbers moving across the display.

    If I have to work in sets of 8 (no long string of numbers), I guess I'll have find out how to take those groups of numbers and put them into a variable and have the program update displays based on whats in those variables.

    If I'm going to receive a total of 48 bits, the first 24 are echo, I can put them into bitarry1,2,3,4 then put the remaining 24 bits into bitarry 5,6,7,8 then since the same amount of bits will come through each time and replace the ones currently in that spot the program can contstantly update itself.

    no easy way to take in one big string tho? this will take some more work...
    20 minute update:
    using instr() to isolate whether a phrase or sequence appears in a line.· like the "start" echo line, and then a "go"·in the actual line I'm tring to return I can send each string to a variable string depending on its contents, then assign that variable string to a label to display it.

    perhaps not the more eloquent way to do it, but for a novice like myself its just simple enough! (please feel free to keep offering advice for myself and others on other (better [noparse]:)[/noparse] ) ways to do this.

    Thanks!!

    Post Edited (Mshkor) : 1/27/2005 12:12:41 AM GMT
  • Kevin SkyMoCoKevin SkyMoCo Posts: 23
    edited 2005-01-27 02:11
    Why doesn't the stamp have programmable flow control on the built in serial port?
    And why does it echo the characters sent to it? Seems like that causes quite
    a few of us to stay up at night.
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-01-27 03:22
    1. RS-232 is a byte oriented protocol. You can only send bytes at a time. Not the BS2's fault.

    2. The BS2 'built-in serial port' has a very clever, small parts count RS-232 converter on it. This converter uses the TX data voltage when it is sending the RX signal back to the PC. A side effect of this is that every time the TX data line changes for a TX byte, that byte too is echo'ed back to the PC. In a way, this is not a bug, this is a feature, because every byte sent for programming the BS2 is automatically echo'ed back to the PC.

    It's actually nice that you get this 'free' RS232 converter port that works as well as it does. And if you can't figure out how to filter out an echo of data that you JUST sent to the BS2, then you have larger issues.

    You can always add your own MAX232 and DB-9 connector, and enable flow control, and do RS232 communications on ANY OTHER PIN you want to use, as the BS2 does support that.
  • Kevin SkyMoCoKevin SkyMoCo Posts: 23
    edited 2005-01-27 04:01
    I wasn't knocking the stamp or its design, I was just wondering the "why" behind the
    way it acts. I understand that it would be wonderful to have everything I think I need
    on one chip, but that might not work so well for everyone else.

    smile.gif
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2005-01-27 04:59
    Folks -

    The echo is not a problem per se, it's a feature solely of Pin Port 16. If you don't want or need the echo, just don't use Pin Port 16. Pin Ports 0-15 are all availalble for SERIN/SEOUT.

    Regards,

    Bruce Bates
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-01-27 15:04
    Sure, Kevin, and I did not mean to be surly with my response. It was late, I was tired.

    However, if you remove the surly from my post, that's still the reason it works the way it does.

    Rock on!
  • RackleRackle Posts: 17
    edited 2005-01-28 01:37
    Can I also ask a non-stamp, but still project related question here to?· How can I simplify this code in my VB5 program to avoid falling into an infinite loop that crashes my program, but avoid repeating lines

    If Text1 = userarray(i).assnum And Text2 = userarray(i).pin Then Form1.Command1(0).Caption = userarray(i).Name

    If Text1 = userarray(i + 1).assnum And Text2 = userarray(i + 1).pin Then Form1.Command1(0).Caption = userarray(i + 1).Name

    If Text1 = userarray(i + 2).assnum And Text2 = userarray(i + 2).pin Then Form1.Command1(0).Caption = userarray(i + 2).Name

    I've tried many combinations of if..then...else and do...while...loops. but they usually end in a crash.


    This:

    Private Sub Command1_Click()
    i = 0
    place:
    i = i + 1
    If Text1 = userarray(i).assnum And Text2 = userarray(i).pin Then Form1.Command1(0).Caption = userarray(i).Name Else GoTo place
    ···
    Kinda works, but I get SUBSCRIPT OUT OF RANGE on some entries.

    ·
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-01-28 02:28
    VB questions belong in the Sandbox....

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA
  • RackleRackle Posts: 17
    edited 2005-01-28 02:46
    ok, thanks! wasn't sure where to put it.
  • freq42freq42 Posts: 18
    edited 2005-01-30 03:17
    You should try this web site it help’s me out a lot when I got started with VB.
    I know they can help you
    http://www.rentron.com/VisualBasic.htm
    http://www.rentron.com/sending_data.htm

    yeah.gif·
  • RackleRackle Posts: 17
    edited 2005-01-30 03:41
    Actually thats the website that first got me started on that project! I've resolved my communicatsions issues, now I'm down to my user interface.

    I dont' expect any more big problems until I try and attempt to expand the output/input capabilities by use of multiplexing. I haven't read up on it in detail, but am interested in the prospect.

    Thanks for the links
Sign In or Register to comment.