Author Topic: WHY Would one Command Work and the other not? HEX COMMAND  (Read 384 times)

Offline Newzwaver

  • Full Member
  • ***
  • Posts: 199
  • Karma: +4/-3
WHY Would one Command Work and the other not? HEX COMMAND
« on: April 11, 2017, 07:02:21 am »
HI

This will be my last question seeing they they go unanswered anyway but if you can help perfect if not no worry.

This command works to send the volume to 34, so 0 to 34 works perfect.
if ( lul_settings.newTargetValue == "34") then
        -- Volume Level -34\x02\x01\x01\x15\xE6\xFF
      local chksums1 = 0x02 + 0x01 + KeyString + 0x15 + 0xE6
        local sres, serr = c:send(string.char(0x02,0x00,KeyString,0x04,0x57,chksums)) -- zone on
      print("Send:", sres, serr)
      local sres, serr = c:send(string.char(0x02,0x01,KeyString,0x15,0xE6,chksums1)) -- volume 34
      print("Send:", sres, serr)
      local sres, serr = c:send(string.char(0x02,0x00,KeyString,0x04,0x57,chksums)) -- zone on
      print("Send:", sres, serr)
      local sres, serr = c:send(string.char(0x02,0x01,KeyString,0x15,0xE6,chksums1)) -- volume 34
      print("Send:", sres, serr)
      end
For some reason the only way to set volume is to turn it on then volume then on then volume.

This command doesn't work and from 35 to 60 will not set the volume.

      if ( lul_settings.newTargetValue == "35") then
        -- Volume Level -34\x02\x01\x01\x15\xE6\xFF
      local chksums1 = 0x02 + 0x01 + KeyString + 0x15 + 0xE7
        local sres, serr = c:send(string.char(0x02,0x00,KeyString,0x04,0x57,chksums)) -- zone on
      print("Send:", sres, serr)
      local sres, serr = c:send(string.char(0x02,0x01,KeyString,0x15,0xE7,chksums1)) -- volume 1
      print("Send:", sres, serr)
      local sres, serr = c:send(string.char(0x02,0x00,KeyString,0x04,0x57,chksums)) -- zone on
      print("Send:", sres, serr)
      local sres, serr = c:send(string.char(0x02,0x01,KeyString,0x15,0xE7,chksums1)) -- volume 1
      print("Send:", sres, serr)
      end

If you can help perfect, if not perfect.



Offline Newzwaver

  • Full Member
  • ***
  • Posts: 199
  • Karma: +4/-3
Re: WHY Would one Command Work and the other not? HEX COMMAND
« Reply #1 on: April 12, 2017, 06:04:51 am »
Anyone know how to send a hex string command with x014(i.e x### vs x##) vs xe1 and etc?  When the checksum is calculated it appears to be to large to be sent as a string in the plugin, it works in the scene.  I must have to set something up in the plugin to allow the string to be sent, any ideas???????????????????????????????

I get an error .....  'char' (invalid string).  The string is perfect as long as the hex stays to x## when it moves to X### then the string fails.  That is why when you go up in zones you get less volume, the number go up by one for each zone moved so volume comes back by one.  So if you can reach 34 volume in zone 1 you can only reach 33 in zone 2, 32 in zone 3 and so on.

Any help would be appreciated.

Online shallowearth

  • Hero Member
  • *****
  • Posts: 891
  • Karma: +41/-9
Re: WHY Would one Command Work and the other not? HEX COMMAND
« Reply #2 on: April 12, 2017, 08:06:35 am »
You often don't give enough info for people to get what you are doing making it very difficult for people to help you, which is why the response rate is so low.

So, it looks like you are trying to send IR strings to AV equipment via an iTach device? (These are not serial port commands correct? I am guessing here because you don't check for a response which is required for serial port commands usually).

What KeyString set to?
What is Checksums set to?

Can you give the whole snippet of code including the connect and close command?

If this is a iTach it usually wants a "/r" at the end of the string that is being sent, that maybe why you have to repeat commands.  Or you may need a tiny sleep at the end of your sequence to make sure the iTach completes all sends before the close  is sent and the scene exits, if your close the connection to the Itach after sending it a bunch of commands the close sometimes happens before the Itach can send all the strings and it will ditch whatever it had queued up dropping the last line sent.

Then finally to your actual question, what plug in are you talking about?  And what do you mean it works via a scene but not via "the plug in".  If you provide the value of "KeyString" and what plugin you are using that might make more sense.  Because in your fist example you say the LUA (I assume from a scene) doesn't work, but then you say it does but not via the plug in.




Offline Newzwaver

  • Full Member
  • ***
  • Posts: 199
  • Karma: +4/-3
Re: WHY Would one Command Work and the other not? HEX COMMAND
« Reply #3 on: April 12, 2017, 11:45:03 am »
Hi,
Thank you for the reply. I apologize for not including information required to offer support.  I have taken a number of plugins and put them together to control the HTD system via GW-SL21(RS232).  I have a working plugin for the HTD MA66 and working on one for the HTD Lync system..  It works for everything including volume up to 34.  To control the volume in a slider you need to send an on and then the volume.  I managed to get this working using the following code, again only to 0 to 34;
   local keyPad = luup.attr_get('altid', lul_device)
      local keyCode = luup.attr_get('altid', lul_device)
      KeyString = "0x" .. string.format( "%02x", keyCode )
      newtttxxx = lul_settings.newTargetValue + 196
      newttt = "0x" .. string.format( "%02x", newtttxxx )
      luup.log("Volume is 123 " .. newttt )
      local chksums = 0x02 + 0x00 + KeyString + 0x04 + 0x57
      newchkcc = "0x" .. string.format( "%02x", chksums )
      local chksums1 = 0x02 + 0x01 + KeyString + 0x15 + newttt
      newchk = "0x" .. string.char( chksums1 % 0x80 )
      --string.char((chksum % 0x80), 0xF7)
      luup.log("Volume chk " .. chksums1 )
      luup.log("Volume chkHH " .. newchk )
       
      -- Volume Level = newttt
        local sres, serr = c:send(string.char(0x02,0x00,KeyString,0x04,0x57,newchkcc)) -- zone on
      print("Send:", sres, serr)
      local sres, serr = c:send(string.char(0x02,0x01,KeyString,0x15,newttt,newchk)) -- volume
      print("Send:", sres, serr)
      local sres, serr = c:send(string.char(0x02,0x00,KeyString,0x04,0x57,newchkcc)) -- zone on
      print("Send:", sres, serr)
      local sres, serr = c:send(string.char(0x02,0x01,KeyString,0x15,newttt,newchk)) -- volume
      print("Send:", sres, serr)
      luup.log("Send:",sres, serr)   

However the code will only control the volume up to a setpoint of 34, 35 and up will not work.  Apparently the string.char is only good for 255 (0xFF).  Most everything else is working, still trying to get the feedback worked out. 

I have tried the following and it works for everything ewith ther exception of volume, the only thing that would work was the above and a scene.  The scene actually works from 0 to 60, so perfectly..
-- Mute Zone On When i send it in this manner but volume will not.
      local keyPad = luup.attr_get('altid', lul_device)
      local keyCode = luup.attr_get('altid', lul_device)
      KeyString = "0x" .. string.format( "%02x", keyCode )
      local chksums = 0x02 + 0x00 + KeyString + 0x04 + 0x1E
      -- Send command
      cmdString = { 0x02, 0x00, KeyString, 0x04, 0x1E, chksums }
      sendCommand(makeCommand(cmdString))

This is the scene

 

local socket = require("socket")
host = "192.168.2.88"
c = assert(socket.connect(host, 10006))
c:settimeout(5)

local sres, serr = c:send(string.char(0x02,0x00,0x01,0x04,0x57,0x5E))-- zone on
print("Send:", sres, serr)

local sres, serr = c:send(string.char(0x02,0x01,0x01,0x15,0xEA,0x03)) -- volume 38
print("Send:", sres, serr)

local sres, serr = c:send(string.char(0x02,0x00,0x01,0x04,0x57,0x5E))-- zone on
print("Send:", sres, serr)

local sres, serr = c:send(string.char(0x02,0x01,0x01,0x15,0xEA,0x03)) -- volume 38
print("Send:", sres, serr)

c:close()

THis is the plugin

local keyCode = luup.attr_get('altid', lul_device)
KeyString = "0x" .. string.format( "%02x", keyCode )

-- calculates check sum with variable
local chksums = 0x02 + 0x00 + KeyString + 0x04 + 0x57
local chksums66 = 0x02 + 0x01 + KeyString + 0x15 + 0x1E

-- Send command
cmdString = { 0x02, 0x01, 0x01, 0x15, 0xC9, 0xE2 }
sendCommand(makeCommand(cmdString))
cmdString = { 0x02, 0x00, KeyString, 0x04, 0x57, chksums }
sendCommand(makeCommand(cmdString))

cmdString = { 0x02, 0x01, 0x01, 0x15, 0xC9, 0xE2 }
sendCommand(makeCommand(cmdString))

cmdString = { 0x02, 0x00, KeyString, 0x04, 0x57, chksums }
sendCommand(makeCommand(cmdString))

cmdString = { 0x02, 0x01, 0x01, 0x15, 0xC9, 0xE2 }
sendCommand(makeCommand(cmdString))

The plugin will turn on the zone but will not set the volume.  To set the volume the unit is required to send a zone on command first and then a set volume.  I need to do this twice to set the volume.

The actually plugin was based on the RNET plugin and included items from a few TV and DVR plugins.  ANything to make it work.

Any help would be greatly appreciated.
Thanks

Online shallowearth

  • Hero Member
  • *****
  • Posts: 891
  • Karma: +41/-9
Re: WHY Would one Command Work and the other not? HEX COMMAND
« Reply #4 on: April 12, 2017, 01:23:09 pm »
The problem is how you are calculating the checksum.  The receiver is expecting the check sum to only be the last two digits, hence must be less than 255 (xFF).

In the example that was working to set volume to 38 you are sending the string:
string.char(0x02,0x01,0x01,0x15,0xEA,0x03)

But in the plug in code (that is broken) you are trying to send the this because are not dropping the high bit when the check sum is greater that 255
string.char(0x02,0x01,0x01,0x15,0xEA,0x103)

These strings are not equivalent! (0x03 does not equal 0x103)  And is what is causing your error.

When calculating the checksum you need to subtract 0x100 if the check sum is > 0xFF

So this code needs to expand from:
local chksums1 = 0x02 + 0x01 + KeyString + 0x15 + newttt

To
local chksums1 = 0x02 + 0x01 + KeyString + 0x15 + newttt
if chksums1 > 0xFF
then
chksums1 = chksums1 - 0x100
end

Then you will be sending the correct string.
as 0x103 will be converted to 0x03 correctly (as will all other volume numbers up to 60)

Offline Newzwaver

  • Full Member
  • ***
  • Posts: 199
  • Karma: +4/-3
Re: WHY Would one Command Work and the other not? HEX COMMAND
« Reply #5 on: April 12, 2017, 05:29:15 pm »
WOW Shallowearth, I can not thank you enough that worked perfectly.  After weeks of driving myself nuts it took you all of 10 minutes.

THANK YOU VERY MUCH IT IS TRULY APPRECIATED

Online shallowearth

  • Hero Member
  • *****
  • Posts: 891
  • Karma: +41/-9
Re: WHY Would one Command Work and the other not? HEX COMMAND
« Reply #6 on: April 12, 2017, 06:30:42 pm »
Glad this was helpful.
Note, this solution is not generic, if you were trying convert any hex value to just the 2 low order bits
For example convert 0xF03 to 0x03

Than you should actually just use the MOD operator (returns the remainder of division)
chksums1 = chksums1 % 0x100
or
chksums1 = math.mod(chksums1,0x100)

Then no if statement is required.

Offline Newzwaver

  • Full Member
  • ***
  • Posts: 199
  • Karma: +4/-3
Re: WHY Would one Command Work and the other not? HEX COMMAND
« Reply #7 on: April 12, 2017, 08:04:56 pm »
Thank you appreciate the help, this is only my second plugin and the first one was very basic.  This one stumped my because of the volume issues.  First the volume would not at all, then i found a step up by one, and finally a slider that worked for half the slider bar, thanks to you i have it working and moving on to reading the feedback.  The have the data figured out all i need it to work it into the plugin. 

My next question will be very detailed.

Thanks again.