Shop OBEX P1 Docs P2 Docs Learn Events
Help needed with SERIN — Parallax Forums

Help needed with SERIN

Sean BarnesSean Barnes Posts: 6
edited 2013-06-02 09:39 in BASIC Stamp
Hello,

I am trying to learn serial communication between a Windows Form program (using C#) and a BS2 stamp. The GUI is just two buttons, one to turn an LED on and the other to turn it off. I'm trying to send a "1" to the stamp to turn it on and a "0" to turn it off. The stamp code is as follows:

' {$STAMP BS2}
' {$PBASIC 2.5}
' {$PORT COM4}

switch VAR bit
HIGH 15
PAUSE 1000
LOW 15

DO
SERIN 16, 16780, [DEC5 switch]
IF switch = 1 THEN HIGH 15
IF switch = 0 THEN LOW 15
DEBUG HOME, "switch = ", DEC5 switch, CR
LOOP

When I push either of the buttons I get an error on the GUI stating that I cannot access COM4. If I turn the stamp off I do not get the error. I don't know if the problem is with my stamp program or the GUI but if someone can tell me if the above looks right or wrong I would be grateful. Also, if there is a better way to test serial communication to and from the stamp, please let me know. Thanks.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2013-06-02 08:24
    Remove the "{$PORT" statement. That's only used when you have more than one Stamp connected to a PC. The Stamp Editor automatically searches the available COM ports for a Stamp.

    Your SERIN statement uses 2400 Baud based on the 16780 constant value. The DEBUG statement uses 9600 Baud. Normally both sides of a conversation have to use the same Baud. I suggest using "DEBUGIN DEC1 switch" instead of the "SERIN 16, 16780, [DEC5 switch]" for two reasons. 1) It automatically uses the PC connection; 2) It automatically uses the same Baud (9600) as the DEBUG statement.

    Your SERIN statement uses a DEC5 formatter for inputting the switch value. If you'll read the section in Appendix C and the chapter on the SERIN statement in the Basic Stamp Syntax and Reference Manual, you'll see that this expects a 5 character field containing a numeric value with no delimiter. That's probably not what your C# program is doing. You probably want DEC1 as I mentioned.

    The COM4 error is probably a result of what your GUI program is doing. If you're trying to run the Stamp Editor at the same time, in order to modify the program or look at the DEBUG output, then that's your problem. The GUI and the Stamp Editor are both trying to use the COM port at the same time and Windows won't allow that.

    If you quit the Stamp Editor while you're running the GUI program, the GUI program should be able to get the COM port. It gets both sides of the connection, so in addition to it transmitting to the Stamp, it will / can receive the output from the DEBUG statement. It's probably ignoring that, but you could read it and display it.
  • Sean BarnesSean Barnes Posts: 6
    edited 2013-06-02 09:38
    Mike,

    Thanks for the help. It is now working as I hoped.
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2013-06-02 09:39
    Hi, this is not your original problem but it is a problem I see.

    The serial port communicates using bytes of data, when you transmit the "1" or the "0" you are transmitting one byte each time. So as Mike points out your SERIN should be formatted to receive just the one byte
    SERIN 16, 16780, [DEC1 switch]

    The problem I am seeing is that the variable "switch" is declared as a bit :- switch VAR bit change this to switch VAR Byte.

    The following link should help you with the Visual Studio side of things and the Basic Stamp. It's a few examples of programming comms with VB but can easily be translated to C#

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

    Jeff T.
Sign In or Register to comment.