Need help creating a library for RS-BUS

Hello!

I'm a bit outside my comfort zone and need some help. I need to create a library that can send data over a Bus called RS bus. This bus is used for relaying sensor data from a model railroad to its control system (Command station). To my understanding this bus is a serial interface with 8 data bits and one parity bit.

Background
I'm building a model railroad layout and I want (of course) automate this. To do this I need to know where my trains are and relay this information to my Command System that controls all the turnouts. To buy a relaying device (Feedback module) that can relay data from 16 sensors would cost about € 50. I'm building a small layout, but i would still need 28 sensors to be able to automate this. This made me interested in making my own relaying unit with an Arduino.

I started googling and didn't find as much as I hoped for. I did not find one Arduino project that could help me. But I found source code for an Atimel processor (Not the one on my Arduino UNO). I thought I could use this, but this proved to be too much for my knowledge and this i why I'm here. Below you can found the information I found out:

Facts about the RS-Bus
The command station I'm using is Lenz LZV100. This is described as Master below. I want to create a Feedback module, (Above referred as a relaying device) Below I will refer this as a Slave.

The Master sends out a pulse train of 130 pulses (aprox. 109us high and 96us low) After this it sends out a pulse of aprox 7ms.
If a slave has a message to send it will send its package at 'its' pulse. Each pulse in the masters pulse train corresponds to a adress of the slave.

Baud 4800 bit/sec
The parity bit is sent first, unlike ordinary UART.

The slave Sends its data package in two bytes. The data will look like this:

  • StartBit
  • ParityBit (Even)
  • Two TT Bits (Will always be 10 for a Feedback module
  • 1 Nibble bit (0 = lower nibble, 1 = upper Nibble)
  • 4 data bits (Lower or upper nibble depending the above bit)
  • StopBit

The above information comes from this German site:
http://www.der-moba.de/index.php/RS-Rückmeldebus

The code I found using this is found here:

Conclution
I would like some help writing a library for Arduino som I can relay sensor data from digital pins of my Arduino to my command station. I'm thinking och two functions:

RSBusInit(unsigned char Adress)
RSBusSend(unsigned char DataToSend)

Where RSBusInt(Adress) initializes the data transfer with the slave adress Adress
And the RSBusSend(DataToSend) Divides the data into two nibbles and sends this data to the master on it's turn.

Is any one interested in helping me with this?

A slave has to split 8 data bits into two transmit bytes (some bit fiddling). Then it sends the first byte (containing the lower data nibble) in response to its address bit in the master pulse train, the second byte in the next pulse train.

Counting the pulses and starting a transmission immediately after the right pulse may become tricky. I'm not sure how the Serial buffer may affect that timing. A scope or logic analyser will be helpful.

Best is to have a look at the implementation of softwareSerial. it looks you need something similar.

When you use the hardware serial the code could look something like snippet below

RSBusSend(unsigned char DataToSend)
{
   byte b[2];
   b[0] = DataTosend >> 4;
   b[0] |= 0x10;  // indicate high nibble 
   b[0] |= 0x40;  // TT bits
   bool parity = true;
   for (int mask = 0x01; mask < 0x80; mask <<= 1)
  {
    if (mask & b[0]) parity = ! parity;  
  }
   if (parity) b[0] |= 0x80;

   b[1] = DataToSend & 0x0F;
   b[1] |= 0x00;  // indicate high nibble 
   b[1] |= 0x40;   // TT bits
   parity = true;
   for (int mask = 0x01; mask < 0x80; mask <<= 1)
  {
    if (mask & b[1]) parity = ! parity;  
  }
   if (parity) b[1] |= 0x80;

   Serial.write(b[0]);
   Serial.write(b[1]);

/*
   // implement yourself
   sendStartBit();
   send(B[0]);
   sendStopBt();
   sendStartBit();
   send(B[1]);
   sendStopBt();
*/
}

hope this helps...

yes it can be written more efficiently