PDA

View Full Version : Checksum and Denon 3910



jolly1
October 19th, 2006, 08:19 AM
Hi
Have got G4 up and running. Controling my Denon AVC-A11XV. By way of a plugin and GML downloaded.
Then tried to find one for my Denon 3910 DVD. Nothing found so started to make one for the dvd.

Have managed to connect to the DVD but not control it.

Format of the commands is
Baud 9600
StopBits 1
StartBits 1
Parity Even


The command requires a checksum to be calculated and added to after ETX

I manual describes as
STX = 02
ETX = 03
for the power of code ! = 21
sum = stx + cc+ PCO+PC1..PCn+etx
=02+21+03
=26

I have no idea how to accomplish this in Lua, not even sure if it's correct. It's my very first go at this.

I have attached the Lau file (most which I have aquired by reading and utilising the denon AVR3805 file I downloaded. So please do not assume I understand Lau programming. It probably a miracle I manged to get it to communicate.

When I send a command to 3910 then response is 0. If I send again response ie 15 = NAK = over run or framing or parity.

Hopefully someone can assist.

regards jolly1

it's daunting when you realise the enormity of what you don't know:-)

jolly1
October 19th, 2006, 08:32 AM
Hi again the checksum should be sent as BCCH high level bit and BCCL low level bit. I'm kinda of gessing her but I take it to mean for 26
serial.tohexbyte(2)..serial.tohexbyte(6) or similar, but to be honest not that clear on this.

jolly1

Rob H
October 19th, 2006, 08:53 AM
local ETX = serial.hextobyte('03')
local STX = serial.hextobyte('02')
function Checksum(data)
local ck = ETX -- include the ETX
for i = 1, string.len(data) do
ck = ck + string.byte(data, i)
end
ck = math.band(ck, 255)
return serial.bytetohex(ck)
end


And your SendCommand function should look something like this



SendCommand = function(self, cmd)
serial.Classes.Queued.SendCommand(self, cmd .. ETX.. Checksum(cmd))
end,

Note that this assumes that SendStartByte is STX

jolly1
October 19th, 2006, 11:50 AM
Rob tried the suggestion, thanks for the reply. I now get an error and the file does not open ?

also here is my understanding of calculating the checksum
stx = start of text
etx = end of text
cc = command code
PC1 to Pcn is command code parameters

Therfore checksum
checksum = stx + cc + PC1 + PC2 + ..Pcn + etc + etx

check sum consists of 2 charcters BCCH and BCCL which are entered as individual ASCII codes. So resulting command to send would look something like this

stx cc pc1 etc etx cksum ascii code cksum ascii code.

Any help given to getting this running is greatly appreciated

jolly1

Rob H
October 19th, 2006, 12:50 PM
Yes, but what's the error you are seeing!?

jolly1
October 19th, 2006, 02:10 PM
Rob On starting girder serial error runscript when girder starts.

jolly1

Rob H
October 19th, 2006, 03:39 PM
You need to open the Lua console (View menu) and give me more details.

jolly1
October 19th, 2006, 04:14 PM
Hi Rob just realised that I could highlight and copy the information.

Serial Error (RunScript, C:\Program Files\Promixis\Girder\/plugins/serial/dvd3910.lua, 1): C:\Program Files\Promixis\Girder\/plugins/serial/dvd3910.lua:38: unexpected symbol near `local'

Rob H
October 19th, 2006, 04:19 PM
Ah, I'm guessing that you put the first block of code inside the device definition. Move it before the device definition and you should be fine.

jolly1
October 19th, 2006, 05:44 PM
Thanks for the patience but still no joy tried various location with no change
Hope you folks don't mind but here is a copy of the file pated in I have marked the various locations I tried

-- First part of calc checksum SEE OTHER LOCATIONS TRIED
--
-- local ETX = serial.hextobyte('03')
-- local STX = serial.hextobyte('02')
-- function Checksum(data)
-- local ck = ETX -- include the ETX
-- for i = 1, string.len(data) do
-- ck = ck + string.byte(data, i)
-- end,

-- ck = math.band(ck, 255)
-- return serial.bytetohex(ck)
-- end,


-- end of First part new calc check sum

-- <====== Tried it here no joy

local device = serial.Classes.Queued:New({

-- <====== Tried it here no joy

Name = "DVD3910",
Description = "Denon DVD",

BaudRate = 9600,
Parity = 2,
Sartbits = 0,
StopBits = 0,
DataBits = 8,
FlowControl = 'N',
IntraCharacterDelay = 0,
SendStartByte = serial.hextobyte('02'),
SendTerminator = serial.hextobyte('03'),
CallbackType = serial.CB_TERMINATED,
ReceiveTerminator = serial.hextobyte('03'),
IncompleteResponseTimeout = 200,
NoResponseTimeout = 200,
LogLevel = 0,
GlobalName = 'DVD',

-- <====== Tried it here no joy

Settings = {},
Responses = {},

-- <====== Tried it here no joy

Initialize = function (self)
gir.LogMessage(self.Name, 'Communications OK',3)
self.Status = "Communication OK"
return serial.Classes.Queued.Initialize (self)
end,


-- <====== Tried it here no joy


-- Second part of of calc checksum


-- SendCommand = function(self, cmd)
-- serial.Classes.Queued.SendCommand(self,cmd,.. ETX.. Checksum(cmd),callback,topofque,force)
-- end,

-- End of second part of calc checksum




-- comment out orginal code
SendCommand = function (self,command,callback,topofque,force)

return serial.Classes.Queued.SendCommand (self,command,callback,topofque,force)

end,



ReceiveResponse = function ( self, data, code )

if math.band (code,serial.RXCHAR) and data then
gir.TriggerEvent ('DVD:'..data,18)

local cmd = string.sub(data, 1, 2)
if cmd then
local func = self.Responses[cmd]
if func then
func(self.Settings, data)
end
end

end

if math.band (code,serial.ERR) then
gir.LogMessage(self.Name, 'Communcication Error',3)
end

if math.band (code, serial.NORESPONSETIMEOUT) > 0 then
code = serial.zerobits (code,serial.NORESPONSETIMEOUT)
end -- tells G4 not to report communication errors\timeouts in the Lua Console

serial.Classes.Queued.ReceiveResponse (self,data,code)

self.Responses['PW'] = function(Settings, data)
_, _, self.PowerMain = string.find (data,"PW(.-)$")
print ("Power:",(self.PowerMain))
NetRemote.SendLabel ('power', (self.PowerMain))
end



end,

}
)

serial.AddDevice (device)

Rob H
October 19th, 2006, 06:03 PM
What specific error is reported in the Lua console when the checksum code is at the start of the file, and what exactly is the code referred to by the error message? Paste the code from the line before and after the error reported.

jolly1
October 19th, 2006, 06:29 PM
Rob this all there is

here is line 1

Serial Error (RunScript, C:\Program Files\Promixis\Girder\/plugins/serial/dvd3910.lua, 1): C:\Program Files\Promixis\Girder\/plugins/serial/dvd3910.lua:9: unexpected symbol near `,'

Here is line 2

Welcome to Promixis Girder 4.0!

Line 3
Registering NR client from 192.168.1.2:3354

Is there away to increase the logging content ?

Regards John

jolly1
October 19th, 2006, 06:33 PM
Rob

I'm assumming the 9 at the end of the message is line 9
which is
local ETX = serial.hextobyte('03') <<< orginally it was this line
local STX = serial.hextobyte('02')
function Checksum(data)
local ck = ETX -- include the ETX
for i = 1, string.len(data) do
ck = ck + string.byte(data, i)
end, <<<<This is line 9

Rob H
October 19th, 2006, 06:35 PM
No, I don't mean the Logger output, I mean the Lua console output.

Rob H
October 19th, 2006, 06:38 PM
Ah, delete the comma after the 'end' - force of habit

jolly1
October 19th, 2006, 06:55 PM
Rob deleted comma still won't load

still get the same message in the Lua console
Serial Error (RunScript, C:\Program Files\Promixis\Girder\/plugins/serial/dvd3910.lua, 1): C:\Program Files\Promixis\Girder\/plugins/serial/dvd3910.lua:9: unexpected symbol near `,'

Rob H
October 19th, 2006, 06:58 PM
Upload the script you have here as a file.

jolly1
October 19th, 2006, 07:11 PM
Rob
Here is the file. The log enteries provide are from the window
Interactive Lua console

Rob H
October 19th, 2006, 07:30 PM
Where did that comma come from? It wasn't in the code I posted at all!

Try this one.

Rob H
October 19th, 2006, 07:36 PM
You'll also need to increase the NoResponseTimeout time to 5000

jolly1
October 19th, 2006, 08:02 PM
Hi Rob it loaded. But did not send any command. Here is the info from the Interactive Lau window I also increased noreponsetime out to 5000

TreeScript (gir_event): ...am Files\Promixis\Girder\/plugins/serial/dvd3910.lua:6: attempt to perform arithmetic on local `ck' (a string value)

stack traceback:


...am Files\Promixis\Girder\/plugins/serial/dvd3910.lua:6: in function <...am Files\Promixis\Girder\/plugins/serial/dvd3910.lua:3>

...am Files\Promixis\Girder\/plugins/serial/dvd3910.lua:45: in function `SendCommand'

...es\Promixis\Girder\/plugins/treescript/Serial UI.lua:62: in function <...es\Promixis\Girder\/plugins/treescript/Serial UI.lua:44>

The comma must have been me as moved the script around the file. Sorry about that.

John

Rob H
October 20th, 2006, 03:25 AM
Ah, okay, that's my fault - at line 4 change from


local ck = ETX

to


local ck = string.byte(ETX)

jolly1
October 20th, 2006, 05:01 AM
Hi Rob communicated Ok but still no action. My understanding of the checksum must be flawed :( Here is a dump from the Lua interactive consul)
for the power off command which is !


Serial: DVD3910 : Command queued: 21 03 33 33 33 36 !.3336 at postion 1
Serial: DVD3910 : Simple Send: 02 21 03 33 33 33 36 03 .!.3336.
Serial: DVD3910 : Simple Receive: Data Code: 0

Regards John :confused: :(

jolly1
October 20th, 2006, 09:25 AM
Hi the descition in the serial manual for Denon 3910 is

STX = Start of text I take this Startbit in Lua ?
CC = Commnand code For Powr of is !
Bit 0 = parameter code
Bit 1 = parameter code
Bit 2 = parameter code
Bit 3 = parameter code
Bit 4 = parameter code
Bit 5 = parameter code
Bit 6 = parameter code
Bit 7 = parameter code
ETX = End of text I I ake is StopBit in Lua

Question format in the documentation shown as
ST b0 b1 b2 b3 b4 b5 b6 b7 P SP
Am I correct in
SP = Startbit
b0 = command code
b1 = parameter code
b2 = parameter code
b3 = parameter code
b4 = parameter code
b5 = parameter code
b6 = parameter code
b7 = parameter code
P = parity
SP = Stopbit

Calculation for checksum is
= STX + Bit1+Bit2+Bit3+Bit4+Bit5+Bit6+Bit7+Bit8+ETX

Calculating the checksum for command code !
STX = 02
! = 21
Bit1 = 00
Bit2 = 00
Bit3 = 00
Bit4 = 00
Bit5 = 00
Bit6 = 00
Bit7 = 00
Bit8 = 00
ETX = 03

Total = 26

Serail doc state the are BCCH Bit code High and BCCL bit code low

Am I correct that BCCH is 2
and BCCL is 6

Each of which is 4 bits long and is converted to ASCII code

This one part I'm really unclear about.
BCCH is 2 ASCII code 32
BCCL is 6 ASCII code 36

IF so should the string that should be sent for command code ! poweroff be

03 21 00 00 00 00 00 00 00 03 32 36

Serial doc then goes on to state the recieving station shall regard the reciept of BBC as completion of the command when it recieves STX.

Hope some of this makes sense this time last week, didn't even know what a serial code was.
I may therfore be completely missing interepting the the serial documentation.
I attached the serial documentation in my my first post. Would be really gratefull if someone could check out the documentation and let me know have I understood it or not.

I really appreciate someone checking if I have understood the inforamtion in the documentation correctly.

Regards John

PS being new to this not sure if this should be a thread

Rob H
October 20th, 2006, 12:58 PM
Nope, you're misreading it.

The checksum doesn't include the STX, the command is a byte code (single character) with a number of parameters each of which are bytes (not bits).

For power off the command code is, as you say, 21h or '!', but it is followed by 4 NULs

So the byte sequence sent should be :-



02 21 00 00 00 00 03 32 34

jolly1
October 20th, 2006, 01:06 PM
Hi Rob after all of the above maybe better understanding of the serial command structre of the denon. Actual commands appear to be sent in ASCI

for power on !

should read STX!ETXSTX(checksum)ETX

check sum = stx + ! + stx in hex = 26 serial doc doesn't make it clear wether the additon is in Hex or decimal. But as mentioned above serial documention says convert this hex number to asccii. Would this then be 26 in decimal or for the stop command A

ASCII Hex
STX 02
A 40
ETX 03
checksum = 45 Now I'm real confuesd.

Rob I must apologise I think I provided incorrect info. Hopefully somone can help.

Regards John
STX

Rob H
October 20th, 2006, 01:34 PM
Try this

local NUL = serial.hextobyte('00')

DVD:SendCommand('!'..NUL..NUL..NUL..NUL)

jolly1
October 20th, 2006, 03:43 PM
Nope, you're misreading it.

The checksum doesn't include the STX, the command is a byte code (single character) with a number of parameters each of which are bytes (not bits).

For power off the command code is, as you say, 21h or '!', but it is followed by 4 NULs

So the byte sequence sent should be :-



02 21 00 00 00 00 03 32 34


Rob

So the first 8 bytes contain the STX and ETX (sorry about mixing bits and bytes)
I'm trying to get this set in my mind
If I cc is command code
and pc is parameter cc the 8 bytes would be assembled as follows

stx cc pc pc pc pc pc etx
then added to that is checksum
stx cc pc pc pc pc pc stx checksum

checksum is 21 + 3 = 24

BCCH is 2 which 32h
BCCL is 4 which 34h

The serial manual is a hard read but then again it's my lack of understanding

Regards John

jolly1
October 20th, 2006, 03:45 PM
Try this

local NUL = serial.hextobyte('00')

DVD:SendCommand('!'..NUL..NUL..NUL..NUL)

Sorry I'm not clear as to where to insert this in the script.

Regards John

Rob H
October 20th, 2006, 03:56 PM
Try putting it in a Lua script action in a GML

jolly1
October 20th, 2006, 04:06 PM
Rob some further information the file dvd3910.lua you sent me returns the following from the interactive lua viewer


Serial: DVD3910 : Command queued: 21 03 33 33 33 36 !.3336 at postion 1

Serial: DVD3910 : Simple Send: 02 21 03 33 33 33 36 03 .!.3336.
02 is the stx
21 is !
03 is etx
33 is 2
36 is 6

Or I'm still not grasping the format ?

Once again I really appreciate all your help

Regards John

jolly1
October 20th, 2006, 04:19 PM
Rob some further information the file dvd3910.lua you sent me returns the following from the interactive lua viewer


Serial: DVD3910 : Command queued: 21 03 33 33 33 36 !.3336 at postion 1

Serial: DVD3910 : Simple Send: 02 21 03 33 33 33 36 03 .!.3336.
02 is the stx
21 is !
03 is etx
33 is 2 ****** whoops 3
36 is 6

Or I'm still not grasping the format ?

Once again I really appreciate all your help

Regards John

Should have bee 3 not 2

jolly1
October 20th, 2006, 05:45 PM
Try this

local NUL = serial.hextobyte('00')

DVD:SendCommand('!'..NUL..NUL..NUL..NUL)

Rob no joy here is the info from Lua Interactive consul
Serial: DVD3910 : Command queued: 21 00 00 00 00 03 33 33 33 36 !.....3336 at postion 1
Serial: DVD3910 : Simple Send: 02 21 00 00 00 00 03 33 33 33 36 03 .!.....3336.

Rob H
October 20th, 2006, 06:12 PM
I'll have to come back to this tomorrow - it's getting far too late here for me to work out what's going on here.

jolly1
October 20th, 2006, 08:22 PM
I'll have to come back to this tomorrow - it's getting far too late here for me to work out what's going on here.

I understand the feeling ..... I'll be having nightmares about serial commands and checksums.

Re read the manual and states

BCCH and BCCL is 4 bit long, is that of any sigficance ?

Regards John

jolly1
October 20th, 2006, 08:47 PM
Rob I found a worked example on Denon site
http://usa.denon.com/2428.asp#Q0

Here it is
6) How do I send a proper command when checksums are required?

Commands with checksums are calculated and sent in the following manner. For this example we will use the Play command from one of our DVD players and provide the basic command string information needed to implement the command. The Play command is a hex 40 otherwise written as 40h or an ASCII "@" sign. Please note, in our protocol document commands in quotes (' ') are ASCII characters. To send this command and activate Play on our DVD player, the complete hex string would look like 02 40 00 00 00 00 03 34 33 as indicated in the protocol document. Now let's explain how we got there. The 02h starts the transmission, 40h is the play command itself, 00h 00h 00h 00h are four reserved bytes and must be sent (placeholders), the 03h is the end of transmission, and the 34h and the 33h bytes are the high (BCCH) and low (BCCL) checksums respectively. To calculate the checksums, you must add from the command byte through the end of transmission byte, omitting the start byte. For this Play command we add in hex 40h 00h 00h 00h 00h and 03h. This equals 43h. It is suggested that you use a hex calculator as not to make a mistake. The Windows calculator has a setting for hex which makes this easy to do. Do not add these numbers in standard decimal format with a regular calculator as this will not work. Once the string has been added, in this case a 43h, we can generate the checksums. To do this, the tens place number "4" is used for calculating the high checksum and the ones place "3" is used for calculating the low checksum. Treat both numbers as ASCII characters and convert them into hex. A "4" equals 34 and a "3" equals 33, therefore the high low checksums are 34h 33h which is added to the end of the command string. Final note, if your added string has a value with a hundreds place, ignore the hundreds place when calculating the checksums.

Rob H
October 21st, 2006, 06:05 AM
Try this one - I think this does the right thing now

jolly1
October 21st, 2006, 07:58 AM
Good Morning Rob,

Unfortunately returns the following errors

Dump from lua interactive consul

TreeScript (gir_event): ...am Files\Promixis\Girder\/plugins/serial/dvd3910.lua:6: attempt to perform arithmetic on local `ck' (a string value)
stack traceback:
...am Files\Promixis\Girder\/plugins/serial/dvd3910.lua:6: in function <...am Files\Promixis\Girder\/plugins/serial/dvd3910.lua:3>
...am Files\Promixis\Girder\/plugins/serial/dvd3910.lua:45: in function `SendCommand'
...es\Promixis\Girder\/plugins/treescript/Serial UI.lua:62: in function <...es\Promixis\Girder\/plugins/treescript/Serial UI.lua:44>


Regards John

Rob H
October 21st, 2006, 08:14 AM
damn, change the first line of the Checksum function to


local ck = 3

jolly1
October 21st, 2006, 09:44 AM
Hi Rob, still no joy. Here the info from the Lua interactive consul.

Serial: DVD3910 : Command queued: 21 03 33 33 33 36 !.3336 at postion 1
Serial: DVD3910 : Simple Send: 02 21 03 33 33 33 36 03 .!.3336.

When I read thro the info from the Denon site I think it is

02 21 00 00 00 00 03 32 34

Sum of 21 + 03 = 24

2 as ascii code 32
4 as ascii code 34

And it requires the format to include the 00. Could be accomplished by.

SendTerminator = serial.hextobyte('00')..('00')..('00')..('00')..(' 03'),

regards John

jolly1
October 21st, 2006, 09:54 AM
Rob no that suggestion don't work as it translate the

('00')..('00')..('00')..('00')..('03')

Serial: DVD3910 : Simple Send: 02 21 03 33 33 33 36 00 30 30 30 30 30 30 30 33 .!.3336.00000003

How do you enter Nul as 00h ?


Regards John.

Rob H
October 21st, 2006, 11:32 AM
As I said the other day




local NUL = serial.hextobyte('00')

DVD:SendCommand('!'..NUL..NUL..NUL..NUL)

What's the last line of the Checksum function in the version you are using? (before the 'end')

Rob H
October 21st, 2006, 11:36 AM
Ah, I see what happened - I had two copies of the file open, the one I'd been working on was in the temp folder! Sorry about that.

Try this one :-

jolly1
October 21st, 2006, 12:10 PM
Hi Rob,
Installe file recieved the installed recieved the following error

Serial Error (RunScript, C:\Program Files\Promixis\Girder\/plugins/serial/dvd3910.lua, 1): C:\Program Files\Promixis\Girder\/plugins/serial/dvd3910.lua:52: unexpected symbol near `..'

I removed the comma after cmd,
serial.Classes.Queued.SendCommand(self,cmd .. ETX.. Checksum(cmd))
-- removed , after cmd,

Put commands in
Local script as follows
local NUL = serial.hextobyte('00')
DVD:SendCommand('!'..NUL..NUL..NUL..NUL)

Ran the script and the DVD player went off
Jumped up done a Jig :) :) :)

I cannot thank you enough.

Best Regrads John

Rob H
October 21st, 2006, 12:40 PM
Yay! Great news, I knew we'd get there in the end.