This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

convert decimal to binary and contrary !
#1
Hi guys !
I have a little problem, I have 3 problems, please help! :
- I want to convert Decimal value to binary, or Hex to binary.
Example: Number 5000 converts to 0001 0011 1000 1000, suppose assigning to string b='0001 0011 1000 1000'
- After getting string b, we can read the value of each bit in string b (read each position from bit 0 to bit 15).
For example, for the string b='0001 0011 1000 1000', I read bits from right to left, the 4th place is "1", the 10th and 11th places are "1" and "0".
for the string b='0001 0011 1000 1000', I also want to read the ordinal bits, the 9th and 10th and 11th positions are "111", the 3rd and 4th positions are "10"
How can I do this?
This is "how to convert dec to bin"
https://www.rapidtables.org/vi/convert/n...inary.html
- I have the string b='0001 0011 1000 1000'. I want to record the 4th value =1 (from right to left) as =0 . What should I do Sad.After assigning I want to convert this string to decimal number
Thank you very much everyone! Smile
Reply
#2
Use this function, it returns a number that is stored in bits starring from offset to offset + length (not inclusive). offset is the lowest bit number that you want to get. length is number of bits to get.
Code:
function getbits(value, offset, length)
  local mask = bit.lshift(1, length) - 1
  return bit.band(bit.rshift(value, offset), mask)
end

value = 5000
result = getbits(value, 9, 3)
log(result)

Note that bits are counted from right to left starting from 0. So bits 9..11 are this: 0001 0011 1000 1000
Reply
#3
Specifically I am wanting to control the air conditioner via modbus rtu. But my device only sends back the value of the register as Decimal for LM. But the modbus documentation of the conditioner must read and send commands via binary value. So I have to convert from Decimal to Binary. But the problem now how do I send the command into any of the 0-15 bits? Through Modbus poll software, I can do this, but on LM I can't do it Sad
I am very happy for your help! But how do I convert the value 5000 to a binary value, and vice versa I have 1 binary value and then convert it to a decimal value? Smile

Attached Files Thumbnail(s)
           
Reply
#4
You can write bits to modbus, there are many examples see this
https://forum.logicmachine.net/showthrea...8#pid25448
------------------------------
Ctrl+F5
Reply
#5
oh..thank so much Daniel ! I will check. Smile

I read through this, but don't quite understand, sorry I'm new to modbus.
Hope everybody help please!
I have the following example, the value of the register is as follows (42014):
To select the dry mode is:
0100 0010 0000 0111 16903 dry mode
To select the cool mode is:
0100 0010 0000 0010 16898 cool mode
To select fan mode is:
0100 0010 0000 0000 16896 fan mode
Thus, when selecting modes, we only need to send the change command for 3 bits 0,1,2 from left to right (here, 111, 011, and 010). How can I send these bits? Sad . Please help me.
Thank so much !
Reply
#6
This can and should be done via a profile. Mapping entry should look like this. I'm not sure about the address 42014, it might be 2014 or 2013.
Code:
{
  "manufacturer": "Manufacturer name",
  "description": "Model name",
  "mapping": [
    {
      "name": "mode",
      "bus_datatype": "int16",
      "datatype": "uint8",
      "type": "register",
      "address": 42014,
      "writable": true,
      "write_bitmask": true,
      "value_bitmask": 7
    }
  ]
}

Later you can assign custom values to the mapped object to convert numerical values to text.

You can use Windows calculator in programmer mode to get bitmasks for certain bits. Switch to bit input mode, then select the required bits (0..2 in this example). Use the decimal value ask value_bitmask (7 in this example).
   
Reply


Forum Jump: