6 Replies Latest reply on Jan 23, 2016 10:10 PM by Roger Palmen

    Howto build an unsigned int 32 data type

    martin.klauck

      Hello,

       

      I want to store an unsigned int 32 in PI/AF. What I tried and party worked was to setup AF with type Int64, the PI Point with Int32 and my service (c++) will take the unsigned int and pass it to the PI SDK.

      Unfortunate this does not work when the highest bit is set and PI throws an exception.

       

      Would it be possible to have 2 int 32 points that get combined with a formula in AF? But this would increase as well the point count for my solution.

      Does anyone has a better idea?

      Could a blob in PI be converted to int 64 in AF?

       

      Thanks

       

      Martin

        • Re: Howto build an unsigned int 32 data type
          Roger Palmen

          If all access to the PI Point and AF sttribute is through your custom code, you could work with the signed Int32 in your PI Point and AF, but just treat it as unsigned in your C++ application.

          A string might be another option.

           

          It makes sense that once you pass the boundary to what can be stored in the Int32, the PI server throws an error. Best practice is to keep AF attribute types and PI Point datatypes identical (except when trying to do implicit typecasting, like pulling epoch seconds out of a DateTime)

            • Re: Howto build an unsigned int 32 data type
              martin.klauck

              Hi Roger,

               

              thanks for the answer. I fully agree. I would like to have the same datatype in PI and AF. Unfortunately PI does not support signed int 64 (PI AF does) which would solve my problem. If my code converts the unsigned int 32 to signed int it will become negative if the highest bit is set in the unsigned int 32.

               

              I thought my solution with AF (64 bit) PI (32 bit) writing the bits from c++ was working a while ago. But after some sdk updates this looks not to work anymore.

               

              /Martin

                • Re: Howto build an unsigned int 32 data type
                  gregor

                  Hello Martin,

                   

                  Rogers suggestion to convert from UInt32 to Int32 before writing to PI and backwards could work because the amount of bits is the same. Your applications could play trick and treat but other client applications and especially the users would become confused by the erroneous values. This said, I consider this an island solution and recommend against it unless there are no better options.

                   

                  Converting the UInt32 to a string and writing to a string tag could as well be an option but have you ever trended a string tag? Besides that storing a UINT32 as a string uses way too much space and you would buy poor performance. The argument against the first option also kind of applies. So let's look for more options ..

                   

                  You asked if using a blob would be an option. Blob's are kind of cool but I wouldn't know any good use case for blob's except maybe "hiding" data. It's usually even a better idea to store the path of a file in a string instead of using a blob for the file.

                   

                  The option that I suggest, is using a Float64 point type. The value range is a lot larger than the one of the UInt32 and it's only twice expensive with regards to the space that is used. That may sound much other options discussed above would be way more expensive. You may want to set displaydigits to 0 (zero) to avoid someone expecting a precision that doesn't exist.

                  1 of 1 people found this helpful
                    • Re: Howto build an unsigned int 32 data type
                      gregor

                      I have created a C# test application using AF SDK  to verify setting the point type to Float64 is indeed a valid option and saw the tag becoming updated with negative values

                       

                      namespace PISquare_12109
                      {
                          class Program
                          {
                              private static Random rnd = new Random();
                              private static UInt32 uiRandom()
                              {
                                  return (UInt32)rnd.Next(Int32.MinValue, Int32.MaxValue);
                              }
                              static void Main(string[] args)
                              {
                                  PIServer srv = PIServers.GetPIServers().DefaultPIServer;
                                  srv.Connect();
                                  PIPoint pt = PIPoint.FindPIPoint(srv, "PISquare_12109");
                                  while (!Console.KeyAvailable)
                                  {
                                      AFValue val = new AFValue(uiRandom(), AFTime.Now);
                                      val.IsGood = true;
                                      pt.UpdateValue(val, OSIsoft.AF.Data.AFUpdateOption.Insert);
                                      Console.WriteLine("{0} - {1}", val.Value, val.Timestamp);
                                      Thread.Sleep(1000);
                                  }
                              }
                          }
                      }
                      

                       

                      The fix however was simply casting to Float64 (double) at line 17

                       

                      AFValue val = new AFValue((double)uiRandom(), AFTime.Now);
                      
                  • Re: Howto build an unsigned int 32 data type
                    martin.klauck

                    Hei Roger,

                     

                    I got the time to test with Float64 and it works perfectly.

                    What I do now is to convert my unsigned int32 to float in c++ and write it to PI Float64. No negative values then.

                    Thanks as well to Gregor for testing.

                     

                    /Martin

                    1 of 1 people found this helpful