Hello, can someone please assist with my application... An automation system collects 512 measurement points per sensor. Each measurement point has dedicated OPC tag which I can read using OpenOPC for Python. However, with multiple sensors, the OPC server can lag. So, the automation system programmer created an "array of floats" as quoted...

..."I pack the profile data into an array of floats (32 bit) which I store in the tag. You would see this as an array of 4*512 = 2048 Bytes, since the OPC server has no information on what type of data stores in an array-tag. Only the size is known, therefore you will get it as bytes. If you can easily address it as an array of floats (in C this is just a typecast of a pointer), you can give it a try. The data has byte alignment and it is stored in little endian format (Intel)."

My problem is that I do not have a clue how to properly import the array of floats into Python and use an array pointer to read/unpack the array. I tried the following code (results pasted in) which I think is just misapplication of a tupple...

ProfileArray = opc.read('QMS.Readings.TABRawProfileValues.0.DataArray')

print ProfileArray
(<read-write buffer ptr 0x03436140, size 2048 at 0x03436120>, 'Good', '08/24/10 10:12:35')

print ProfileArray[0]
\ï

print ProfileArray[1]
Good 

print ProfileArray[2]
08/24/10 10:12:35

I have researched Python online and consulted one other forum which landed me here. I do not know where to focus next...struct? ctype? numpy?

Can someone please outline Python code that will handle the array of floats?

Thank you

You can use array or BitBuffer An eample using array

import array
a_long = array.array('f')     ## float (was 'L' for long hence var name)
fp = open(fname, 'rb')

a_long.fromfile(fp, 1)
print a_long

a_long.fromfile(fp, 1)
print a_long

fp.close()

Edited 6 Years Ago by woooee: n/a

Thanks for the reply, woooee. Could you add more code comments? Does variable 'fb' stand for float pointer? What is 'rb'? Python 8.6 array documentation does not define 'rb'. Is 'fname' a text file that I need to have in the directory? What is the difference between lines 5 and 8 in your example code? Does line 2 of your code create an array named a_long, line 3 opens up a text file named fname.txt, line 5 appends 1 item from fname.txt into array a_long, line 8 appends same/next item??

Thanks for the reply, woooee. Could you add more code comments? Does variable 'fb' stand for float pointer? What is 'rb'? Python 8.6 array documentation does not define 'rb'. Is 'fname' a text file that I need to have in the directory? What is the difference between lines 5 and 8 in your example code? Does line 2 of your code create an array named a_long, line 3 opens up a text file named fname.txt, line 5 appends 1 item from fname.txt into array a_long, line 8 appends same/next item??

Woooee's code had not fb, it had only one file "fname.txt" opened in binary mode "rb" as fp. He read two times value from the file to object, which he set up to represent float value 'f', by the objects fromfile method.

If array's elements quantity known in advance you can use this code:

val1, val2, val3 = opc.read('QMS.Readings.TABRawProfileValues.0.DataArray')

Edited 6 Years Ago by _neo_: n/a

Thanks for the direction, woooee and tonyjv. From your examples I came up with the code below but ask for additional help to correct what I think is my array unpacking code is off by one...

ProfileArray = opc.read('QMS.Readings.TABRawProfileValues.0.DataArray')
    # print result was: (<read-write buffer ptr 0x03610D00, size 2048 at 0x03610CE0>, 'Good', '09/01/10 22:07:19')
                              
  ProfileArrayFloatsPacked = open('ProfileArrayFloatsPacked.txt', 'w')

  print >> ProfileArrayFloatsPacked, ProfileArray[0] 
   # writes 512 packed floats to ProfileArrayFloatsPacked.txt

  ProfileArrayFloatsPacked.close()

  array_of_floats = array.array('f') # creates an empty array of type float

  fp = open('ProfileArrayFloatsPacked.txt', 'rb') 
   # opens ProfileArrayFloatsPacked.txt in binary mode ('rb') as fp

  array_of_floats.fromfile(fp,512) 
   # reads/unpacks 512 floats from fp into the array array_of_floats

  logfile = open('logfile.txt', 'a')
  print >> logfile, array_of_floats,","
  fp.close()
  logfile.close()

Below is the contents of logfile.txt... notice float0 value is text array('f' and there should not be any ([)] in values.

float0 array('f'
float1 [0.031583152711391449
float2 3.13E-02
float3 3.08E-02
float4 0.031223474
float5 3.09E-02
float6 3.15E-02
float7 3.17E-02
float8 3.18E-02
float9 3.14E-02
float10 3.19E-02
float11 3.13E-02
float12 3.12E-02
float13 3.17E-02
float14 3.19E-02
float15 3.19E-02
float16 3.24E-02
float17 3.23E-02
float18 3.18E-02
float19 3.21E-02
float20 3.15E-02
float21 3.20E-02
float22 3.23E-02
float23 3.26E-02
float24 3.21E-02
float25 3.24E-02
float26 3.21E-02
float27 3.24E-02
float28 3.26E-02
float29 3.29E-02
float30 3.20E-02
float31 3.25E-02
float32 3.25E-02
float33 3.27E-02
float34 3.28E-02
float35 3.28E-02
float36 3.24E-02
float37 3.26E-02
float38 3.25E-02
float39 3.27E-02
float40 3.23E-02
float41 3.25E-02
float42 3.25E-02
float43 3.26E-02
float44 3.29E-02
float45 3.25E-02
float46 3.35E-02
float47 3.29E-02
float48 3.29E-02
float49 3.30E-02
float50 3.34E-02
float51 3.37E-02
float52 3.27E-02
float53 3.33E-02
float54 3.31E-02
float55 3.31E-02
float56 3.34E-02
float57 3.35E-02
float58 3.31E-02
float59 3.36E-02
float60 3.32E-02
float61 3.33E-02
float62 3.34E-02
float63 6.83E-33
float64 7.49E-31
float65 6.80E-33
float66 -1.36E+19
float67 3.37E-02
float68 3.35E-02
float69 3.40E-02
float70 3.32E-02
float71 3.35E-02
float72 3.36E-02
float73 3.34E-02
float74 6.80E-33
float75 2.76E-33
float76 9.71E-34
float77 5.40E-33
float78 9.76E-32
float79 4.81E-31
float80 2.75E-32
float81 4.77E-31
float82 5.61E-33
float83 2.31E-31
float84 4.57E-32
float85 4.46E-32
float86 1.68E-31
float87 3.11E-32
float88 1.36E-30
float89 -8.95E-15
float90 8.21E+01
float91 2.80E+03
float92 2.69E-12
float93 -3.55E-10
float94 4.19E+21
float95 1.66E+00
float96 -4.90E-12
float97 1.18E-01
float98 6.80E-33
float99 1.98E+08
float100 9.14E+26
float101 -4.10E-17
float102 -4.96E+07
float103 5.71E+25
float104 2.44E-24
float105 -1.68E-13
float106 3.56E-35
float107 1.39E-37
float108 1.36E+19
float109 1.27E+10
float110 -6.56E-16
float111 -2.28E-33
float112 1.46E-31
float113 -3.47E-38
float114 1.21E+04
float115 8.89E-36
float116 -1.30E+13
float117 1.46E+28
float118 5.20E+13
float119 1.49E-28
float120 5.08E+10
float121 -9.10E-33
float122 1.43E+25
float123 -9.32E-30
float124 1.00E-20
float125 -5.69E-34
float126 1.94E+05
float127 -1.89E+02
float128 1.53E+34
float129 6.88E-10
float130 -1.30E+13
float131 -3.17E+09
float132 1.68E-13
float133 -7.21E-04
float134 -2.45E+35
float135 -3.83E+33
float136 3.73E-29
float137 -6.56E-16
float138 2.62E-15
float139 -6.28E+37
float140 3.17E+09
float141 1.89E+02
float142 2.28E-33
float143 -2.50E-21
float144 -1.94E+05
float145 5.97E-28
float146 1.46E+28
float147 5.69E-34
float148 5.08E+10
float149 -1.05E-14
float150 -5.83E-31
float151 9.55E-27
float152 4.40E-08
float153 2.56E-18
float154 -2.88E-03
float155 7.93E+08
float156 1.33E+16
float157 3.49E+21
float158 -3.91E-23
float159 9.14E+26
float160 4.84E+04
float161 -7.39E-01
float162 2.44E-24
float163 -3.25E+12
float164 2.51E+38
float165 -2.75E-09
float166 2.44E-24
float167 3.41E+18
float168 -6.88E-10
float169 9.55E-27
float170 5.56E-37
float171 5.71E+25
float172 1.76E-07
float173 -3.91E-23
float174 8.32E+14
float175 -2.75E-09
float176 -9.82E+35
float177 -1.40E+22
float178 3.25E+12
float179 -1.33E+16
float180 5.08E+10
float181 1.94E+05
float182 -4.40E-08
float183 4.51E-05
float184 -2.88E-03
float185 -5.85E+28
float186 1.10E-08
float187 -1.46E+28
float188 -7.39E-01
float189 -7.39E-01
float190 -7.56E+02
float191 3.25E+12
float192 -3.91E-23
float193 -2.44E-24
float194 -2.08E+14
float195 1.68E-13
float196 2.28E-33
float197 -3.10E+06
float198 -2.34E+29
float199 -2.45E+35
float200 -9.10E-33
float201 5.08E+10
float202 1.33E+16
float203 -3.66E+27
float204 -9.55E-27
float205 -1.76E-07
float206 5.20E+13
float207 -1.18E+01
float208 -2.34E+29
float209 5.85E+28
float210 1.13E-05
float211 8.32E+14
float212 1.76E-07
float213 -3.66E+27
float214 9.55E-27
float215 2.33E-30
float216 1.49E-28
float217 -1.57E+37
float218 -5.20E+13
float219 -1.13E-05
float220 -1.39E-37
float221 -2.22E-36
float222 -1.42E-34
float223 5.99E+31
float224 -1.40E+22
float225 -4.40E-08
float226 1.46E-31
float227 -1.56E-22
float228 2.51E+38
float229 -4.20E-14
float230 -7.93E+08
float231 -3.33E+15
float232 -4.00E-20
float233 3.75E+30
float234 1.15E-02
float235 2.28E-33
float236 1.98E+08
float237 -4.10E-17
float238 4.40E-08
float239 -2.82E-06
float240 2.50E-21
float241 -3.73E-29
float242 1.53E-25
float243 -7.93E+08
float244 -2.51E+38
float245 3.91E-23
float246 9.14E+26
float247 -1.49E-28
float248 4.96E+07
float249 -1.36E+19
float250 2.39E-27
float251 1.64E-16
float252 6.72E-13
float253 -6.11E-25
float254 8.32E+14
float255 -1.98E+08
float256 -1.05E-14
float257 -1.94E+05
float258 -3.47E-38
float259 2.33E-30
float260 -3.91E-23
float261 2.45E+35
float262 -3.57E+24
float263 1.98E+08
float264 -5.69E-34
float265 -2.50E-21
float266 -8.32E+14
float267 -8.72E+20
float268 -2.50E-21
float269 1.00E-20
float270 -1.80E-04
float271 -2.39E-27
float272 1.03E-17
float273 3.33E+15
float274 -1.05E-14
float275 -3.93E+36
float276 9.32E-30
float277 5.99E+31
float278 -4.51E-05
float279 1.50E+31
float280 1.18E+01
float281 1.57E+37
float282 -3.49E+21
float283 1.03E-17
float284 -1.68E-13
float285 8.93E+23
float286 1.72E-10
float287 0.046165321
float288 2.03E+11
float289 1.53E+34
float290 1.05E-14
float291 7.56E+02
float292 4.20E-14
float293 8.12E+11
float294 3.56E-35
float295 -1.42E-34
float296 -3.64E-32
float297 2.34E+29
float298 -1.68E-13
float299 -1.07E-11
float300 -9.36E+29
float301 5.71E+25
float302 -9.10E-33
float303 -3.56E-35
float304 3.56E-35
float305 5.83E-31
float306 3.63E-02
float307 3.61E-02
float308 3.68E-02
float309 3.56E-02
float310 3.64E-02
float311 3.61E-02
float312 3.68E-02
float313 3.61E-02
float314 3.59E-02
float315 3.58E-02
float316 3.63E-02
float317 3.60E-02
float318 3.58E-02
float319 3.65E-02
float320 3.60E-02
float321 3.59E-02
float322 4.69E-26
float323 3.36E-26
float324 1.39E-27
float325 7.40E-27
float326 1.64E-28
float327 1.56E-27
float328 5.86E-28
float329 5.83E-28
float330 3.36E-28
float331 2.94E-28
float332 1.10E-26
float333 1.03E-25
float334 9.54E-27
float335 4.12E-26
float336 5.74E-27
float337 4.31E-28
float338 1.39E-27
float339 1.58E-27
float340 4.37E-26
float341 1.02E-27
float342 1.24E-26
float343 7.90E-28
float344 1.32E-27
float345 1.68E-28
float346 1.64E-27
float347 5.93E-27
float348 6.79E-29
float349 9.84E-27
float350 6.81E-33
float351 1.58E+34
float352 -4.37E-18
float353 1.70E+32
float354 -4.74E-22
float355 4.70E-04
float356 -4.85E+32
float357 5.35E-12
float358 -1.36E+06
float359 -8.17E-19
float360 5.55E-11
float361 -1.62E-15
float362 3.01E+33
float363 1.34E+26
float364 -1.45E+12
float365 3.32E-18
float366 4.04E+32
float367 1.16E-09
float368 6.41E-25
float369 2.38E+37
float370 -4.44E-07
float371 1.16E-05
float372 7.94E+08
float373 -1.26E-20
float374 -1.63E-10
float375 4.84E+14
float376 1.97E+21
float377 -1.26E-29
float378 4.87E-09
float379 7.64E+24
float380 4.43E-28
float381 -4.88E+29
float382 5.83E+24
float383 1.50E-16
float384 1.60E+09
float385 3.49E-06
float386 -2.61E+06
float387 -3.77E-37
float388 -1.16E-11
float389 3.09E+03
float390 4.67E+32
float391 -7.50E+16
float392 4.97E+36
float393 -9.91E-19
float394 -1.33E+31
float395 2.65E+12
float396 -7.43E+08
float397 2.50E+30
float398 3.12E-34
float399 -1.34E-19
float400 1.18E+36
float401 -1.05E+32
float402 -5.76E-10
float403 1.76E+07
float404 -1.47E+21
float405 1.76E-07
float406 -7.56E+02
float407 1.07E-11
float408 -6.41E-19
float409 3.10E+06
float410 1.05E-14
float411 1.68E-13
float412 5.69E-34
float413 9.36E+29
float414 -6.41E-19
float415 1.39E+22
float416 -1.05E-14
float417 7.21E-04
float418 -5.97E-28
float419 -1.50E+31
float420 3.37E-02
float421 6.82E-33
float422 2.30E-31
float423 6.77E-32
float424 4.60E-30
float425 6.03E-33
float426 2.75E-32
float427 2.73E-30
float428 3.23E-31
float429 1.27E-33
float430 3.41E-32
float431 1.40E-30
float432 1.04E+04
float433 5.19E+05
float434 4.62E-02
float435 -1.68E-13
float436 -1.36E+19
float437 -3.49E+21
float438 3.33E+15
float439 3.37E-02
float440 6.82E-33
float441 4.56E-32
float442 4.49E-33
float443 3.00E-34
float444 1.15E-31
float445 3.90E-33
float446 7.25E-31
float447 5.87E+21
float448 8.89E-36
float449 3.37E-02
float450 3.33E-02
float451 6.83E-33
float452 9.83E-31
float453 2.65E-31
float454 7.32E+06
float455 6.85E+28
float456 -2.01E+26
float457 6.75E-03
float458 -1.02E-17
float459 9.54E-27
float460 1.94E+05
float461 3.49E+21
float462 -9.77E-24
float463 2.13E+17
float464 1.98E+08
float465 1.57E+37
float466 -2.82E-06
float467 2.50E-21
float468 6.25E-22
float469 -2.62E-15
float470 8.72E+20
float471 -4.96E+07
float472 -6.56E-16
float473 2.51E+38
float474 2.56E-18
float475 -1.27E+10
float476 5.07E+10
float477 -2.22E-36
float478 1.39E-37
float479 -5.20E+13
float480 -1.89E+02
float481 -1.49E-28
float482 -2.50E-21
float483 -1.42E-34
float484 -1.36E+19
float485 5.85E+28
float486 -1.80E-04
float487 3.56E-35
float488 -2.13E+17
float489 -1.13E-05
float490 -1.42E-34
float491 8.51E+17
float492 1.57E+37
float493 -9.32E-30
float494 -3.64E-32
float495 -2.50E-21
float496 2.51E+38
float497 -9.81E+35
float498 -3.93E+36
float499 9.36E+29
float500 -9.81E+35
float501 -3.66E+27
float502 9.14E+26
float503 8.31E+14
float504 5.69E-34
float505 1.46E-31
float506 1.07E-11
float507 -3.47E-38
float508 -4.95E+07
float509 -3.25E+12
float510 -3.83E+33
float511 -6.56E-16
-13299883775623168.0])

This article has been dead for over six months. Start a new discussion instead.