Electronic – Multiplexing serial line

microcontrollerserial

I have built this ID sensor for a Scalextric slot car track, based on a PIC12F629. The ID sensor sends the ID of a detected car as a RS232 signal on one pin (TTL level).

My question is, how can I receive data from like four of these micro controllers into another micro controller's (PIC18F2550) USART?

I came up with these possibilities:

  1. Just connect all of the serial lines "directly" to the RX pin of the PIC18 and hope that two cars doesn't pass any sensor that close in time so the signals are overlapping each other. This could actually be a good start and will probably work 99.9% of the time. I mean, the mathematical probability that two cars are detected that close in time cannot be worth the effort of the other suggestions…after all, it's a hobby project.
  2. Implement a busy signal that is raised when the ID sensors sends data and checked before sending.
  3. Add some fancy multiplexer chip that eats the serial signals and outs them on a single line.

Each ID sensor chip will be coded with an identifier that is sent as part of the data, so they can be separated at the receiving end.

Update: Added some more info about the sensor hardware.

Best Answer

If you set your protocol up so the cars only respond when queries by the controller, then you can just tie (at the logical level, at least) everyone together.

The trick is whether you have real RS-232 (with 1 = -6 to -12 V, and 0 = 6 to 12 V) or just a standard logic line (1 = VCC, 0 = GND). Either the data sheet or a scope should answer this.

If it's standard logic, it could be really easy. If your sensors can control their output drivers, then program them to not drive the output unless a message is going out. If you have to leave the output driver on all the time, have it drive a transistor or two to make an "open-collector" configuration, connect all the sensors' collectors together, and pull up the connected line to VCC and hook it to your main controller's RX pin. This works because the RS-232 protocol idles at a logic 1 state. If RS-232 signal levels are used, then you have to change the transistor configuration a bit, but it'll still be open-collector at heart.

The main controller simply asks each sensor for its data in sequence. Each sensor responds when queried. That way you don't have more than one driving the RX line at a time, which is the main goal.

Now if you can't get your sensors to speak only when spoken to... then your problem got a lot hard. So much that the simple answer is to give each sensor its own serial port, maybe using 8-pin controllers as sensor managers, which can then be hooked up on a cooperative serial bus. Other techniques, such as collision detection with message retransmission (like 10base-T Ethernet did), are much more complex.