Library for Modelltrains


could some one please give me a link to a LocoNet or XpressNet library for arduino? Or make one?

If you can make me one, here are the documentations:



XpressNet is a RS458 protocol so it shouldn't be so hard to write a library.

Please help me!!!


If it isn't so hard why don't you start writing it yourself?

I started on a library for XpressNet but gave up as I came to the priority, call etc bits. I have programmed a lot in my live and I am on the Qt Dev Days in SFO right now but I have never worked with serial or writing any library.

So don't assume I haven't even looked at it, because I have.

I don't mind if it needs a different ATmega to convert from XpressNet to USART to get better timing, if it needs to.


In which case, you need to make your questions more specific. As you've found, asking "will you folks do this for me" isn't going to get much traction. But asking, "Is it possible to do 9 bit serial?", is much more likely to move the project along.

So, does anyone know if it is possible to do 9 bit serial with an Arduino? Has anyone done this? Is anyone willing to share a bit of code?

From what I can tell, the first byte of a message has the "ninth bit" set high and the remaining bytes have the "ninth bit" set low. This appears to be a way to wake / attention a slave by causing a "parity high" interrupt.

Ok, thanks Coding Badly.


Has anyone any clue about if it's possible to do this 9-bit serial with arduino? Does anyone perhaps have some code?


Hi Jan,

Have a look at the NewSoftSerial library, latest version @ There is a function called - void NewSoftSerial::write(uint8_t b) - in the cpp file that clocks out 8 bits using the mask to count. This could be the basis for clocking out your 9 bits. As 9 bits does not fit in a byte, you will have to use an int for the mask and for the value being sent, and a loop that goes from 1…9 of course.

for (byte mask = 0x01; mask; mask <<= 1)
      if (b & mask) // choose bit
        tx_pin_write(HIGH); // send 1
        tx_pin_write(LOW); // send 0

Hopes this get you started.

I was just looking at the ATMega328 datasheet, and there was some C code in there for doing 9-bit data transmission with the hardware UART...

Do you mean this to Transmitt?:

void USART_Transmit( unsigned int data )
/* Wait for empty transmit buffer */
while ( !( UCSRnA & (1<<UDREn))) )
/* Copy 9th bit to TXB8 */
UCSRnB &= ~(1<<TXB8);
if ( data & 0x0100 )
UCSRnB |= (1<<TXB8);
/* Put data into buffer, sends the data */
UDRn = data;

And this to Recieve?:

unsigned int USART_Receive( void )
unsigned char status, resh, resl;
/* Wait for data to be received */
while ( !(UCSRnA & (1<<RXCn)) )
/* Get status and 9th bit, then data */
/* from buffer */
status = UCSRnA;
resh = UCSRnB;
resl = UDRn;
/* If error, return -1 */
if ( status & (1<<FEn)|(1<<DORn)|(1<<UPEn) )
return -1;
/* Filter the 9th bit, then return */
resh = (resh >> 1) & 0x01;
return ((resh << 8) | resl);

If yes, should I put the two functions into the end of the code for the XpressNet transmitter/reciever and when use the two functions like Serial.print(); and; ? Or is there any difference between how they work?