Shop OBEX P1 Docs P2 Docs Learn Events
Geek programmer at it again! — Parallax Forums

Geek programmer at it again!

gr8_big_geekgr8_big_geek Posts: 34
edited 2012-04-26 00:36 in BASIC Stamp
Okay,

I understand the heuristic of the microcontroller which is to write a script, upload it to the device and then unplug from the computer and viola! The microcontroller is suppose to store in its memory the program and then execute it when the given conditions are met.

Here's my question/situation...

I have an electronic keypad that I programmed (check out the video here: http://www.youtube.com/watch?v=oPAoJ4xoZTw) and currently I am sending different keystrokes to the literal Stampw.exe program installed on the PC. Is there a better way to do it? Say if such and such a button is pressed (in a given sequence) then the microcontroller would execute the program (w/out the Stampw.exe running at all)? I'm having to manually load the program (bla.bs2) into the parallax basic stamp app then send the key strokes to run it, then sending the keystrokes to unload the document from the basic stamp app (so it's ready for the next bla2.bs2 to be loaded and run). It's kind of a long work around and I figured there would be a better way to do it so I thought I would just throw this out to the cosmos.

Anyway, any and/or all feedback (ideas/thoughts/concerns...well you get the idea) would be wonderful to hear.

Thank you all in advance!

Keep getting' your geek on~

Comments

  • stamptrolstamptrol Posts: 1,731
    edited 2012-04-24 04:49
    Hi geek,

    Without knowing a few more details of the system this is but a conjecture: you should be able to have the keypad unit talk directly to the Stamp which could operate the door lock and actuator directly.

    Assuming the keypad outputs a serial string, the Stamp would receive it and act accordingly.

    How about a block diagram of your system?

    Cheers,
  • lardomlardom Posts: 1,659
    edited 2012-04-24 07:51
    :lol: That is beyond cool!
  • gr8_big_geekgr8_big_geek Posts: 34
    edited 2012-04-24 11:44
    Tom,

    The output is again nothing more than VB loading the document up in Stampw.exe and then running it; so, in visual basic I created a sub that opens/runs my different BS2 files (when the password/authorization is correct):

    Sub Open_Door()
    'Open the doc
    System.Diagnostics.Process.Start("C:\Name_Of_File_Loc\Stamp_doc.bs2")
    'Now Find the ProcID of Stampw.exe and set focus on it (in the background as the form has "topmost" set)
    ProcID = Shell("C:\Program Files\Parallax Inc\Stamp Editor v2.5.2\Stampw.exe", AppWinStyle.NormalFocus)
    'Now that the focus is set on that app send these keystrokes
    My.Computer.Keyboard.SendKeys("^r", True) 'this runs the program or sends it to the microcontroller to run
    'Set focus back on the Stampw.exe app in case focus was lost
    ProcID = Shell("C:\Program Files\Parallax Inc\Stamp Editor v2.5.2\Stampw.exe", AppWinStyle.NormalFocus)
    'Pull up the file menu
    My.Computer.Keyboard.SendKeys("%f", True)
    'Clear all docs from Basic Stamp Editor
    My.Computer.Keyboard.SendKeys("l", True)
    'The End
    End Sub

    This opens my doc, runs it and then closes it again but it also keeps the Basic Stamp Editor running the background all the time for several reasons:
    1. Cutting down on the time to open the editor when I need to execute a file
    2. The darn editor has this splash screen (which I cannot seem to disable) that opens with the editor which takes "topmost" and kills the LCARS skin idea and flushes it down the drain

    The keypad is a VB app I wrote and I'm not sure how to get the string to be sent to the microcontroller in a way so that the BS2 file (or microcontroller) can simply receive a signal and then do X with it.

    I understand also that normal keypads can be interfaced with the microcontroller (ie the 4x4 Matrix Membrane Keypad found/sold here: https://www.parallax.com/Store/Microcontrollers/BASICStampModules/tabid/134/ProductID/739/List/1/Default.aspx?SortField=UnitCost,ProductName) but they don't have that LCARS skin to it. That's were customized programming comes in.

    Basically, I'll keep the microcontroller plugged into the PC that's running the VB app but I'm just wondering how to send certain/special characters to the microcontroller so it takes that to mean run function x, y or z. I actually only have about 4 different ways in which the door can be manipulated so there only needs to be that many functions within the BS2 doc. VB can filter out which one the user needs and then send the appropriate signal to the microcontroller to run the given function.

    If you have any ideas or some pseudo code that you wouldn't mind posting I would be most grateful. I've done a lot in basic stamp but that was years ago and so I'm not up on my coding in that area. I hope that is specific enough or makes sense. If you have any further questions please feel free to shoot them my way.

    I will continue to work on the project to see if there are more efficient ways to get the job done. If I do figure out a better way I will add it to this post as well (just in case someone in the future is googling around and finds this post).

    Thanks in advance everyone!
  • xanaduxanadu Posts: 3,347
    edited 2012-04-24 12:26
    You should send ASCII commands (for button presses) on the com port using VB. Then on the Stamp you have a loop that listens for ASCII commands.

    You can use DEBUGIN for the commands.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    FREQOUT 4, 1000, 300       'reboot warning
    
    cmd VAR Word               'create variable
    cmd = 0                    'init variable setting
    
    
    
    DO                         'Do loop listens for serial input
    DEBUGIN DEC cmd            'on Basic Stamp.
    IF cmd = 1 THEN act        'Add a subroutine for the command,
    LOOP                       'and keep listening.
    
    act:                       'Replace with existing code
    FREQOUT 4, 500, 3000
    RETURN
    
  • gr8_big_geekgr8_big_geek Posts: 34
    edited 2012-04-24 12:48
    I've been reading up and found some posts:

    http://forums.parallax.com/entry.php?423-VB-Express-2010-to-Basic-Stamp-2-Serial-Communication

    which leads to this one also:

    http://forums.parallax.com/showthread.php?96973-VB-Express-to-Stamp-Template

    Those are very helpful but I am using a Basic Stamp homework board (the one with the breadboard attached for noobs). Currently, I have 2 different servos plugged into the board:
    1 is at P04
    2 is at P10

    How do I get the Stamp to accept an incoming message from a serial port (in my case COM1) which will trigger action to those locations?

    My debug port info:

    Com Port: NONE
    Baud Rate: 9600
    Parity: NONE
    Data Bits: 8
    H/W Flow Control: Off

    Which works fine when I am using my COM1 port plug to my microcontroller BASIC Stamp HomeWork Board.

    So check this out, I have these 3 things I need to run and currently I have them in different .bs2 documents:

    DOC #1:

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    counter VAR Word

    FOR counter = 1 TO 55
    PULSOUT 4, 1000
    PAUSE 8
    NEXT

    PAUSE 1500

    FOR counter = 1 TO 400
    PULSOUT 10, 675
    PAUSE 10
    NEXT

    FOR counter = 1 TO 200
    PULSOUT 10, 90
    PAUSE 10
    NEXT

    PAUSE 4000

    FOR counter = 1 TO 100
    PULSOUT 4, 45
    PAUSE 8
    NEXT


    and here is the next document/.bs2:

    DOC #2:

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    counter VAR Word

    FOR counter = 1 TO 100
    PULSOUT 4, 45
    PAUSE 8
    NEXT

    and here is the next document/.bs2:

    DOC #3:

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    counter VAR Word

    FOR counter = 1 TO 55
    PULSOUT 4, 1000
    PAUSE 8
    NEXT

    I really like this page here: http://www.theabramgroup.com/basicstamp/ because it takes you step-by-step on opening up the serial port but I'm curious to know how the serial port sends to P01 - P15 (on the HomeWork Board) or executes the above files/actions?

    So, you get the BaudRate 9600 port open (SerialPort1.Open() in VB), then what? Send what to the microcontroller to activate the function which triggers the following sequence?

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    counter VAR Word

    FOR counter = 1 TO 55
    PULSOUT 4, 1000
    PAUSE 8
    NEXT

    I will continue reading up on this and practicing because I am learning a lot about how the interface between VB and Stamp work(s). At this point I'm really stuck on what to send through Baud Rate 9600 and how that can activate my code above within the microcontroller. Do I need to write out all of the code:

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    counter VAR Word

    FOR counter = 1 TO 55
    PULSOUT 4, 1000
    PAUSE 8
    NEXT

    as a sub in VB and send all of that to the microcontroller/stamp? so I just have a sub with that data in it and when SerialPort1.Open() I then make a call to Sub _Whatever() which triggers X action? I know with batch (.bat) files you can create menus so that when the user selects X from the menu it triggers a call to that part of the batch but can you do that in .bs2? So, you have something like this:

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    counter VAR Word

    ::Condition 1 'which is run when BaudRate receives message of "Condition 1" which equals KeyStroke = X
    '...run this section of code

    ::Condition 2 ''which is run when BaudRate receives message of "Condition 2" which equals KeyStroke = Y
    '...run this section of code

    ::Condition 3 'which is run when BaudRate receives message of "Condition 3" which equals KeyStroke = Z
    '...run this section of code


    This post is horrible (I know), sorry about that, I'm really thinking out loud as I'm traveling through this noob journey.

    I'm still reading here: http://forums.parallax.com/showthread.php?96973-VB-Express-to-Stamp-Template and seeing how/if I can answer my question using that information. I'll keep you all "posted" literally.
  • gr8_big_geekgr8_big_geek Posts: 34
    edited 2012-04-24 12:55
    Jon (aka xanadu <- totally hot name btw),

    I just posted a few minutes after you did. So, I didn't see that you had written anything yet.

    I see what you mean about setting up a "listener" on the Stamp side which answers one of my curiosities about how the Stamp rcv's information from the VB program. So, once Stamp get's

    IF cmd = 1 THEN act 'Add a subroutine for the command,
    LOOP 'and keep listening.

    That is where the Stamp will execute the given section of code? So, if I have 3 things that I need to "listen" for then set up 3 independent loops? Each listening for their given que?

    How would you set up the code for my conditions?

    Also, as a side not to everyone, I have planned to post my entire VB solution file up on this thread once I'm done. That way if anyone wants my LCARS door opener program they can have it. I'm totally open source and I think if anyone can learn from this that's great!
  • xanaduxanadu Posts: 3,347
    edited 2012-04-24 13:13
    Glad I can help. I should have mentioned there is a piezo speaker hooked up to pin 4 in case you didn't notice. The code below allows for three different serial keystrokes, calls their subroutine and execute the subroutine code. So you'd replace my boring sounds with your cool servo code :)

    I haven't done much with this so there may be better ways to ensure proper functionality.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    FREQOUT 4, 1000, 300       'reboot warning
    
    cmd VAR Word               'create variable
    cmd = 0                    'init variable setting
    
    
    
    DO                         'DO loop 'listens for serial input
    DEBUGIN DEC cmd            'on Basic Stamp.
    IF cmd = 1 THEN act1       'Add a subroutine for the command,
    IF cmd = 2 THEN act2
    IF cmd = 3 THEN act3
    LOOP                       'and keep listening.
    
    act1:                      'Replace with program1
    FREQOUT 4, 500, 3000
    RETURN
    
    act2:                      'Replace with program2
    FREQOUT 4, 500, 4000
    RETURN
    
    act3:                      'Replace with program3
    FREQOUT 4, 500, 5000
    RETURN
    
  • gr8_big_geekgr8_big_geek Posts: 34
    edited 2012-04-24 13:18
    Perfect! I have your code and have created the bs2 file. It is set to listen. Now on the VB side I am trying to set up the BaudRate to transmit. Once I've finished the code up and things are playing nice I'll post my completed solution zip up here on the forum in case anyone wants the code. Thank you very much Jon!
  • xanaduxanadu Posts: 3,347
    edited 2012-04-24 13:28
    Nice, I cannot wait to see it!

    Also, I should mention if you're leaving the Stamp plugged into a PC you may experience random resets due to noise. This is why I use XBee for serial com with a PC. Since it's a door lock I'd definitely go wired though, there are work arounds like adding a resistor and capacitor to pin 4. http://www.emesystems.com/BS2rs232.htm#resets Definitely worth checking out.
  • gr8_big_geekgr8_big_geek Posts: 34
    edited 2012-04-24 13:31
    Very true. Sometimes there is a surge and the Stamp runs the last thing (which may be to open the door)!

    I'm still hammering out the code to get VB to send the cmd 1 to Stamp. Yikes, isn't there an easier way Microsoft!? haha

    Thank you for the link and idea to reduce accidental door opens to the face! <- ouch!

    I'll post back on here once I get this VB button press to send something over BaudRate to the Stamp
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2012-04-24 15:24
    Hi, there are a couple of options for serial comms with the Stamp, you can use the same cable you use for programming as the serial connection and this is defined as P16 in the program code or you can use a RS232 to TTL converter from the PC and connect Rx/Tx to any two I/O pins in the range of P0 to P15.

    The programming port (P16) is a nice option because it's there, no additional hardware required. There are limitations to the Stamps serial comms but if you are aware of them it's not too much of a problem.

    There is no serial port buffer, no interupts, when you go over 4800 baud there are certain situations when the communications get unreliable and when using P16 any characters transmitted to the Stamp are echoed back to the PC.

    The echo mentioned above can be troublesome with a VB application because the echoed characters are retained in the PC's serial port buffer and if they are not cleared before the next transmission they can corrupt the next data packet.

    Out of a personal preference the only thing I might change with Jon's code would be to use SERIN as opposed to DEBUGIN, it has a little more flexibility and I would make cmd a byte variable.
    cmd VAR byte               'create variable
    cmd = 0                    'init variable setting
    
    DO                               'DO loop 'listens for serial input
    SERIN 16, 16780 ,[DEC cmd]       'receive acknowledgement on Basic Stamp.(4800 baud)
    IF cmd = 1 THEN GOSUB act1       'Add a subroutine for the command,
    IF cmd = 2 THEN GOSUB act2
    IF cmd = 3 THEN GOSUB act3
    LOOP
    

    At the Visual Basic end you could possibly go with
    Dim myvariable As Byte
            myvariable = 1
            SerialPort1.ReadExisting()   'Clear Buffer
            SerialPort1.Write(myvariable.ToString)
    

    Jeff T.
  • gr8_big_geekgr8_big_geek Posts: 34
    edited 2012-04-24 15:39
    Okay, I'm having issues with the attachment feature but I'm trying to attach a test SLN for Visual Studio 2010 Ultimate Visual Basic project as well as a .bs2 file.

    For some reason I have followed various suggestions and codes and I cannot get the communication to happen

    Does the Stampw.exe (aka parallax Basic Stamp v2.5.2) have to be running (so it's listening) on BaudRate 9600 while I transmit to it using BaudRate 4800 (or some other port)?

    Either way, I've attached where I'm currently at. This is still a work in progress so I will continue to work at it until I solve this! :)

    PrjBaudRate.zip, Copy of LISTEN.bs2
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2012-04-24 16:28
    Hi, first I have to apologize for stating the wrong baud rate figure for the BS2, for 4800 baud it should be 16572 and not 16780

    You set the baud in VB using the serialport properties window, you will also need to make sure you are connecting to the right port.

    The baud rate for the Stamp is in the SERIN instruction and must match the baud of your VB app.
    SERIN 16, 16572 ,[DEC cmd]
    

    16572 is the inverted calculated value for 4800, inverted because the programming port has a line driver which inverts the incoming signal.

    cmd should be a BYTE not WORD variable

    Once you have downloaded Listen.bs2 you no longer require Stampw.exe.

    Jeff T.

    BTW I thought the touchscreen was neat, who is the manufacturer.
  • xanaduxanadu Posts: 3,347
    edited 2012-04-24 16:34
    Unsoundcode brings up some good points about the serial useage and that is the direction to go once you get the other kinks worked out.
    Does the Stampw.exe (aka parallax Basic Stamp v2.5.2) have to be running (so it's listening) on BaudRate 9600 while I transmit to it using BaudRate 4800 (or some other port)?

    Keep in mind only one application can access a serial port (unless you use extra software) so if you're sending the command with the Parallax debug window then yes that application needs to be running. While the Parallax debug window is running your VB script will not be able to open the serial port. So make sure that it's closed when you're done testing.

    As far as baud rate goes, at the top of the Debug window should be all of your settings when it opens.

    Also, like you said in your first post, the microcontroller is something that can run stand alone. So really once it's programmed you don't need to run the programmer at all. It would just be your VB GUI talking directly to the serial port, with the Stamp on the other end listening.
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2012-04-25 04:42
    Expanding on the VB side of things then perhaps the simplest application for test purposes would be a Form with only one button and a serialport dragged over from the toolbox.

    Double clicking on the Form brings up the Form load event handler where the port can be set up and opened.
    SerialPort1.BaudRate="4800"
    SerialPort1.Portname="COM1"   'Or whatever com port is used
    SerialPort1.Open()
    

    Double clicking on the button will bring up the Button1 event handler where the code to transmit a value can be coded.
    Dim myvalue As Byte
    myvalue=1
    SerialPort1.ReadExisting()
    SerialPort1.Write(myvalue.ToString)
    

    The above should work nicely with the BS2 code we have already.

    Jeff T.
  • gr8_big_geekgr8_big_geek Posts: 34
    edited 2012-04-25 12:40
    I really do appreciate all of the help on this particular forum. Everyone's feedback and assistance has been very helpful. Thank you for the reminder though that only one thing can use the port at a time. Also, thank you for the sample VB code on the Form1_Load example as well.

    Currently, I created an entirely new VB project until I have the Stamp communicating with the VB request properly then I will go from there. Attached are both the VB solution folder along with the stamp program I'm using. For some reason it is still not connecting. I'm not sure if the listener is set correctly on the stamp side (mostly because I do not understand where the port number is determined and also what that section of code means).

    How do you determine the stamp side Baud? 16572 = 4800 is that because you are taking 4800 * 3.4525? So, if (for example) I wanted 9600 I would use: 9600 * 3.4525 = 33144, therefore 33144 would be the stamp side BaudRate?

    Also,

    SERIN 16, 16572 , [DEC cmd] ' what is the "16" for?

    Either way, what I have so far runs fine on the VB side (no errors, etc) and I've uploaded my .bs2 program to the microcontroller but when I click on the VB buttons nothing happens. So, I know something is not connecting or communicating. Attached is the latest revision. I've got the Stamp side BaudRate set at 4800 and on the VB side I'm using 4800 and I've also modified the stamp code to "SERIN 16, 16572 , [DEC cmd]" but to no avail.

    (If anyone is having trouble opening the attachments please let me know and I can post the code text on here)

    Frustrating, naw, just another challange! :) I will keep working at it though.


    Baud_Rate.zipLISTEN.bs2
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2012-04-25 12:57
    Hi, goto this link http://www.parallax.com/tabid/440/Default.aspx
    and download the BASIC Stamp Syntax and Reference Manual 2.2 it is invaluable when programming the Stamp.

    Look under the section Command Reference and read the section on SERIN and SEROUT, it tells how the baudrate is derived for each of the different Stamps.

    For each serial command a Port pin must be assigned for Tx and Rx, when using the programming port you would automatically assign Port 16.

    I will check out the sln and bs2 files a little later a get back to you.

    Jeff T.
  • gr8_big_geekgr8_big_geek Posts: 34
    edited 2012-04-25 14:24
    Jeff,

    Thanks for the info on port 16 (also for the link). I have been picking the kids up and running some errands but I'll be looking at the code again in just a little bit as well.

    I'll be posting some more later on. :)
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2012-04-25 15:33
    I see what is happening, here is a brief explanation.

    If we look at the SERIN instruction it's formatted SERIN,16,[DEC cmd], that's the instruction (SERIN), the Port (16) and a variable to hold the data (cmd). The element I haven't mentioned is DEC , DEC is a formatter, it formats the received data into an ASCII character and as it is written in our program DEC will just sit there waiting for a non printable character ( such as carriage return or newline ) before it moves on in the program. Another option with the DEC formatter is the option to specify how many characters to wait for, for example SERIN,16,[DEC3 cmd] will wait for three characters or a non printable character, whichever comes first, before moving on.

    So basically our present program(s) is hung at the SERIN instruction, there are two ways we can resolve this, the first is to modify our BS2 code to move on after receiving one charcter

    SERIN,16,[DEC1 cmd]

    the second is to modify the VB code to append a non printable character to the transmitted data

    SerialPort1.WriteLine(myvariable.ToString)

    Writeline automatically appends the NewLine character to any string it writes, newline is by default binary value 10 but can be changed to any byte value desired, for example 13 would be carriage return.

    Everything else in your solution looks good, baud etc.

    Jeff T.
  • gr8_big_geekgr8_big_geek Posts: 34
    edited 2012-04-25 16:18
    Jeff you are a genius!!!

    That worked perfectly! I got it working and the code runs so much more smooth without having to have anything running in the background. It connects directly to the Stamp/microcontroller!

    I need to clean up my code because it is very messy (with notes and such all over) but I will post the solution for the whole VB keypad later on tonight (maybe after class). Either way, all of my questions have been answered that I came on this forum for! I'm so excited.

    Lastly, I need to follow through with making sure I come up with a solid way to deal with potential power surges because it will be connected to the computer directly as mentioned previously in this post.

    So, there should be one more post by me later on tonight and then we can mark this thread [SOLVED]!
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2012-04-25 16:48
    I know the feeling of that aha moment there are plenty to come with the Stamp, it's a wonderful hobby and very capable in serious applications.

    With regard to VB if you feel comfortable with it and intend to continue developing interfaces/GUI's then do keep working through my thread on the Stamp Template http://forums.parallax.com/showthread.php?96973-VB-Express-to-Stamp-Template

    Also check this page out, it has many VB tutorials and a book download that you can purchase for a little over 5 bucks http://www.homeandlearn.co.uk/NET/vbNet.html

    The thread on the Stamp Template has a few mistakes and things that can be done better but if you have any questions post them in the Template thread and I will attempt to answer them.

    Jeff T.
  • gr8_big_geekgr8_big_geek Posts: 34
    edited 2012-04-26 00:36
    Okay all,

    As promised, I intended on attaching the zipped VB solution I wrote for the VB keypad; however, the VB solution zip is 24.8MB so it will not upload to this post. I am still willing to allow folks to have the code. If you are interested in having it please send me a message and I'll email it to you or host it on my website so you can download it (& then I'll just shoot you the link to it).

    That seems cheesy but it's the best I can do for now. Thank you again for all of your help everyone! Get your geek on & keep on coding!

    Peace out~
Sign In or Register to comment.