Shop OBEX P1 Docs P2 Docs Learn Events
Serial Communication between two STAMPS — Parallax Forums

Serial Communication between two STAMPS

John KauffmanJohn Kauffman Posts: 653
edited 2008-04-13 12:42 in BASIC Stamp
I am experimenting with serial communications between two STAMPS. The overall goal is to get the very minimum code that will allow a two-way serial communication so that I (and others) can customize for our specific projects. If anyone has working examples, I would appreciate the post of their code.
·
My experimental model:
STAMP #1 = DAUGHTER that senses 4 environmental factors and stores them in variables. Daughter should be able to deliver any one of the·values to the mother (one value at a time).
STAMP #2 = MOTHER that does various tasks that depend on getting the sensor values from the daughter
·
Wiring:
CS (chip select) between pin 0s that alerts daughterboard that a request is coming.
DATA between pin 1s that sends/receives data
·
Communication sequence:
Both stamps are waiting
········· Mother doing other work
········· Daughter doing environmental sensing and also watching for CS to go high
Mother sets CS high
Daughter notes CS high & begins a SerIn
Mother sends a request for one·SensorType
Daughter receives the request
Daughter sends back the value for that SensorType
Both stamps go back to waiting

The attached code is my effort.
·
Problem:
Mother sends a SensorType of 3, but Daughter seems to always receive it as 0.
·
Possible problems??
Communications is happening too fast,·too slow or at wrong time for my code to detect ?
Wrong settings for serout or serin?
Setting CS high and low effects the other STAMP in an unexpected way?
Changing between pins INPUT and OUPUT (for both boards) is messing up the state?
Stupid mistake? ·
·
Thanks.

Comments

  • John KauffmanJohn Kauffman Posts: 653
    edited 2008-04-13 03:47
    The help on SerIn says:
    "SERIN ... Data can be sent using as little as two wires; one for data and one for ground. "

    I only have one communication wire: P1 to P1 for data. Do I need to connect the grounds of the two boards?
    (To control start of communication I also have P0 to P0 for chip select)

    Thanks.
  • John KauffmanJohn Kauffman Posts: 653
    edited 2008-04-13 04:07
    I've trimmed the mother and daughter programs to be even simpler:
    1. Mother sends request with CS high
    2. Daughter sends the number 7 (hard coded)

    They run without error, but the value mother is reporting bounces around from 5 to 7 to 1.

    I still suspect the timing - could the mother be reading half a message from the daughter?

    I've tried a pause in daughter code to let mother get ready for Serin, but no luck

    Thanks.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2008-04-13 04:36
    Mother is·getting a numeric value from Daughter, but I think that Daughter needs to follow "value" with a non-numeric ( --> SEROUT D_Data,396,[noparse][[/noparse]7, CR] 'send the value (7 hard coded for test) ), which will cue Mother that the numeric is done.

    Better have a look at SERIN/SEROUT in PBASIC Help, there's a diagram showing how to connect to Stamps for communication.
  • kennetkennet Posts: 33
    edited 2008-04-13 05:28
    For precise communication between two Stamps you will need to use the Flow Control function offered with the Serin/Serout commands. It allows the two Stamps to work in precise timing sequence. In other words the Master·will only
    communicate with the Slave when the Slave is ready and the Slave·will only communicate with the Master when the Master is ready. This protocol is quite useful.

    Nuts & Volts has a very good article on this method. The article is called ' A Tale of Two Stamps' (Column#81,January 2002).The article shows the exact way to connect the two Stamps, and how flow control works. Use this information as a guide, then go back to the Editors's Help section and break down the the Example Programs in the back of the Serin and Serout sections.

    Ken
  • John KauffmanJohn Kauffman Posts: 653
    edited 2008-04-13 06:28
    Thanks (with embaressment, I was using stupid keywords in my searches).
  • MSDTechMSDTech Posts: 342
    edited 2008-04-13 12:36
    Since the Basic Stamp does not have interrupt processing, you need to run the system as a "pure" master - slave system. In the "pure" system, the slave sits and waits for a request from the master. I have two Basic Stams operating in a data collection system where the slave is connected to the sensors while the master controlls the data colection, recording and reporting. The serial communication is through a pair of eb501-SER bluetooth modules which have been paired using the A7 easyconnect feature.

    To initate a converstion, the master sends a message prefixed with the characters DR and followed by a one character code indicating the sensor to be read (in the variable instrument). This is the data request code:

    Get_Data:
    · SEROUT bt_tx, BlueBaud,[noparse][[/noparse]"DR",Instrument]
    · SERIN bt_rx,BlueBaud,5000, Oops,[noparse][[/noparse]WAIT (Instrument), DataIn1,DataIn2]
    · SystemStatus = OnLine
    RETURN

    Oops:
    · SystemStatus = OffLine
    GOTO Main

    Variables used:
    bt_tx - the pin number connected to the eb501 transmit line
    bt_rx - the pin number connected to the eb501 receive line
    BlueBaud - a constant setting the serial baud rate
    Instrument - a byte variable containing the instrument to be read by the slave
    DataIn1 & DataIn2 - the high and low bytes of a word variable used to store the returned reading. I used this method as some of the instruments only return a 1 byte data value and some return a word for data.
    SystemStatus - a bit variable to indicate if the instruments are responding.

    The slave uses the following code:

    Main:
    DO
    SERIN bt_rx,bluebaud,[noparse][[/noparse]WAIT ("DR"), Instrument]

    SELECT Instrument
    · CASE "S"
    ··· VoltSelect = SolarV1
    ··· GOSUB ReadVoltages
    · CASE "U"
    ··· VoltSelect = SolarV2
    ··· GOSUB ReadVoltages
    · CASE "N"
    ··· VoltSelect = SolarV3
    ··· GOSUB ReadVoltages
    · CASE "B"
    ··· VoltSelect = BattBus
    ··· GOSUB ReadVoltages
    · CASE "E"
    ··· GOSUB ReadTempDs
    · CASE "T"
    ··· GOSUB SHT_Measure_Temp
    ··· DataValue=soT
    · CASE "H"
    ··· GOSUB SHT_Measure_Humidity
    ··· DataValue=soRH
    · CASE "W"
    ··· GOSUB MeasureSnow
    · CASE "R"
    ··· GOSUB MeasureRain
    · CASE "X"
    ··· GOSUB Reset_Snow
    · CASE "Y"
    ··· GOSUB SetupADC
    · CASE "Z"
    ··· GOSUB CalibrateADC
    ENDSELECT
    SEROUT bt_tx,bluebaud,[noparse][[/noparse]Instrument,DataOut1,DataOut2]
    LOOP

    Note, since no timeout value is given on the slave, it will sit at the SERIN command forever waiting for the master to request data. The master, on the other hand, is not so patient. I allow 5 seconds for the slave to read the appropriate sensor and return a value. If nothing has come back I note the slave is Off-line and start again.

    This is only one method to accomplish a master/slave, but I've found it works very well with my two units.
  • kennetkennet Posts: 33
    edited 2008-04-13 12:42
    One further word of note. The material on Serin/Serout commands is pretty extensive. I would suggest reading the material on Serin commands and the Serout commands at the same time. This way , you can draw direct refference on each commands effect on the other.



    Ken
Sign In or Register to comment.