Skip to main content

Reply to "Sending TMCC Commands to Legacy Base over Serial Connections"

Professor Chaos posted:

Chris, your code would not work.  Remember, the TMCC addresses are NOT aligned on the byte boundaries.  As shown on page 36 of the TMCC manual, a switch command is:

01AAAAAAACCDDDDD  (where AAAAAAA is the TMCC id)

splitting that into two bytes gets you

01AAAAAA ACCDDDDD

so part of the TMCC id is in the third byte.  As in my code fragment above, what you want to do is shift and OR the bits to assemble the bytes:

Byte2 = B01000000 | (TMCC_SwitchID[3] >> 1);
Byte3 = (TMCC_SwitchID[3] << 7) | TMCC_Out;

Serial.write(0xFE);
Serial.write(Byte2);
Serial.write(Byte3);



I would look at the Arduino Reference page, specifically the parts about bitwise OR and bitwise shifting, to understand how the byte assembly works.

Geez! I got so got up in the method of sending the commands that I forgot. My bad. I updated my code as you suggested, which I like because I would like to just add the switch id and let the code handle the rest. It would be a pain to have to put all the different code for the different switch ids in the code. Here is my updated code: 

The variables: 

// ---- TMCC Variables
int TMCC = 0xFE;
byte TMCC_Switch = B01000000;
byte TMCC_SwitchID[4]= {B0000001, B0000010, B0000101, B0001010}; /* Binary Code for each switch used by the program */
/* 1 2 5 10 */
byte TMCC_Thru = B0000000; /* Binary Code for throwing the switch in the through position */
byte TMCC_Out = B0011111; /* Binary Code for throwing the switch in the out position */

// ---- Bytes to Send to Base
byte Byte2;
byte Byte3;

 

The code: 

 Byte2 = TMCC_Switch | (TMCC_SwitchID[3] >> 1);
Byte3 = (TMCC_SwitchID[3] << 7) | TMCC_Out;
Serial.write(TMCC);
Serial.write(Byte2);
Serial.write(Byte3);

 

Professor Chaos posted:

I realized that the Sparkfun page I linked to is not actually a MAX232 circuit - you might want to use one that is a real MAX232 as some people report problems with the Sparkfun adaptor.

 

Professor Chaos would this be the MAX232 you are referring to? 

Professor Chaos posted:

You can look at my code here (the Train Control sketch) to see how I did the TMCC buffer thing - it's at the top of the _interface tab.  But I am afraid the code is very complex and probably not too easy to understand.  It also depends on some libraries that are in the libraries folder.

Thank you. I will take a look at this. 

Professor Chaos posted:

I am reading the state of DZ-2500 switch machines by tapping into the line that normally lights red or green LEDS on the pushbutton controller.  If I recall correctly, I stuck a few diodes in series with that line, and fed it into an optocoupler. That gave me a logic level signal representing the state of the switch machine.  But that may be getting more complex than you want to try at first.

I will have to keep this in mind at least I know there is away to keep the state of the switch updated. I was thinking that if Lionel ever released this. That it would update the base the state of the switch, I am not sure that the base would transmit that over "serial" or if the arduino would be able to read the commands the switch monitor was sending to the base. 

What I want to do now is get a arduino and SER2 and try this out to see how the base is outputting the commands. Do either you know if the commands outputted from the base are the same as the ones that go in or is it different?

My next question is probably more Arduino related, but if I have arduino monitoring the output from the command base, sending to the command base, and monitoring buttons attached to the arduino. Is it best to use if statements for each? I think it would be better to check the output from the base first, then check the buttons, and then send commands to base. For example. 

if(Serial.readBytes() != 0){
//do something
}
if(Button = HIGH) {
//do something and then do a serial write
}

 

Thanks, 

Chris 

OGR Publishing, Inc., 1310 Eastside Centre Ct, Suite 6, Mountain Home, AR 72653
800-980-OGRR (6477)
www.ogaugerr.com

×
×
×
×
×