DEFINE_FUNCTION
PatG
Posts: 42
Hi, can someone link me to an example of this ( or post one ). I have never used one before. How to employ it also escapes me.
Thanks
0
Best Answer
-
[Deleted User] Harman Integrated Technologies Group (ITG) Posts: 0
Resource 1 - Within Netlinx Studio - MENU>HELP>NETLINX KEYWORDS HELP > DEFINE_FUNCTION
Resource 2 - Online P1 Video Series (CP131) - Lesson 12 - Functions - https://training.harmanpro.com/#/courses/course/e7f4d34e-74ed-4638-8a2b-78e354f76e911
Answers
Excellent thanks
Here is the example given in the course material, all of which makes sense to me, except when calling it I'm not sure what sBuffer is referring to. I know I need the return but I don't get what this sBuffer is which is wherein the return would reside I'm guessing
Sorry here is the image url https://imgur.com/a/BGbmegi
sBuffer for this checksum function should contain all those hex values but this snippet doesn’t show where or how that gets populated.
Pat - the screenshot is from the overview video and is more of a static example. Did the content that followed the overview, and gave real examples inside Netlinx Studio help clarify this concept for you? Be sure to watch the concept review video at the end of the section which will walk through the completed exercise and demonstrate one way to solve for the task.
I did a few times but the example they use in that case is quite a bit different from the one I posted. I have a method that works but it's no where near as elegant as I think this could be. For example I have a device the will return different length strings depending on the command so I have a function for each which is like
DEFINE_FUNCTION CHAR fnChecksum6 (CHAR cPROJ_BUFFER[]) //Checksum for projector strings
{
STACK_VAR CHAR Result
Result = cPROJ_BUFFER[1]+cPROJ_BUFFER[2]+cPROJ_BUFFER[3]+cPROJ_BUFFER[4]+cPROJ_BUFFER[5]
RETURN Result
}
And that's only for returned strings, I have different functions for different length send_strings, all to calculate the checksum. I feel like I'm defeating the purpose of using a function.
I cannot tell what kind of variable cPROJ_BUFFER[] is. I'm assuming it's a char.
Netlix is a bit funn when it comes to type casting. What you might think of as a char in other environments is not exactly what Netlinx does. Can you post what the string you're working with is?
Can you leverage a FOR loop with the LENGTH_STRING(cProj_Buffer[]) as the loop count limiter within a single function?
Like Eric mentioned, I was concerned about data types, but after trying several combinations of ATOI or HEXTOI - it didn't end up needing it to correctly perform the additive math I was going for in this demo. We still do not have the full details of what you are trying to accomplish, but based on my assumptions - here is what I wrote to test the function. NOTE - as a calculation, you need this function to be an INTEGER and not a CHAR for the return type.
Thanks everyone, I did have some success in that the function works when I send strings but not on the ones I receive. I think it's because a received string will already contain a checksum, which ends up added to the rest of the string and THEN my function is coming up with a checksum based on that, if that makes any sense.
Here is my function if you're interested:
DEFINE_FUNCTION integer fnChecksum (CHAR cSend_Proj[]) //Checksum for strings to projector
{
STACK_VAR integer i
STACK_VAR CHAR ckSum
for(i=1;i<=LENGTH_STRING(cSend_Proj);i++)
{
ckSum = ckSum + cSend_Proj[i]
}
RETURN ckSum
}
And to execute
Where inPROJCOMP is just a constant containing all the hex values.
If I am right about the send vs receive string I can live with it like this. Just getting it to work on the sent strings saved a lot of code. THanks again for all your help.
One thing you might do (I do this always as a matter of course) is to change
STACK_VAR CHAR ckSum
to
STACK_VAR CHAR ckSum[1]
It may seem counter-intuitive since a one-celled char array is implied by the first statement. but I find that Netlinx seems to like a little help when it comes to arrays. Like I said earlier, Netlinx is a bit funny when it comes to Types. Most other languages are not so wonky when it comes to variables.