Welcome to the forum   
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Share
Options
View
Go to last post in this topic Go to first unread post in this topic
Offline fosuna  
#1 Posted : 10 January 2020 07:42:44(UTC)
fosuna

Spain   
Joined: 14/04/2015(UTC)
Posts: 28
Hi everybody!

I'd like to make a program to control my Märklin layout in H0. For this purpose, I have read the Märklin document "CAN CS2 Protokoll -2.0.doc".

Well, I understand it more less, and even I think I could make the commands to send to the CS2 for my purpose.

But I don't know which data must be in the "Hash" filed in the CAN-Bus-Id. The Hash field is a 16-bit / 2-byte filed. In the examples shown in that document, the value of this field is "(0x) 47 11". But I do not know why, neither the value must be entered in each case.

Anyone knows how to manage and use this field?

Thank you very much! And have a nice day.

Francisco


Offline bertc3p0  
#2 Posted : 01 February 2020 01:53:13(UTC)
bertc3p0

Germany   
Joined: 03/03/2014(UTC)
Posts: 10
Location: HESSEN, FRANKFURT
Originally Posted by: fosuna Go to Quoted Post
Hi everybody!

I'd like to make a program to control my Märklin layout in H0. For this purpose, I have read the Märklin document "CAN CS2 Protokoll -2.0.doc".

Well, I understand it more less, and even I think I could make the commands to send to the CS2 for my purpose.

But I don't know which data must be in the "Hash" filed in the CAN-Bus-Id. The Hash field is a 16-bit / 2-byte filed. In the examples shown in that document, the value of this field is "(0x) 47 11". But I do not know why, neither the value must be entered in each case.

Anyone knows how to manage and use this field?

Thank you very much! And have a nice day.

Francisco


Example:
Code:
/*
  CS2 CAN hash generation

  xxxxxxx11 0xxxxxxxx
  ^^^^^^^    ^^^^^^^^
   \\\\\\\   ||||||||
    \\\\\\-- ||||||||
     \\\\\\ \||||||||
  000xxxxxx xxxxxxxxx

 */
uint16_t generateHash(uint32_t uid) {
    uint16_t hash, highword, lowword;

    highword = uid >> 16;
    lowword = uid & 0xFFFF;
    hash = highword ^ lowword;
    hash = (((hash << 3) & 0xFF00) | 0x0300) | (hash & 0x7F);
    return hash;
}

The CAN-ID is a 29bit Value composed the following rule:

0x00cchhhh

where cc ist the command shifted left by one and hhhh ist the hash like 0x4711.

Regards

Gerd
thanks 2 users liked this useful post by bertc3p0
Offline fosuna  
#3 Posted : 24 May 2020 13:38:57(UTC)
fosuna

Spain   
Joined: 14/04/2015(UTC)
Posts: 28
Hello bertc3p0!

Thank you very much for your post. Sorry for the delay, but I read your message a couple of days ago.

Well, let me ask you some questions, just in case you know the answers.

1) In the C code you show for a HASH generation, I understand that a 'x' stands for a '0' or a '1'. But, where come these bits from? How they are generated? In a random way, for example?

2) Why does the high order byte end in '11'? And why the low order byte starts with '0'?

3) Why is it neccesary to make all bits opartions which are made to add three '0' bits to the high order byte, shiftting the bits with a varaible value (as I unsderstand)? I mean, why must it pass from "xxxxxx11 0xxxxxxx" to "000xxxxx xxxxx"?

I saw all of these in the Märklin "CAN CS2 Protokoll 2.0.doc" documment, but I do not understand why all of these.

Thank you very much, and have all a nice day.

Francisco
Offline fransja  
#4 Posted : 05 October 2020 08:26:26(UTC)
fransja

Netherlands   
Joined: 27/09/2020(UTC)
Posts: 7
Location: Zuid-Holland, Papendrecht
Hi,

I am also writing a program to connect with the CS2 myself and got more or less the same issue.
I found out from the CAN manual which is served by the CS2 that the hash is calculated from the CS2 UID, so from what I now understand is that each CS2 has its own UID, which is used to calculate the hash with the described procedure. This hash is send by the CS2 every time, hence is does not change.

Regards,

Frans
Offline costing  
#5 Posted : 05 October 2020 21:15:00(UTC)
costing

Switzerland   
Joined: 20/08/2018(UTC)
Posts: 120
Location: Geneve, Geneva
Maybe the Railuino code can help here?
https://github.com/MBura...no/src/Railuino.cpp#L258

Cheers,

.costin
JMRI on RPi & DCC++ / C-track / Marklin (SBB Re 4/4 II, Ee 3/3, DB BR 24, BR 233), Roco (DB BR 103, BR 215, CFR 040-EC-001), ESU engineering (DB 265 MRCE) / Christmas car collector
Offline fosuna  
#6 Posted : 07 October 2020 20:13:11(UTC)
fosuna

Spain   
Joined: 14/04/2015(UTC)
Posts: 28
Originally Posted by: fransja Go to Quoted Post
Hi,

I found out from the CAN manual which is served by the CS2 that the hash is calculated from the CS2 UID, so from what I now understand is that each CS2 has its own UID, which is used to calculate the hash with the described procedure. This hash is send by the CS2 every time, hence is does not change.



Hi Frans! Thank you for your post.

Is the CAN manual which is served by the CS2 the "CAN CS2 Protokoll - 2.0.doc"? If so, where did you find that the hash is calculated from the CS2 UID? And if not, could you send me a copy of the pages of the CS2 manual where that is stated. I have not got a CS2, but instead a CS3.

It makes sense. As far as I understand, all the decoders and mfx-protocoll units have a unique UID from factory which identify them. Please, correct me if I'm not right. So set a Hash from this UID would be this Hash unique.

In Railuino.cpp (thanks .costin for your message), as far as I remember, when I looked at that program, the Hash is set up randomly. Could it be possible because the Arduino hardware does not come from factory with a UID valid for the Märklin CAN bus?

Greetings,

Francisco
Offline fransja  
#7 Posted : 08 October 2020 16:48:56(UTC)
fransja

Netherlands   
Joined: 27/09/2020(UTC)
Posts: 7
Location: Zuid-Holland, Papendrecht
Hi Francisco,

Yes, this document: http://streaming.maerkli...cs2CAN-Protokoll-2_0.pdf on page 6, paragraph 1.2.4: "Bit Wert wird gebildet aus der UID Hash" -> "Bit value is formed from the UID hash"
I can confirm that the CS3 does the same as I have tested that at my fathers track. He is using a CS3+.

If found somewhere (can't remember where, I am sorry) that the UID is calculated from the CS2/3 serial with this formula, but doubt that is correct: "uid = 0x4343200 + 2*serial".

So my guess is that the hash is unique for the UID.

Regards,

Frans
Offline fosuna  
#8 Posted : 09 October 2020 19:27:34(UTC)
fosuna

Spain   
Joined: 14/04/2015(UTC)
Posts: 28
Hi Frans,

Yes, I agree with you that the Hash is unique. By the way, do you know what is the exact function of the Hash field? I think it is a unique identifier for the units in a CAN Märklin bus, so that the diferent units connected in a CAN Bus can know which messages are for them. But I am not sure, as I do not know much about the CAN protocol.

Another question is the following. If a PC computer is connected to the CS2 or CS3, should the PC have a different Hash than that of the CS2/3? I think so. That is, the PC maybe is connected to the CAN Bus through an internal interface inside the CS2. In this case, it must have a diferent Hash than that of the CS2. And in this case, the problem is how to set and assign a Hash to the computer. A block diagram of the CS2 internals would be helpful, but I do not have it.

I made a very small program just to switch off the STOP from my computer. I set a Hash randomly, as it is done in the Railuino.cpp program, and it worked. But I would like to know well the role the Hash field plays in the CAN Märklin Bus. The Märklin docs about this is for me somewhat criptic.

Have a nice weekend!

Francisco
Offline fransja  
#9 Posted : 10 October 2020 14:18:05(UTC)
fransja

Netherlands   
Joined: 27/09/2020(UTC)
Posts: 7
Location: Zuid-Holland, Papendrecht
Hi Francisco,

What I so far made up from the Markin Documentation is that the PC does not need a UID.
I am working as a hobby to automate my layout. Started using the 6050 interface for controlling the Loco's.
Due to the fact that the 6050 baud rate is only 2400 bps I started to use a Raspberry Pi for the feedback (connected the S88 to the GPIO of the PI) and wrote a program for that in Java.

Now I have bought a second hand CS2. So I am adapting my software (in Java) to use the CS2.
I started debugging the commands using the PacketSender tool.
With this tool you can actually send the example of the documentation and see the results.

So this is what I know so far. Would be great if I could get some help on areas which do not work yet.

Started with the simple Stop and GO.
GO you need to send "00 00 cb 12 05 00 00 00 00 01 00 00 00" to the CS2.
The hash is calculated over the 4 first data bytes which are "00 00 00 00".
The CS2 response in my case with: "00 01 CB 13 05 43 53 9A 40 01 00 00 00".
Notice the second byte should be 01 as a response and the first 4 data bytes hold the UID in my case "43 53 9A 40".
I store the UID as an int so in my case: 1129552448.

I calculate the hash as follows:
int msb = uid >> 16;
int lsb = uid & 0xffff;
int hash = msb ^ lsb;
hash = (((hash << 3) & 0xFF00) | 0x0300) | (hash & 0x7F);

Next command which worked is STOP I send: "00 00 cb 13 05 43 53 9a 40 00 00 00 00" and the CS2 response is: "00 01 CB 13 05 43 53 9A 40 00 00 00 00";

I do not want to fill in the IP address of the CS2 in my software so found out the you could send a ping on UDP. The CS2 will respond to to that, hence you automatically know the IP address of the CS2.
My software starts with sending "00 31 47 11 08 4f 59 10 df 01 04 ee ee" to the UDP broadcast address. The CS2 will respond like so: "00 30 00 00 00 4F 59 10 DF 01 04 EE EE"
Notice that for some reason its starts with the response bit set. I could not find why this is in any documentation.
All other commands could be send using UDP, but also TCP will work.

So I have expanded my software, and can send commands for the turnout and signals. And the Loco's now also works.

I am stuck with the config data commands.
According to the documentation you can ask "loks" (a list of all locomotives in the CS2) but for some reason I can't get it to work.
I send: "00 40 3b 00 08 6c 6f 6b 73 00 00 00 00" and the response is: "00 41 CB 12 08 6C 6F 6B 73 00 00 00 00" but than, what I understand from the documentation, is it should return a stream with the locomotives. But nothing happens.

So May be somebody else has done this? Help would be appreciated.

So in the meanwile I will try to get the feed back working.

I hope I could help yo a bit,

Regards,

Frans
thanks 2 users liked this useful post by fransja
Offline DaleSchultz  
#10 Posted : 10 October 2020 15:11:55(UTC)
DaleSchultz

United States   
Joined: 10/02/2006(UTC)
Posts: 3,521
I have not read any of the docs nor tried any of this programming, but to me it sounds like the hash is designed to produce a unique identifier on the bus.

If so, generating a random hash is probably not a good practice, but lacking some sort of unique ID on which to base the hash, I suspect that the developer is simply hoping that the hash produced will be unique based on chance. Most of the time this will probably be true.

Dale
Intellibox + own software, K-Track
My current layout: https://cabin-layout.mixmox.com
Arrival and Departure signs: https://remotesign.mixmox.com
Offline fosuna  
#11 Posted : 12 October 2020 19:41:59(UTC)
fosuna

Spain   
Joined: 14/04/2015(UTC)
Posts: 28
Originally Posted by: fransja Go to Quoted Post
Hi Francisco,

What I so far made up from the Markin Documentation is that the PC does not need a UID.
I am working as a hobby to automate my layout. Started using the 6050 interface for controlling the Loco's.
Due to the fact that the 6050 baud rate is only 2400 bps I started to use a Raspberry Pi for the feedback (connected the S88 to the GPIO of the PI) and wrote a program for that in Java.

Now I have bought a second hand CS2. So I am adapting my software (in Java) to use the CS2.
I started debugging the commands using the PacketSender tool.
With this tool you can actually send the example of the documentation and see the results.

So this is what I know so far. Would be great if I could get some help on areas which do not work yet.

Started with the simple Stop and GO.
GO you need to send "00 00 cb 12 05 00 00 00 00 01 00 00 00" to the CS2.
The hash is calculated over the 4 first data bytes which are "00 00 00 00".
The CS2 response in my case with: "00 01 CB 13 05 43 53 9A 40 01 00 00 00".
Notice the second byte should be 01 as a response and the first 4 data bytes hold the UID in my case "43 53 9A 40".
I store the UID as an int so in my case: 1129552448.

I calculate the hash as follows:
int msb = uid >> 16;
int lsb = uid & 0xffff;
int hash = msb ^ lsb;
hash = (((hash << 3) & 0xFF00) | 0x0300) | (hash & 0x7F);

Next command which worked is STOP I send: "00 00 cb 13 05 43 53 9a 40 00 00 00 00" and the CS2 response is: "00 01 CB 13 05 43 53 9A 40 00 00 00 00";

I do not want to fill in the IP address of the CS2 in my software so found out the you could send a ping on UDP. The CS2 will respond to to that, hence you automatically know the IP address of the CS2.
My software starts with sending "00 31 47 11 08 4f 59 10 df 01 04 ee ee" to the UDP broadcast address. The CS2 will respond like so: "00 30 00 00 00 4F 59 10 DF 01 04 EE EE"
Notice that for some reason its starts with the response bit set. I could not find why this is in any documentation.
All other commands could be send using UDP, but also TCP will work.

So I have expanded my software, and can send commands for the turnout and signals. And the Loco's now also works.

I am stuck with the config data commands.
According to the documentation you can ask "loks" (a list of all locomotives in the CS2) but for some reason I can't get it to work.
I send: "00 40 3b 00 08 6c 6f 6b 73 00 00 00 00" and the response is: "00 41 CB 12 08 6C 6F 6B 73 00 00 00 00" but than, what I understand from the documentation, is it should return a stream with the locomotives. But nothing happens.

So May be somebody else has done this? Help would be appreciated.

So in the meanwile I will try to get the feed back working.

I hope I could help yo a bit,

Regards,

Frans


Hi Frans! Thanks a lot for your post. It helped me a lot.

I'll try to code some commands to my CS3 just for testing purposes. I'll notify you what I get.

Happy week for everyone!

Francisco


thanks 1 user liked this useful post by fosuna
Offline fosuna  
#12 Posted : 12 October 2020 19:45:56(UTC)
fosuna

Spain   
Joined: 14/04/2015(UTC)
Posts: 28
Originally Posted by: DaleSchultz Go to Quoted Post
I have not read any of the docs nor tried any of this programming, but to me it sounds like the hash is designed to produce a unique identifier on the bus.

If so, generating a random hash is probably not a good practice, but lacking some sort of unique ID on which to base the hash, I suspect that the developer is simply hoping that the hash produced will be unique based on chance. Most of the time this will probably be true.



Hi Dale!

You are right. But the matter is to generate a random one until the one generated is unique. This is what is done in the Railuino.cpp program.

Greetings,

Francisco
thanks 1 user liked this useful post by fosuna
Offline Drese Alain  
#13 Posted : 24 October 2020 15:02:18(UTC)
Drese Alain

Belgium   
Joined: 10/10/2020(UTC)
Posts: 7
Location: Vlaams-Brabant, Zaventem
Originally Posted by: fransja Go to Quoted Post
Hi Francisco,

What I so far made up from the Markin Documentation is that the PC does not need a UID.
I am working as a hobby to automate my layout. Started using the 6050 interface for controlling the Loco's.
Due to the fact that the 6050 baud rate is only 2400 bps I started to use a Raspberry Pi for the feedback (connected the S88 to the GPIO of the PI) and wrote a program for that in Java.

Now I have bought a second hand CS2. So I am adapting my software (in Java) to use the CS2.
I started debugging the commands using the PacketSender tool.
With this tool you can actually send the example of the documentation and see the results.

So this is what I know so far. Would be great if I could get some help on areas which do not work yet.

Started with the simple Stop and GO.
GO you need to send "00 00 cb 12 05 00 00 00 00 01 00 00 00" to the CS2.
The hash is calculated over the 4 first data bytes which are "00 00 00 00".
The CS2 response in my case with: "00 01 CB 13 05 43 53 9A 40 01 00 00 00".
Notice the second byte should be 01 as a response and the first 4 data bytes hold the UID in my case "43 53 9A 40".
I store the UID as an int so in my case: 1129552448.

I calculate the hash as follows:
int msb = uid >> 16;
int lsb = uid & 0xffff;
int hash = msb ^ lsb;
hash = (((hash << 3) & 0xFF00) | 0x0300) | (hash & 0x7F);

Next command which worked is STOP I send: "00 00 cb 13 05 43 53 9a 40 00 00 00 00" and the CS2 response is: "00 01 CB 13 05 43 53 9A 40 00 00 00 00";

I do not want to fill in the IP address of the CS2 in my software so found out the you could send a ping on UDP. The CS2 will respond to to that, hence you automatically know the IP address of the CS2.
My software starts with sending "00 31 47 11 08 4f 59 10 df 01 04 ee ee" to the UDP broadcast address. The CS2 will respond like so: "00 30 00 00 00 4F 59 10 DF 01 04 EE EE"
Notice that for some reason its starts with the response bit set. I could not find why this is in any documentation.
All other commands could be send using UDP, but also TCP will work.

So I have expanded my software, and can send commands for the turnout and signals. And the Loco's now also works.

I am stuck with the config data commands.
According to the documentation you can ask "loks" (a list of all locomotives in the CS2) but for some reason I can't get it to work.
I send: "00 40 3b 00 08 6c 6f 6b 73 00 00 00 00" and the response is: "00 41 CB 12 08 6C 6F 6B 73 00 00 00 00" but than, what I understand from the documentation, is it should return a stream with the locomotives. But nothing happens.

So May be somebody else has done this? Help would be appreciated.

So in the meanwile I will try to get the feed back working.

I hope I could help yo a bit,

Regards,

Frans


Hi Frans,

I am having the same issues, trying to discover the loks, but do not get a positive reply.
I am using an ms2 and cc-schnitte device to link the pc with the gleibox.
Have you progressed in the meanwhile ?

Regards
Alain

Offline fransja  
#14 Posted : 25 October 2020 20:43:44(UTC)
fransja

Netherlands   
Joined: 27/09/2020(UTC)
Posts: 7
Location: Zuid-Holland, Papendrecht
Hi Alain,

I made some progress today on my software.
After a long search I came to the conclusion that you can easily get the loc and magnet configuration using http on port 80 of the CS2.
You go to the http://<cs2/3 host address>/config/ there you find all de configuration of the CS2/3.
I do no have a clue why it is not working using the CAN command but http seems to work.

I have also started with adding feedback in my software.
The S88 polling command seems no longer supported, but the port events are transmitted.
I found that you can use UDP and put CAN broadcast on the CS2/3.

But it seems now that also TCP is supported. As you can't broadcast with TCP you have to poll regularly for some status.
I query the power status of the CS2/3. Message: 00 00 03 00 04 00 00 00 00 00 00 00 00, which returns "00 01 CB 13 05 43 53 9A 40 01 00 00 00 00 01 CB 13 05 00 00 00 00 01 00 00 00".
I do not (yet) understand why this is returning 2 CAN messages, as the first answer is the expected reply (the UID plus the status, in this case 01, track power on).
With the answer on the power status you get also the event messages when a sensor has changes like so:

I send 4 times per second "00 00 03 00 04 00 00 00 00 00 00 00 00"
Then reply is then "00 01 CB 13 05 43 53 9A 40 01 00 00 00" (expected)
second "00 01 CB 13 05 00 00 00 00 01 00 00 00" (no idea why)
third: "00 23 CB 12 08 00 00 00 01 00 01 00 41" which is a S88 event reply message for contact 1, changed from 0 to 1 for 410 ms.

The issue I am no facing is how to query all contacts when I start my program, as I now seem to get only a message when something is changing?

Is there any one who has experience in this area?

My experiments are on GitHub: https://github.com/fransjacobs/model-railway

Any help will be appreciated.

Regards,
Frans





Users browsing this topic
OceanSpiders 2.0
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

| Powered by YAF.NET | YAF.NET © 2003-2020, Yet Another Forum.NET
This page was generated in 0.713 seconds.