Question sending HEX to device
undrtkr
Posts: 35
I'm going to be controlling a couple of Samsung displays. Their protocol is HEX with a checksum and I'm trying to calculate the checksum through code. Basically to get the checksum you add bytes 5-2. Here is what I have so far
DEFINE_VARIABLE
integer byte[6]
DEFINE_CALL 'Checksum'
{
byte[1] = byte[5] + byte[4] + byte[3] + byte[2]
byte[1] = byte[1] BAND $FF
SEND_STRING dvDISPAY, "byte[6],byte[5],byte[4],byte[3],byte[2],byte[1],"
}
DEFINE_START
byte[6] = $AA //Header Byte
byte[5] = $00 //Command Byte
byte[4] = $FF //Display ID
byte[3] = $01 //Data Length
byte[2] = $00 //Data Byte
byte[1] = $00 //Checksum
DEFINE_EVENT
BUTTON_EVENT[dvTP,1]//Power On
BUTTON_EVENT[dvTP,2]//Power Off
{
PUSH:
{
SELECT
{
ACTIVE(BUTTON.INPUT.CHANNEL = 1):
{
byte[5] = $11
byte[2] = $01
}
ACTIVE(BUTTON.INPUT.CHANNEL = 2):
{
byte[5] = $11
byte[2] = $00
}
}
CALL 'Checksum'
}
}
Now, this code calculates all bytes correctly as I watch the byte variable in debug. However looking in netlinx diagnostics it only sends the full string on power on which is "$AA$11$FF$01$01$12". Power off sends only this "$AA$11$FF$01". The only thing I can think of is byte 2 is $00 in the power off command but am not understanding why it's behaving this way. If someone could shed some light and offer some changes to make it work correctly I'd appreciate it.
.
DEFINE_VARIABLE
integer byte[6]
DEFINE_CALL 'Checksum'
{
byte[1] = byte[5] + byte[4] + byte[3] + byte[2]
byte[1] = byte[1] BAND $FF
SEND_STRING dvDISPAY, "byte[6],byte[5],byte[4],byte[3],byte[2],byte[1],"
}
DEFINE_START
byte[6] = $AA //Header Byte
byte[5] = $00 //Command Byte
byte[4] = $FF //Display ID
byte[3] = $01 //Data Length
byte[2] = $00 //Data Byte
byte[1] = $00 //Checksum
DEFINE_EVENT
BUTTON_EVENT[dvTP,1]//Power On
BUTTON_EVENT[dvTP,2]//Power Off
{
PUSH:
{
SELECT
{
ACTIVE(BUTTON.INPUT.CHANNEL = 1):
{
byte[5] = $11
byte[2] = $01
}
ACTIVE(BUTTON.INPUT.CHANNEL = 2):
{
byte[5] = $11
byte[2] = $00
}
}
CALL 'Checksum'
}
}
Now, this code calculates all bytes correctly as I watch the byte variable in debug. However looking in netlinx diagnostics it only sends the full string on power on which is "$AA$11$FF$01$01$12". Power off sends only this "$AA$11$FF$01". The only thing I can think of is byte 2 is $00 in the power off command but am not understanding why it's behaving this way. If someone could shed some light and offer some changes to make it work correctly I'd appreciate it.
.
0
Comments
Oh, and I htink you really want byte to be a char array, not integer. If the checksum adds up to over FF, it may not give you what you think you are getting.
And you can loose the BAND $FF statement - when you put an integer variable in as part of a string like that, the upper byte is automatically ignored - the OS understands that only the lower 8 bits can be used in string construction that way.
- Chip
Other <cough> vendors' diagnostic software deals with these much more nicely...
If you had a PC running the right terminal software (like the one provided by that other vendor, in fact) connected on the receive end of that cable, you would have a few options for viewing strings like this. You could, for instance, enable a mode that takes ANY byte outside of the printable ascii set and show its hex value instead. Extremely handy.
- Chip
LOCAL_VAR nByte
nByte = HEXTOI(Byte[5]) + HEXTOI(Byte[4]) + HEXTOI(Byte[3]) + HEXTOI(Byte[2])
SEND_STRING dvSERIAL1, "BYTE[6],BYTE[5],BYTE[4],BYTE[3],BYTE[2],nByte"
So even if nByte ends up being over FF the SEND_STRING won't show the upper byte? Is there a reason why I should do a char array or is it just a preference thing?
If you monitor the strings going out the RS-232 port in the Notifications window you?ll see the data properly. The Notify window doesn?t discriminate against NULLs like the Diag window does. I hope this bug gets fixed in the next release.
Well I'll be..your right it works in notifications but not in diagnostics. Didn't even think to try that. Thanks Joe!
You don't need to change a thing except the type declaration, just change it from integer to char. I ran a copy of your code that way on my test NetLinx, it works fine. The double quotes are only needed if you are assigning a string, not a single character to an array.