FsWebIO Posted October 14, 2014 Report Share Posted October 14, 2014 Hello, I'm in the process of creating a cool interface with your DLL.So far so good but I come across bugs that confuse me and it seems I didn't really get the FSUIPC conversation. An example, let say I have an object holding Offsets as it : public class FsVar { public string Name { get; set; } public int Offset { get; set; } public int OffsetLength { get; set; } public string OffsetType { get; set; } public int BitIndex{ get; set; } public Offset<BitArray> OffsetBitArray { get; set; } public int ColdAndDarkDefault { get; set; } public string Value { get { if (OffsetType == "BIT") return (OffsetBitArray.Value[bitIndex]) ? "1" : "0"; return "" } set { if (OffsetType == "BIT") OffsetBitArray.Value[bitIndex] = (value == "1"); return; } } } My program : public class MainProgram { public List<FsVar> FsVarList { get; set; } MainProgram() { FSUIPCConnection.Open(); FsVarList = new List<FsVar>(); FsVarList = FsVarGetFromCsv(); //So now I have a list of FsVar, I instantiate the Offset<BitArray> of these foreach (var item in FsVarList) { switch (item.OffsetType) { case "BIT": item.OffsetBitArray = new Offset<BitArray>(item.Offset, item.OffsetLength); break; } } //Register the offsets ? FSUIPCConnection.Process(); //Now I set default values to the offset foreach (var item in FsVarList) { item.Value = item.ColdAndDarkDefault.ToString(); } //Each offset has now is default value, so I send this to FS ProcessAction(); } } My problem is at the end when I set the default values. To me FsVarList has all the objects correctly set and just calling ProcessAction(); will update corresponding Offset in FS. This is actually somewhat true, but by testing I figured out not all values where correctly sent to FS, what I had to do to insure proper setting of EACH value is that : foreach (var item in FsVarList) { item.Value = item.ColdAndDarkDefault.ToString(); ProcessAction();} There is approx 250 values in the list (Project Magenta pmSystems Offsets), can you explain me the difference between these two functions behind the scene in your DLL and in FSUIPC : // Kind of work but some values are not sent to FS as it should public void ColdAndDarkStateSet() { foreach (var item in FsVarList) { item.Value = item.ColdAndDarkDefault.ToString(); } ProcessAction(); } // Seems to work on every hit public void ColdAndDarkStateSet() { foreach (var item in FsVarList) { item.Value = item.ColdAndDarkDefault.ToString(); ProcessAction(); } } Of course the one thate work seems slower to run and has i read in your documentation ProcessAction(); should only be used only when necessary. The logic of the first function seemed right to me. Is it possible that in between the settings of each of the offset values in my FsVarList and the call to ProcessAction(); the other concurrently running application (Project Magenta pmSystems) has set different values to these offset ? In fact, each setting of every offset should be considered immediately followed by a ProcessAction(); ? Is that right ? Thank you Link to comment Share on other sites More sharing options...
Paul Henty Posted October 14, 2014 Report Share Posted October 14, 2014 Hi, I think the problem is being caused because you are creating multiple offset instances for the same address. For example if you want to access 4 bits from the same offset you end up with four entries in your FsVarList collection, each having the same offset address but different bit numbers. For each one of these you create a new Offset but they all have the same address. The problem is that when writing to FSUIPC, all bits of the offset are written at the same time. So say you want to write 1 in bits 0 and 1: Your first instruction to FSUIPC would be to write the value 1 to offset x. Then you use the second copy of the offset to write the value 2. This second copy does not know about the first one so it cannot send the correct value of 3. When you call process after each set, all the offsets that were not written get updated from FSUIPC. So after you write 1 that second copy of the offset gets updated to 1. Then you set bit 2 on this offset so the new value is 3 which is correct. To fix this you should only have one Offset instantiated for each address. Then you will be able to call process() after you set everything which is more efficient. I suggest keeping a dictionary of offsets keyed on the address. Before creating a new offset check the dictionary to see if there is already one there for that address and use that if there is. If not create one and store it in the dictionary for other FsVar objects to use. You want each FsVar object that uses the same address to be using the same instance of the FSUIPC Offset. If you need any more help with this let me know. Paul Link to comment Share on other sites More sharing options...
FsWebIO Posted October 14, 2014 Author Report Share Posted October 14, 2014 Brilliant ! You absolutely spotted on the problem just reading bad pasted code, you are effectly right, now that you say it I do create one instant of Offset for every Bit. That's explain why I have essentially this problem on BitArray offset ! Well frankly I still have a lot of difficulty understanding theses offset things. I may have other question, for the moment I'll try to see how to adapt my code to this fact. Thanks ! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now