Shop OBEX P1 Docs P2 Docs Learn Events
BS2 to BS2 communication using RS232 — Parallax Forums

BS2 to BS2 communication using RS232

RoHerman00RoHerman00 Posts: 22
edited 2012-07-27 05:20 in BASIC Stamp
I have a question with the serin and serout commands. Everyone who does a tutorial on serial communication between a computer or another BS2 always uses pins 0-15 to communicate. For example "serout 15, 9600, [DEC 8832]" or something like that. I know that you use serin/serout 16, 9600 etc. when communicated with your computer because those use RS232 voltages rather than TTL. Could I use those pins (physical pins 1 and 2) to communicate with another Basic Stamp? So rather than using Serout 16, 9600, [DEC 8832] for the debug terminal on a computer I use that same command and same pins but connected to pins 1 and 2 of another basic stamp? Also, should this work could I write something like Serout 16\16, 9600, [DEC 8832] and use one of the pins as flow control?

Thanks a lot

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2011-01-18 17:37
    You can't use I/O pin 16 for flow control because it doesn't really exist. I/O pin 16 is a special case for SERIN and SEROUT and this value can't be used anywhere else where an I/O pin number is expected.

    I believe you'll need a pulldown resistor on the receive line on both Stamps. I don't have my Stamp Manual handy (to be able to look at the Stamp schematic), but I think that the transmit line uses this as a source of negative voltage when the line is idle. A pulldown resistor to Vss might work, but a source of -5V would be more like the use of a real RS232 line.

    Whatever is sent to a Stamp's RS232 port is echoed back by the RS232 interface hardware. You'd have to account for this in your programming.
  • RoHerman00RoHerman00 Posts: 22
    edited 2011-01-18 18:28
    Thank you for replying so soon.

    The flow control is not really needed I was just wondering if it was possible. Just to be sure I understand what you mean (I have to look at the wiring for serial pins) I could connect for example physical pins 1 on both stamps and have one stamp use only serin 16 commands and the other serout 16 and it might work using the points you made with connecting them?
  • Mike GreenMike Green Posts: 23,101
    edited 2011-01-18 18:51
    You would have to connect the receive pin of one Stamp with the transmit pin of the other Stamp, not pin 1 to pin 1. You'd use SEROUT on the Stamp with the transmit pin and SERIN on the Stamp with the receive pin. I'll try to look at the Stamp Manual when I get home later. Maybe someone else can chime in in the meantime.
  • RoHerman00RoHerman00 Posts: 22
    edited 2011-01-19 04:37
    Actually never mind it says on the manual that pin 1 is "out" and pin 2 is "in".
  • Mike GreenMike Green Posts: 23,101
    edited 2011-01-19 08:23
    I looked again at the schematic. It looks like you don't have to have a resistor from SIN to ground or -5V because there's already a 20K resistance to ground. That should work ok as long as the Stamps are not far apart or in an electrically noisy environment.
  • RoHerman00RoHerman00 Posts: 22
    edited 2011-01-19 12:32
    Is there somewhere i could look for this schematic. What do you mean by a 20k resistance to ground? Can't i just connect a wire from one bs2 to another or do i have to do something else?
  • stamptrolstamptrol Posts: 1,731
    edited 2011-01-19 13:11
    When learning about serial communication, theres nothing like starting with something that is known to work.

    For Stamp to Stamp communication, look in the Help file of the programming software under SERIN or SEROUT. Note that you can't use p16 in Stamp to Stamp without a negative voltage source. It will only cause tears and frustration when learning the basics. Thats why most examples are p0 - p15.

    For Stamp to PC, use the programming port ( that is, pin16) and the DEBUG command in the STamp. The negative voltage is supplied by the PC's serial port.

    Cheers,
  • Mike GreenMike Green Posts: 23,101
    edited 2011-01-19 13:15
    The schematic is on the Basic Stamp Documentation page here. If you follow the schematic, there's a total of 20K of resistance between SIN and ground (Vss).

    Yes, you can connect SIN of one Stamp to SOUT of the other Stamp (and vice versa). This may not work reliably if the Stamps are far apart (more than a foot or two) or if there's a lot of electrical noise nearby (like motors or other high current devices switched on and off), but it should work otherwise. It would work better with a source of negative voltage as stamptrol mentioned.

    Remember that one Stamp has to be "listening" to the other Stamp (by executing a SERIN) when the other Stamp sends its data with SEROUT. There's no buffering.
  • RoHerman00RoHerman00 Posts: 22
    edited 2011-01-19 14:35
    Thanks a lot that helped. But I am still wondering what you guys mean by applying a negative voltage. I see in the schematic that the db-9 connector is wired to the vss pin of the bs2, but do i just connect the vss pins of both BS2's and then have a single wire connect to ground from there? That's all I'm stuck on.
  • stamptrolstamptrol Posts: 1,731
    edited 2011-01-19 15:22
    The negative voltage is part of the definition of the RS-232 version of serial communication. The negative voltage represents a "1" and the positive voltage represents the "0". By taking advantage of certain characteristics of the Stamp hardware, the negative voltage is "stolen" from the PC. That cannot happen when Stamp is connected to Stamp with the 9-pin connector.

    The db-9 connector is meant for connection to the PC or some other RS-232 device. Again, you will not be terribly successful in using the db-9 connection from Stamp to Stamp. The usual minimum connection between devices using a 9-pin connector is pin 2, pin 3 and pin 5. That is, Rx, Tx and Gnd.

    The recommendation to start by communicating with the normal outputs ( p0 - p15) allows serial communication to take place with TTL voltage levels of 0 and 5 volts.
  • RoHerman00RoHerman00 Posts: 22
    edited 2011-01-19 17:41
    But the reason i'm wondering about rs232 communication is because all of my i/o pins are taken up and i am not using a db-9 connector to connect BS2's. The db-9 connectors are wired as usual to the BS2's as shown by the manual so i can program the stamp but branching off of pins 1 and 2 is a header so i can plug the pins 1 and 2 from another BS2 into it thereby connecting them serially. The program is prewritten to use the statement serout (or serin), 16, 9600 etc. on both stamps and neither stamp when running is connecting to the pc.
  • stamptrolstamptrol Posts: 1,731
    edited 2011-01-20 06:01
    OK, its a bit more complicated, but heres what you can do.

    You will need 2 rs232 level shifting chips (MAX232 or MAX 233 or equiv.) Some have two sets of channels so one chip might suffice depending on how far apart the two Stamps are.

    The level shifter gets connected to each Stamp's db-9 connector and provides the negative voltage thats been mentioned. Then, the TTL levels of the chips are connected forming the link between the Stamps. Obviously, Tx on one Stamp gets connected to RX on the other and vice versa. This will still not give you hand-shaking nor buffering so its best at speeds of 4800 and lower.

    Cheers,
  • RoHerman00RoHerman00 Posts: 22
    edited 2011-01-20 07:33
    ok thanks a lot thats what i needed to hear. Is there a reason why i cannot though just cannot the rx/tx pins as you said without the chips even if i was willing to accept some loss of signal and/or a less reliable connection?
  • Mike GreenMike Green Posts: 23,101
    edited 2011-01-20 08:46
    Try it. I think you'll find that directly connecting SIN to SOUT does work over short distances at the kind of speeds (9600 Baud or less) that you're going to be using. I would suggest that you keep to low speeds (2400 Baud would be best) more because the Stamps are slow and can be unreliable at receiving at 9600 Baud because of that.
  • RoHerman00RoHerman00 Posts: 22
    edited 2011-01-20 10:56
    Thank you very much for the help i will definitely try that. I'm just glad the idea might actually work. Is that all you suggest i should do? Just connect a wire nothing special to add like a wire to ground or anything?
  • Mike GreenMike Green Posts: 23,101
    edited 2011-01-20 13:58
    You always need to connect the grounds of the two boards together. You can connect the ground leads of the DB9 connectors together and that will do it.
  • Wolverine69Wolverine69 Posts: 31
    edited 2012-07-26 08:11
    Where can I find a reference to pin 16 being the programming port on the BS2?

    I don't know if you guys are talking about the actual physical pin, or the pin number as referenced in PBASIC.

    When I look at Figure 1.4 in the PBASIC Syntax & Reference manual v2.2, it shows that physical pin 16 is for I/O P11 and there is no P16 shown.

    From what I've read, pins 1 SOUT & 2 SIN are the dedicated RS-232 level pins.

    Where am I going wrong here?
  • Mike GreenMike Green Posts: 23,101
    edited 2012-07-26 08:53
    On the first page of the section in the Stamp Manual on the SEROUT statement (and the SERIN statement), it mentions that 16 can be used in the pin # parameter and refers to the SOUT / SIN programming connections. This is a special case and there is no real I/O pin 16.

    In the future, please start your own thread for any question not directly related to the subject of a thread. If you just add to an existing thread, you may not get a quick answer and it's confusing to others who may be looking for an answer to the same or similar question.
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2012-07-27 05:20
    Hi, think of P as being "Port", the Stamp has 16 TTL Ports P0 through P15. These are 5 volt signals used for general purpose I/O.

    The programming port is separate from the above ports but apart from programming it can also be used for PC communication. In the PBasic language this port is called P16.

    In the case of a DB9 serial connection the higher voltages from the PC's serial port are routed to a "line driver" on the Stamps circuit board, the line driver reduces these higher voltages from the PC to a safe level for the Stamp. Not only is the voltage level reduced but the signal is inverted, that is one of the reasons the manual lists baud values for True or Inverted.

    One other thing to be aware of when using P16, characters transmitted from the PC to Stamp using P16 will be echoed back to the PC. This will give you some unexpected results if you don't make provision for it.

    Jeff T.
Sign In or Register to comment.