passing variables into array functions

Recommended Posts

I am trouble with functions such as Min(), Max(), and Sum(). I have a variable that stores an array of channels. I then have three other channels that store the min, max and sum of these channels (the most recent element of each). Here is my code:

CVArray = {Battery1Voltage[0], Battery2Voltage[0], Battery3Voltage[0], Battery4Voltage[0]}




When I look at these channels, a 0 gets added. Can you not pass a variable into these functions? Thanks...

Link to comment
Share on other sites

You can't form an array that way. {} notation only works with constants, so {1,2,3} or {0xf3, 0x14, 0x34}. You can't use variables inside. If you have less than 20 items you want to put in the array, you can use concat:

CVArray = concat(Battery1Voltage[0], Battery2Voltage[0], Battery3Voltage[0], Battery4Voltage[0])

If you have more, you have to set the elements individually:

for (private i = 0, i < 50, i++)
CVArray[i] = evaluate("Battery" + i + "Voltage[0]")


Link to comment
Share on other sites

Alright, thanks for your help!

Now I have another quick question:

I am communicating with a device serially which takes a 26-byte message. The particular message I am sending includes set point values in the form a high byte and a low byte. For example, to change the max current to 30A, you would send a 0x1E. Byte 4 represents the low byte of the max current and Byte 5 is the high byte. So I would need to set Byte 4 to be 0x1E, and the high to 0x00. Also the values 0-30A are represented as the values 0-30000. I have a local variable that is altered using a variable value box. I am trying to compose this message by altering values within an array. Here is my code:

Message.MessageArray[3] = (1000*Message.MaxCurrent) & 0xFF

Message.MessageArray[4] = ((1000*Message.MaxCurrent) & 0xFF00) >> 4

Will this work? Is there a more efficient way to do this? Thanks again for your time...

Link to comment
Share on other sites

Thanks again for your continued help! All the sequences that I have know that send messages are working correctly! Now I am having problems parsing data that I have received. I receive a 26 byte message with byte pairs (words) that represent specific values. The message looks almost exactly like what you have read about in my previous post. The word is made up of a low byte, and a high byte, with the low appearing first. This is code I am having problems with:

Message.MessageArray[2] = 0x91

Message.Data = asca(device.TEKPOWER.Read(26))
Message.Data = asca(Fill(0x00, 26))

// Parse Output State data
Message.BitArray = To.Bit(Message.Data[17])

//Parse Load Resistance data
LoadResistance = To.uWord(Transpose(Message.Data[15,16],1))[/CODE]

My message sends successfully, and I do indeed receive a response:

Tx (11:45:55.440): \170\001\145\002\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\062

Rx (11:45:55.448): \170\001\145\000\000\117\062\000\000\000\000\048\117\208\007\080\195\001\001\000\000\000\000\080\195\147

It is giving me an invalid parameter error when assigning the values to my variables. Am I going about this correctly?

I also tried to just parse the Data string as a string using DoubleToStr(Mid(Message.Data,4,1)) and assigning that to a two dimensional array (Message.ParseArray[0][0], doing the high byte as well with the second column) and then assigning that array to my variable by passing it into To.Word(....

Which way is better, and what am I doing wrong? Thanks again!

Link to comment
Share on other sites

1) you don't really need to transpose when just doing one value in to.uWord

2) to.uWord is kind of silly if you ask me, since its so easy to calculate. You might consider just doing the math

3) I can't say what your are doing wrong without knowing the exact error and what line, not to mention the rest of the code. Add some ? debugging so you know what is happening.

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.