<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div dir="ltr"></div><div dir="ltr"><br></div><div dir="ltr"><br><blockquote type="cite">Il giorno 16 feb 2022, alle ore 06:53, Vinny Mautone <vinny74man@gmail.com> ha scritto:<br><br></blockquote></div><blockquote type="cite"><div dir="ltr"><div dir="ltr">Ciao a tutti sto provando a leggere dei dati da un apparecchiatura con la quale comunico tramite il protocollo modbus, sto usando il modulo pymodbusTCP, ho alcuni dati come ad esempio la versione del firmware che occupano sei registri da 16 bit, riesco a leggerli e con la funzione di lettura del modulo ottengo una lista di 6 elementi da 16bit, solo che non riesco a scomporre e a recuperare l'informazione in modo leggibile, dal manuale leggo che il dato è formato da 11 byte in caratteri ascii, quindo ho capito che devo scomporre ogni elemento della lista nei due byte che lo compongono e poi riassemblare il tutto.<div><br></div><div></div></div><br></div></blockquote><div>Dall’esempio postato successivamente provo a spiegartelo meglio:</div><div><br></div><p style="margin: 0px; font-stretch: normal; font-size: 14.3px; line-height: normal; font-family: Menlo;"><span style="font-family: Menlo-Regular; font-size: 14.29px; font-variant-ligatures: no-common-ligatures;">>>> reg=[21070,12601,12340,12593,12593,13312]</span></p>
<p style="margin: 0px; font-stretch: normal; font-size: 14.3px; line-height: normal; font-family: Menlo;"><span style="font-family: Menlo-Regular; font-size: 14.29px; font-variant-ligatures: no-common-ligatures;"><br></span></p><p style="margin: 0px; font-stretch: normal; font-size: 14.3px; line-height: normal; font-family: Menlo;"><span style="font-family: Menlo-Regular; font-size: 14.29px; font-variant-ligatures: no-common-ligatures;"><span style="font-family: UICTFontTextStyleBody; font-size: 15px;">Per prima cosa dobbiamo trasformare nel relativo esadecimale usando la funzione hex </span></span></p><p style="margin: 0px; font-stretch: normal; font-size: 14.3px; line-height: normal; font-family: Menlo;"><span style="font-family: Menlo-Regular; font-size: 14.29px; font-variant-ligatures: no-common-ligatures;"><span style="font-family: UICTFontTextStyleBody; font-size: 15px;"><br></span></span></p><p style="margin: 0px; font-stretch: normal; font-size: 14.3px; line-height: normal; font-family: Menlo;"><span style="font-family: Menlo-Regular; font-size: 14.29px; font-variant-ligatures: no-common-ligatures;">>>> [hex(c) for c in reg]</span></p>
<p style="margin: 0px; font-stretch: normal; font-size: 14.3px; line-height: normal; font-family: Menlo;"><span style="font-family: Menlo-Regular; font-size: 14.29px; font-variant-ligatures: no-common-ligatures;">['0x524e', '0x3139', '0x3034', '0x3131', '0x3131', '0x3400']</span></p><p style="margin: 0px; font-stretch: normal; font-size: 14.3px; line-height: normal; font-family: Menlo;"><span style="font-family: Menlo-Regular; font-size: 14.29px; font-variant-ligatures: no-common-ligatures;">B</span></p><div>Ora vogliamo ottenere un unica stringa che rappresenti il valore esadecimale</div><div>e per questo dobbiamo fare una join ma eliminando ‘0x’ all’inizio. </div><div>Scriviamo quindi:</div><div><p style="margin: 0px; font-stretch: normal; font-size: 14.3px; line-height: normal; font-family: Menlo;"><span style="font-family: Menlo-Regular; font-size: 14.29px; font-variant-ligatures: no-common-ligatures;"><br></span></p><p style="margin: 0px; font-stretch: normal; font-size: 14.3px; line-height: normal; font-family: Menlo;"><span style="font-family: Menlo-Regular; font-size: 14.29px; font-variant-ligatures: no-common-ligatures;">>>> ''.join([hex(c)[2:] for c in reg])</span></p>
<p style="margin: 0px; font-stretch: normal; font-size: 14.3px; line-height: normal; font-family: Menlo;"><span style="font-family: Menlo-Regular; font-size: 14.29px; font-variant-ligatures: no-common-ligatures;">'524e31393034313131313400'</span></p><p style="margin: 0px; font-stretch: normal; font-size: 14.3px; line-height: normal; font-family: Menlo;"><span style="font-family: Menlo-Regular; font-size: 14.29px; font-variant-ligatures: no-common-ligatures;"><br></span></p><p style="margin: 0px; font-stretch: normal; font-size: 14.3px; line-height: normal; font-family: Menlo;"><span style="font-family: Menlo-Regular; font-size: 14.29px; font-variant-ligatures: no-common-ligatures;"><br></span></p></div><div>Ora trasformiamo in un array di byte:</div><div><p style="margin: 0px; font-stretch: normal; font-size: 14.3px; line-height: normal; font-family: Menlo;"><span style="font-family: Menlo-Regular; font-size: 14.29px; font-variant-ligatures: no-common-ligatures;"><br></span></p><p style="margin: 0px; font-stretch: normal; font-size: 14.3px; line-height: normal; font-family: Menlo;"><span style="font-family: Menlo-Regular; font-size: 14.29px; font-variant-ligatures: no-common-ligatures;">>>> bytearray.fromhex(''.join([hex(c)[2:] for c in reg]))</span></p>
<p style="margin: 0px; font-stretch: normal; font-size: 14.3px; line-height: normal; font-family: Menlo;"><span style="font-family: Menlo-Regular; font-size: 14.29px; font-variant-ligatures: no-common-ligatures;">bytearray(b'RN190411114\x00')</span></p><p style="margin: 0px; font-stretch: normal; font-size: 14.3px; line-height: normal; font-family: Menlo;"><span style="font-family: Menlo-Regular; font-size: 14.29px; font-variant-ligatures: no-common-ligatures;"><br></span></p><p style="margin: 0px; font-stretch: normal; font-size: 14.3px; line-height: normal; font-family: Menlo;"><span style="font-family: Menlo-Regular; font-size: 14.29px; font-variant-ligatures: no-common-ligatures;">E infine con decode() otteniamo la rappresentazione:</span></p><p style="margin: 0px; font-stretch: normal; font-size: 14.3px; line-height: normal; font-family: Menlo;"><span style="font-family: Menlo-Regular; font-size: 14.29px; font-variant-ligatures: no-common-ligatures;"><br></span></p><p style="margin: 0px; font-stretch: normal; font-size: 14.3px; line-height: normal; font-family: Menlo;"><span style="font-family: Menlo-Regular; font-size: 14.29px; font-variant-ligatures: no-common-ligatures;">>>> bytearray.fromhex(''.join([hex(c)[2:] for c in reg])).decode()</span></p><p style="margin: 0px; font-stretch: normal; font-size: 14.3px; line-height: normal; font-family: Menlo;"><span style="font-family: Menlo-Regular; font-size: 14.29px; font-variant-ligatures: no-common-ligatures;">
</span></p><p style="margin: 0px; font-stretch: normal; font-size: 14.3px; line-height: normal; font-family: Menlo;"><span style="font-family: Menlo-Regular; font-size: 14.29px; font-variant-ligatures: no-common-ligatures;">'RN190411114\x00'</span></p><p style="margin: 0px; font-stretch: normal; font-size: 14.3px; line-height: normal; font-family: Menlo;"><span style="font-family: Menlo-Regular; font-size: 14.29px; font-variant-ligatures: no-common-ligatures;"><br></span></p><p style="margin: 0px; font-stretch: normal; line-height: normal;"><font face="Menlo-Regular"><span style="font-size: 14.289999961853027px;">Per eliminare il char(0) finale possiamo rimpiazzare con</span></font></p><p style="margin: 0px; font-stretch: normal; line-height: normal;"><font face="Menlo-Regular"><span style="font-size: 14.289999961853027px;">stringa vuota:</span></font></p><p style="margin: 0px; font-stretch: normal; font-size: 14.3px; line-height: normal; font-family: Menlo;"><span style="font-family: Menlo-Regular; font-size: 14.29px; font-variant-ligatures: no-common-ligatures;"><br></span></p><p style="margin: 0px; font-stretch: normal; font-size: 14.3px; line-height: normal; font-family: Menlo;"><span style="font-family: Menlo-Regular; font-size: 14.29px; font-variant-ligatures: no-common-ligatures;">>>> bytearray.fromhex(''.join([hex(c)[2:] for c in reg])).decode().replace(chr(0),'')</span></p><p style="margin: 0px; font-stretch: normal; font-size: 14.3px; line-height: normal; font-family: Menlo;"><span style="font-family: Menlo-Regular; font-size: 14.29px; font-variant-ligatures: no-common-ligatures;">'RN190411114'</span></p><p style="margin: 0px; font-stretch: normal; font-size: 14.3px; line-height: normal; font-family: Menlo;"><span style="font-family: Menlo-Regular; font-size: 14.29px; font-variant-ligatures: no-common-ligatures;"><br></span></p><p style="margin: 0px; font-stretch: normal; line-height: normal;"><font face="Menlo-Regular"><span style="font-size: 14.289999961853027px;">Ciao</span></font></p><p style="margin: 0px; font-stretch: normal; line-height: normal;"><font face="Menlo-Regular"><span style="font-size: 14.289999961853027px;"><br></span></font></p><p style="margin: 0px; font-stretch: normal; line-height: normal;"><font face="Menlo-Regular"><span style="font-size: 14.289999961853027px;">G</span></font></p><p style="margin: 0px; font-stretch: normal; line-height: normal;"><font face="Menlo-Regular"><span style="font-size: 14.289999961853027px;"><br></span></font></p><p style="margin: 0px; font-stretch: normal; line-height: normal;"><font face="Menlo-Regular"><span style="font-size: 14.289999961853027px;"><br></span></font></p><p style="margin: 0px; font-stretch: normal; line-height: normal;"><font face="Menlo-Regular"><span style="font-size: 14.289999961853027px;"><br></span></font></p><p style="margin: 0px; font-stretch: normal; line-height: normal;"><font face="Menlo-Regular"><span style="font-size: 14.289999961853027px;"><br></span></font></p></div><div><br></div><div><pre style="-webkit-text-size-adjust: auto; box-sizing: border-box; overflow: auto; font-size: 14px; padding: 1px 0px; margin-top: 0px; margin-bottom: 0px; line-height: inherit; word-break: break-all; word-wrap: break-word; border: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; vertical-align: baseline;"><div class="cell code_cell unrendered unselected" tabindex="2" style="white-space: normal; box-sizing: border-box; -webkit-box-orient: vertical; -webkit-box-align: stretch; display: flex; flex-direction: column; align-items: stretch; border-top-left-radius: 2px; border-top-right-radius: 2px; border-bottom-right-radius: 2px; border-bottom-left-radius: 2px; border: 1px solid transparent; width: 1110px; padding: 5px; margin: 0px; outline: none; position: relative; overflow: visible; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; background-color: rgb(255, 255, 255);"><div class="input" style="box-sizing: border-box; break-inside: avoid; -webkit-box-orient: horizontal; -webkit-box-align: stretch; display: flex; flex-direction: row; align-items: stretch;"><div class="prompt_container" style="box-sizing: border-box; display: flex; flex-direction: row; justify-content: space-between; align-items: flex-start; text-align: right;"><div class="inner_cell" style="box-sizing: border-box; min-width: 0px; -webkit-box-orient: vertical; -webkit-box-align: stretch; display: flex; flex-direction: column; align-items: stretch; -webkit-box-flex: 1; flex: 1 1 0%;"><div class="input_area" aria-label="Edit code here" style="box-sizing: border-box; border: 1px solid rgb(207, 207, 207); border-top-left-radius: 2px; border-top-right-radius: 2px; border-bottom-right-radius: 2px; border-bottom-left-radius: 2px; background-color: rgb(247, 247, 247); line-height: 1.21429em;"><div class="CodeMirror cm-s-ipython" style="box-sizing: border-box; font-family: monospace; height: auto; direction: ltr; position: relative; overflow: hidden; background-image: none; line-height: 1.21429em;"></div></div></div></div></div></div></pre></div><div><div><br></div></div></body></html>