Hi there, I've been running into an issue where I get a Duplicate Field Name error when I attempt to use StructureType. I have three charge controllers and I am trying to get the Voltage and Current readings from it by parsing an incoming string on Serial Ports. These two values should then be stored in relevantly named members of the StructureType t_ChargeController. I try to call them up then in the ChargeTable but receive a Duplicate Field Name error during compilation. Not sure if I am missing some settings somewhere.
I've attempted to Alias the individual members of the ChrgControllers array but then I receive a 'variable not declared yet so cannot be aliased' error.
It has been bugging me for some time so would be grateful for any suggestions. Please see the code below
Public ChrgC2_message As String * 100 Public ChrgC4_message As String * 100 Public ChrgC6_message As String * 100 Public NBytesReturned As Float StructureType t_ChargeController Voltage As Float Units Voltage = mV Current As Float Units Current = Amps ReadOnly Voltage, Current EndStructureType Public ChrgControllers(3) As t_ChargeController 'Define Data Tables DataTable (ChargeTable,1,-1) 'Set table size to # of records, or -1 to autoallocate. DataInterval (0,15,Sec,10) Minimum (1,Batt_volt,FP2,False,False) Sample (1,PTemp,FP2) Sample (3, ChrgControllers, t_ChargeController) EndTable '######################################################################## 'Main Program BeginProg SerialOpen(ComSDC7, 19200, 0, 0, 50) SerialOpen(ComSDC8, 19200, 0, 0, 50) SerialOpen(ComSDC10, 19200, 0, 0, 50) Scan (1,Sec,0,0) PanelTemp (PTemp,15000) Battery (Batt_volt) SerialInRecord (ComSDC7,ChrgC2_message,&h3645,0,&h5650,NBytesReturned,01) SplitStr(ChrgControllers(1), ChrgC2_message, " ", 2, 0) SerialInRecord (ComSDC8,ChrgC4_message,&h4133,0,&h5650,NBytesReturned,01) SplitStr(ChrgControllers(2), ChrgC4_message, " ", 2, 0) SerialInRecord (ComSDC10,ChrgC6_message,&h465A,0,&h5650,NBytesReturned,01) SplitStr(ChrgControllers(3), ChrgC6_message, " ", 2, 0) CallTable ChargeTable NextScan EndProg
Note the Duplicate Field Name error also happens if the ChargeControllers are declared separately and then called with Sample method in the table definition. See below:
Public ChrgController_1 as t_ChargeController Public ChrgController_2 as t_ChargeController ... DataTable() ... Sample (1, ChrgController_1, t_ChargeController) Sample (1, ChrgController_2, t_ChargeController) ... EndTable
It may be inconvenient, but you need to use the FieldNames instruction. The example program for StrutureType shows how FieldNames can be used.
If you do reps of one so it will compile, you will see that the fields in the table are "Voltage" and "Current". They inherit just the names of the variables within the structure. Two structures of the same type have the same variable names.
Hi JDavis, thank you for your suggestion. I've tried the following but it still results in the Duplicate Field name error saying 'Duplicate field name Voltage in DataTable ChargeTable' and 'Duplicate field name Current in DataTable ChargeTable'. I am not seeing the abovementioned example StructureType program with the FieldNames used, could you please point me to it? Note I am using CRBasic Editor 3.9
Public Charger_1 as t_ChargeController Public Charger_2 as t_ChargeController DataTable(ChargeTable, 1, -1) ... Sample (1, Charger_1, t_ChargeController) FieldNames ("CH1_Voltage, CH1_Current") Sample (1, Charger_2, t_ChargeController) FieldNames ("CH2_Voltage, CH2_Current") EndTable
When you look at the help for an instruction, you can expand the example program by clicking the "Example" link.
https://help.campbellsci.com/crbasic/cr1000x/#Instructions/structuretype.htm#kanchor149
Hmm, interestingly the usage of Fieldnames with Structures isn't showing in the in-built help of the CRBasic Editor. That's where I was looking. The examples there show the same programs but are missing the Fieldnames instructions. Tried the following. The first one works and the table produced then shows fields such as Charger_1.Voltage and Charger_1.Current. The second variation where the Charge Controllers are declared separately doesn't.Makes me wonder if the issue is with the Sample function. Note the version of the compiler that shows for me is ...\CR6Comp.exe VERSION:CR6.Std.11.00 DATE:02/11/2021, not sure if there is a newer one out there.
Working one:
Public Number_of_Charge_Controllers As Float = 2 Public ChrgControllers(Number_of_Charge_Controllers) As t_ChargeController DataTable (ChargeTable,1,-1) DataInterval (0,10,Sec,10) Sample(Number_of_Charge_Controllers, ChrgControllers, t_ChargeController) FieldNames("Charger_1,Charger_2") EndTable
Not working one:
Public Charger_1 As t_ChargeController Public Charger_2 As t_ChargeController DataTable (ChargeTable,1,-1) DataInterval (0,10,Sec,10) Sample(1, Charger_1, t_ChargeController) FieldNames("Charger_1") Sample(1, Charger_2, t_ChargeController) FieldNames("Charger_2") EndTable
Download CR6 OS 12 from the website, and run the exe on your computer. It will update the compiler and help in CRBasic Editor.
Hi JDavis. I've updated the OS to 12.02 as suggested and can confirm now that both code variants above are now working. The Help files were also updated and now contain the up-to-date information. Delighted with that. Thank you for your time and effort.