hash 1-247 question

chrisxzy

New member
Joined
Jan 12, 2022
Messages
7
Hi,
I hope you will all excuse me - I'm not a maths expert.
I have been trying to find a way to identify an electronic module by its serial number.
I need to assign each module with a unique number between 1 and 247. (This is an industrial modbus slave address)
There are usually up to 10 of these modules connected together, so I need a way of uniquely addressing each one,
and allocating them an address in the range above without a duplicated address.
e.g. unique serial number: CT1400-523
My question is:
Is it possible to create an algorithm which will do this or is it impossible?
Thanks
Chris
 
I am not aware of any such algorithm, and I doubt it is doable. Unless you keep track of you numbers, which would be a typical "non-hash" approach, you would have to pick your numbers in some randomized manner. My quick and dirty script tells me that if you randomly pick 10 numbers out of 247 your chance of having them all different is about 83%.

P.S. There is a popular probability puzzle: if you have 25 people in a party what are the chances that at least two of them have birthdays on the same date?
 
25 people in a party what are the chances that at least two of them have birthdays
50:50
But are there any other approaches that you could use?
Off the top of my head- perhaps you could read all the numerical parts of the serial numbers, put them end to end and rotate them until you find 10 different numbers, all between 1 and 247?
e.g. 001 523 043 123 456... etc. Or am I talking rubbish!!!
 
There are over 6 billion possible serial numbers. You want to assign each of those to one of 247 addresses. That assignment obviously cannot be done uniquely. You need a table.
 
50:50
But are there any other approaches that you could use?
Off the top of my head- perhaps you could read all the numerical parts of the serial numbers, put them end to end and rotate them until you find 10 different numbers, all between 1 and 247?
e.g. 001 523 043 123 456... etc. Or am I talking rubbish!!!
523 is not between 1 and 247. Or am I missing something here?
 
Perhaps there is some way of using the last two digits and modifying them if they were not unique.
Anyway, thanks for your help!
 
P.S. There is a popular probability puzzle: if you have 25 people in a party what are the chances that at least two of them have birthdays on the same date?
If I remember correctly, we need >23 people to have a chance of >50%.
 
Perhaps there is some way of using the last two digits and modifying them if they were not unique.
Anyway, thanks for your help!
If all you need is 10 unique numbers then why not use the numbers from 1 to 10?
I still cannot help thinking that I am missing something in the description of your problem.
 
If all you need is 10 unique numbers then why not use the numbers from 1 to 10?
I still cannot help thinking that I am missing something in the description of your problem.
I think the problem the OP set himself is impossible. He wants to associate serial numbers and addresses. Straight forward table look up. Using binary search, the thing is very quick. But, for some reason, he wants an algorithm to decipher the address from the serial number itself. Whether it is 10 addresses or 247 is really not the issue because there is no way that you can map uniquely from 6 plus billion possible serial numbers to 10 or even 247 possible addresses. What he wants to do is precluded by the pigeon hole principle.
 
... there is no way that you can map uniquely from 6 plus billion possible serial numbers to 10 or even 247 possible addresses.
True, but hashing is still usable in practice, albeit for numbers much larger than 247. E.g., SHA256 sums map any file to just 256 bits even though there is no way one can map uniquely all possible files to just 256 bits. But 256 bits numbers have the upper bound of more than [imath]10^{77}[/imath], which is much more than 247, so I agree that in this situation hashing is not likely to succeed :)
 
True, but hashing is still usable in practice, albeit for numbers much larger than 247. E.g., SHA256 sums map any file to just 256 bits even though there is no way one can map uniquely all possible files to just 256 bits. But 256 bits numbers have the upper bound of more than [imath]10^{77}[/imath], which is much more than 247, so I agree that in this situation hashing is not likely to succeed :)
We are not disagreeing. I have not seriously thought about hashing for over 50 years, but I am not even sure that what the OP was talking about is traditional hashing because of the uniqueness requirement.
 
Sorry to have caused such a kerfuffle!
I should have said that it is necessary for each device to compute its own unique address from its serial number (which follows the same format of CT1400-523) from a fixed algorithm without knowledge of the other addresses. The last 7 numbers are all variable, 0-9
As you say, this is probably not possible - or is it?
Chris
 
Sorry to have caused such a kerfuffle!
I should have said that it is necessary for each device to compute its own unique address from its serial number (which follows the same format of CT1400-523) from a fixed algorithm without knowledge of the other addresses. The last 7 numbers are all variable, 0-9
As you say, this is probably not possible - or is it?
Chris
I agree with @JeffMM here: you cannot uniquely map 6 billion inputs to 247 outputs -- as soon as you pick 248 of them at least two will end up mapped to the same output.
 
Thanks, but perhaps I dont understand - I only want up to 10 devices?
True, but if you compute you small 3-digit addresses based exclusively on 7-digits inputs I don't see how you can guarantee (with high enough probability, that is) that you don't get duplicates.
 
If you have access to program the software on these devices, then perhaps you can set up something similar to DHCP. This is where a single server has control of address allocation. A startup "handshake" between each new device and the server assigns a unique address to each device.
 
Thats probably the best idea so far, but maybe I could just broadcast a message to all of them simultaneously and each would reply after a delay of some seconds based upon a combination of their serial numbers.
An immediate reply from the master would then allocate an address based upon the combination and the way it was calculated..
Any identical transmissions would be corrupted and not answered. This could then be repeated using a different method of calculating the delay from the devices, until they all have their individual address. They will each know how to calculate it, and by which combination.
Needs some thought in the software though!!
 
...
Needs some thought in the software though!!
Yes some thought will be necessary unless you can find a pre-existing solution. Modbus has been around for many years so it seems likely that someone else must have had this kind of requirement. But after a quick google search I didn't see anything. Maybe most people prefer to set a known address on every device via some physical method (like dip switches)

An alternative suggestion in software is to use a binary chop on the serial numbers. The master could broadcast a message that says "if your serial is between xxxxxx and yyyyyy then assign yourself modbus address mmm and immediately send an acknowledgment message."
- If there's a single clear response, within a certain time, then ONE device has a serial number in that range
- If there is no response then no device has a serial in the range
- If the response is garbled, or multiple replies are received, then multiple devices have serial numbers between xxxxxx and yyyyyy. Therefore queue up another two broadcast messages:- one between xxxxxx and zzzzzz; and another one between zzzzzz+1 and yyyyyy WHERE zzzzzz is the midpoint.

EDIT: If the response from a slave includes that device's serial number in the message, then you might be able to optimise the situation where multiple responses are received.

Obviously you'll need to think about the ways that this system might fail. There's obviously a single point of failure with the master device (but this might be an inherent feature of Modbus anyway?). Should the master always expect 10 slave devices? What does the master do if it can only discover 9 (or fewer) devices? Etc
 
Last edited:
Handy advice! Thanks.
However, I might just avoid all the complications and put a dip switch on as you suggest.
I thought there might have been an elegant solution, but not so.
Thanks all.
Chris
 
Top