How to create script to read binary file having different data types using Python in Ignition and add data to excel sheet in tabular format. File is of 220 bytes

Following is the table of structure of data in binary file with size in bytes and data types and script should read data in the same sequence shown in table starting from MacID with 50 bytes of char data type to idealcycletime with 4 bytes of float data type.
|DATA TYPE|DATA NAME|DATA SIZE|EXAMPLE VALUES|DESCRIPTION|

||char|MacID[50];|50 Bytes|DDDDDDDDDDDDDDD|machine identification name set by customer|
||char|MacNo[16];|16 Bytes|TD - 11111111|machine number set at factory|
||char|MacCf[50];|50 Bytes|150/510 - 600, D50|machine configuration (format: clamp force/tie bar distance - iu specificatin, screw diameter in sqmm|
||char|MoldId[16];|16 Bytes|CCCCCCCCCCCCCCC|mold identification name set by customer|
||char|PartNo[16];|16 Bytes|BBBBBBBBBBBBBBB|part number set by customer|
||char|MaterialName[16];|16 Bytes|AAAAAAAAAAAAAAA|material name used to mold the component set by customer|
||ushort|MacMode;|2 Bytes|0->INIT; 1->SETTING; 2->MANUAL; 3->SEMI-AUTO; 4->AUTO|machine mode|
||ushort|CavityCnt;|2 Bytes|12|mold cavity count|
||ushort|PmpSts;|2 Bytes|0->OFF; 1->ON|pump on/off status|
||ushort|HtrSts;|2 Bytes|0->OFF; 1->ON|barrel heater on/off status|
||float|ShotWeight;|4 Bytes|22.32|shot weight in grams|
||uint|GoodPartAct;|4 Bytes|240|total good parts produced|
||uint|RejectAct;|4 Bytes|12|total rejected parts|
||uint|GoodShotAct;|4 Bytes|20|total good shots produced|
||uint|TotalShotAct;|4 Bytes|21|total shots produced including rejects|
||uint|BinGoodPartAct;|4 Bytes|60|total good parts produced for a bin|
||uint|BinRejectAct;|4 Bytes|12|total rejected parts for a bin|
||uint|BinGoodShotAct;|4 Bytes|5|total good shots produced for a bin|
||uint|BinTotalShotAct;|4 Bytes|6|total shots produced including rejects for a bin|
||uint|GoodPartSet;|4 Bytes|1000000|total good parts to be produced set by customer|
||int|EnergyVal|4 Bytes||energy value in kwh|
||float|IdealCycTime|4 Bytes|12.6|Ideal Cycle time in seconds|

I would read the entire file as bytes, wrap the byte array in a java.nio.ByteBuffer, then read and convert from the BB.

I am looking for python script as i am new with python and not a clue where to start.

Try this for starters. Here is a link to Ignition’s built in scripting functions.

path = system.file.openFile('bin', '\\optional\DefaultFileLocation')
if path != None:
   system.file.readFileAsBytes(path)

Once you read the file as bytes use Java’s ByteBuffer as pturmel suggested. Ignition uses Jython not Python, so you can import java.nio.ByteBuffer .

1 Like

Thanks.
I have converted the file in to array but now I dont know how to use java.nio.ByteBuffer function in ignition. Below is the Return.
array(‘b’, [84, 69, 83, 84, 32, 77, 65, 67, 72, 73, 78, 69, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 84, 83, 32, 45, 32, 48, 49, 53, 48, 48, 48, 54, 50, 0, 0, 0, 49, 53, 48, 47, 53, 49, 48, 32, 45, 32, 57, 48, 48, 44, 32, 68, 53, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 49, 50, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 75, 79, 54, 56, 49, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 80, 80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 1, 0, 1, 0, 1, 0, 0, 0, 22, 67, 85, 59, 4, 0, 1, 0, 0, 0, 85, 59, 4, 0, 86, 59, 4, 0, 85, 59, 4, 0, 1, 0, 0, 0, 85, 59, 4, 0, 86, 59, 4, 0, -1, -55, -102, 59, 0, 0, 0, 0])

I’m not very good with Java, but this will get you started. Read the documentation on the ByteBuffer methods, like get() and wrap() etc, then look up Java code examples. This will help you understand how to use it.

from java.nio import ByteBuffer
path = system.file.openFile('bin', '\\optional\DefaultFileLocation')
if path != None:
	myBytes = system.file.readFileAsBytes(path)
	myBuffer = ByteBuffer.wrap(myBytes)

	while(myBuffer.hasRemaining()):
		myBuffer.get()