问题
Background
I am a researcher, using a piece of equipment that is remotely logging various data and sending this to a server. I have had a problem where the data wasn't converted by the server into a CSV and need to manually decode it.
The manufacturers of the equipment are unable to do this for me and have given me the following advice:
The data is imbedded in the Logger to Gateway, DD messages following the next 2 numbers.
The logger data is 16 bit (2 bytes) and is represented by either an ASCII character if it falls into the ASCII range or by a Hex representation eg [0xfd]. There are 2n bytes of data where n is the number of channels.
You can convert to CSV using the standard linear convertion given by each channel’s scaling. Look at your current CSV and the original DD message for that set of readings.
I must admit, I don't really understand this.
The problem
I have a number of individual 'log files' (.txt), which contain the information that I need to decode. A small section of one of the log files is shown below:
12:09:01:Bind() Ok
12:57:10:,Logger to Gateway,LI,14,58957,K01817-12158
12:57:10:,Gateway to Logger,TM,8,58957,[0xc][0xc][0x13][0xc]9[0xa]
12:57:13:K01817-12158,Logger to Gateway,SD,30,58957,[0xa][0x3][0x13]c[0x0][0x0][0x0][0x0][0x0][0x0][0x7f][0xa0]d[0x0][0xac][0x7f][0x9e]d[0x0][0xfb][0x7f][0x90]d[0x0]3[0x0][0x0][0x0]
12:57:13:,Gateway to Logger,DK,2,58957,
12:57:16:K01817-12158,Logger to Gateway,DD,70,58957,,[[0x8][0x1c][0x12][0x6][0xc][0x13][0xa6][0x1]a[0xc][0x9c][0x1][0xf4][0x0][0xa4][0x1][0xf7][0x1][0xa8][0x0],[0x4][0xfe][0x0][0x7][0x0][0x7][0x2][0xf0][0x7]L[0x2]u[0x0][0xa5][0x0][0xe3][0x0][0xb9][0x0]@[0x4][0xb][0x1][0x4][0x0][0x6][0x2][0x14][0x8]m[0x2]v[0x0][0xb3][0x0][0xbb][0x0][0xc5][0x0][0xa2][0xff][0x12][0x1][0x6][0x0]
12:57:16:,Gateway to Logger,DK,2,58957,
12:57:24:K01817-12158,Logger to Gateway,DD,70,58957,,\[0x8]![0x12][0x6][0xc][0x13][0xa6][0x1]T[0xc][0xa1][0x1][0xc3][0x0]\[0x1][0x9f][0x1][0xb0][0x0][0x18][0x4][0xff][0x0][0x7][0x0][0x7][0x2][0xf7][0x7]W[0x2][0x8d][0x0][0xd4][0x0][0xdd][0x0][0xb1][0x0]B[0x4][0xb][0x1][0x4][0x0][0x6][0x2][0xf][0x8]o[0x2][0x86][0x0][0xbd][0x0][0xca][0x0][0xc5][0x0][0xa2][0xff][0x10][0x1][0x6][0x0]
12:57:24:,Gateway to Logger,DK,2,58957,
12:57:31:K01817-12158,Logger to Gateway,DD,70,58957,,][0x8]&[0x12][0x6][0xc][0x13][0xa6][0x1]V[0xc][0xad][0x1][0xe8][0x0][0xa5][0x1]'[0x2][0xa2][0x0]"[0x4][0x0][0x1][0x7][0x0][0x8][0x2][0xfc][0x7]U[0x2][0x8f][0x0][0xd9][0x0]n[0xb][0xc3][0x0]F[0x4][0xb][0x1][0x4][0x0][0x6][0x2][0xa][0x8]k[0x2]q[0x0][0xab][0x0][0x12][0x4][0xc5][0x0][0xa2][0xff][0xe][0x1][0x6][0x0]
What i need to do
I need to write some code in Python to import all the log files, decode the information and add it to a larger dataset. The format of the dataset that the server creates is given below:
I am struggling where to start with this problem - my skills are normally in manipulating data once its imported and in a format I can use.
Thank you so much for any help you can offer...
来源:https://stackoverflow.com/questions/59309469/convert-decode-string-of-hexidecimal-and-ascii-into-decimal