Posted Tue, 28 Mar 2023 22:42:49 GMT by Johnson, Sheldon
Hello,&#160;<br> <br> I am trying to get a PNG file over Ethernet socket. Every time I get the file its is missing the header of the file.<br> <br> Looks like it's missing the first 0x2D (45) bytes of the PNG file. This is very consistent, it happens every time. How do I make sure I get the PNG header so I can open the file correctly?&#160;<br> <br> If I try it with pyVisa it works correctly, but unfortunately, Apple silicon is not supported by pyVisa so I need to switch to sockets.<br> <br> Here is my code but it's not formatted correctly cause I guess you can't copy and paste correctly in this website.&#160; <pre class="linenums prettyprint">import argparse from configparser import ConfigParser import os import sys import socketToVisa as s2v import time from datetime import datetime configFile = 'config.ini' class Instrument: def __init__(self, ipAddress, port): try: #create an AF_INET, STREAM socket (TCP) self.instrument = socket.socket(socket.AF_INET, socket.SOCK_STREAM) except : msg = socket.herror() print(f'Error Creating Socket. Code:{msg[0]} Description: {msg[1]}') socket.close() self.instrument.connect((ipAddress , port)) self.instrument.setblocking(False) def __del__(self): self.disconnect() def disconnect(self): '''closes connection''' self.instrument.shutdown(socket.SHUT_RDWR) self.instrument.close() def write(self, command: str): if not command.endswith('\n'): command = command + '\n' command = bytes(command, 'utf-8') self.instrument.sendall(command) def read(self, numberOfBytes = None): response = '' while True: char = &quot;&quot; try: char = self.instrument.recv(1).decode('utf-8') except: time.sleep(0.1) if response.rstrip() != &quot;&quot;: break if char: response += char return response.rstrip() # if numberOfBytes != None: # value = self.instrument.recv(numberOfBytes) # else: # value = self.instrument.recv() # return value.decode('utf-8') def read_raw(self, numberOfBytes = None): # if numberOfBytes != None: # value = self.instrument.recv(numberOfBytes) # else: # value = self.instrument.recv(4096) # return value response = b&quot;&quot; while True: char = bytearray(b'') try: char = self.instrument.recv(100) print(char) input() except: print('no char') time.sleep(0.1) if response.rstrip() != b'': break # if char == b'\n': # print('char = LE') # break if char: response += char time.sleep(0.001) return response.rstrip() def query(self, command, delay = 0, recNumberOfbytes = None): self.write(command) time.sleep(delay) readValue = self.read() return readValue if __name__ == '__main__': thisPath = os.path.dirname(__file__) configFileFullPath =os.path.join(thisPath, configFile) parser = argparse.ArgumentParser(prog='Tek Scope Control', description='Command and Control Tek scope') parser.add_argument('-i', '--init', action='store_true', help = 'Create ini file') #parser.add_argument('-t', '--times', action='store_true',help= &quot;Calculate times&quot;) #parser.add_argument('-r', '--remake', action='store_true',help= &quot;Remake the DF&quot;) args = parser.parse_args() if args.init: createInitFile(configFileFullPath) print('config.ini created, edit file to match your setup') sys.exit() else: assert os.path.isfile(configFileFullPath), &quot;run -i to create a ini file, Edit file to match you setup&quot; configDict = readConfigFile(configFileFullPath) print(configDict) scope = s2v.Instrument(ipAddress= configDict['ip_address'], port=configDict['port']) print('Scope Setup') scope.write('*CLS\n') #scope.write('*RST;*OPC?') #time.sleep(0.001) #read = scope.read(8) #print(read) read = scope.query(&quot;*IDN?\n&quot;) # time.sleep(0.1) # read = scope.read(255) print(read) print('Writing PNG to scope') scope.write('SAVE:IMAGe \&quot;C:/Temp.png\&quot;\n') scope.query('*OPC?\n') scope.write('SAVe:WAVEform:FILEFormat SPREADSheet\n') scope.query('*OPC?\n') #scope.write('SAVE:WAVEform ALL,\&quot;C:/Temp.csv\&quot;\n') #scope.query('*OPC?\n') dt = datetime.now() fileName = dt.strftime(&quot;MSO5_%Y%m%d_%H%M%S.png&quot;) # Wait for instrument to finish writing image to disk time.sleep(2) scope.write('FILESystem:READFile \&quot;C:/Temp.png\&quot;\n') print('Writing PNG to Computer') imgData = scope.read_raw(2_073_999) #imgData = scope.read_raw(2_073_600) file = open(configDict['png_path'] +fileName, &quot;wb&quot;) file.write(imgData) file.close() </pre>
Posted Wed, 29 Mar 2023 17:22:44 GMT by Teles, Afonso
Hi,<br> <br> I would strongly suggest staying away from raw sockets as much as possible.<br> <br> Some people do seem to have been able to get pyvisa working on apple silicon: <a href="https://github.com/pyusb/pyusb/issues/355">https://github.com/pyusb/pyusb/issues/355</a><br> You will probably need to use PyVISA-py as the backend: <a href="https://pypi.org/project/PyVISA-py/">https://pypi.org/project/PyVISA-py/</a>
Posted Wed, 29 Mar 2023 23:20:14 GMT by Johnson, Sheldon
Thanks, that does seem to get pyvisa working for my Apple silicon. I will go that path, if sockets are so terrible.&#160;

You must be signed in to post in this forum.