I build a sensor unit where data are gathered at one Raspberry Pi and send to another's over the network. My first Pi creates a line with multiple readings from different sensors. It supposed to create a server and send it to clients. The client Pis needs to receive the sentence, do further processing or visualisation.
To test my solutions I want to read data from a txt file, which was build in an experiment. The problem is that sometimes data are corrupted, has different format depending on sensor and rows can be different for different set-ups.
I have build a function which suppose to change the input line to bytes. (I tried different methods but only this clunky function is the closest to any results). But it does not convert over the network
import struct
message = ['First sensor', 'second data', 'third',1, '19.04.2016', 0.1]
def packerForNet(message): patte = '' newMessage = [] for cell in message: if isinstance( cell, int ): patte += ('I') newMessage.append(cell) elif isinstance( cell, float ): patte += ('d') newMessage.append(cell) elif isinstance(cell, str): patte += (str(len(cell))) patte += ('s') newMessage.append(cell.encode('UTF-8')) else: cell = str(cell) patte += (len(cell)) patte += ('s') newMessage.append(cell.encode('UTF-8')) retu (newMessage, patte)
newMessage, patte = packerForNet(message)
patteStruct = struct.Struct(patte)
packedM = patteStruct.pack(*newMessage)
The output from the function does not unpack correctly:
packedM = b'First sensorsecond datathirdx01x00x00x0019.04.2016x00x00x00x00x00x00x9ax99x99x99x99x99xb9?'
56 print('unpacked = %s' % patteStruct.unpack(packedM))
TypeError: not all arguments converted during string formatting
In addition I need to know the patte
to unpack it on the client side so in general it doesn't have sense.
In final version the server needs to work in the way that after client connects it will send to the client line from the sensors every millisecond. Sensors parsing is implemented in C and at the moment it creates a txt file for off-line processing. I can't change the way the sensor's line are made.
I don't know how to pack the list of different types and constantly send such lists to the client.