Hello, <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? <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. 
<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 = ""
try:
char = self.instrument.recv(1).decode('utf-8')
except:
time.sleep(0.1)
if response.rstrip() != "":
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""
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= "Calculate times")
#parser.add_argument('-r', '--remake', action='store_true',help= "Remake the DF")
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), "run -i to create a ini file, Edit file to match you setup"
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("*IDN?\n")
# time.sleep(0.1)
# read = scope.read(255)
print(read)
print('Writing PNG to scope')
scope.write('SAVE:IMAGe \"C:/Temp.png\"\n')
scope.query('*OPC?\n')
scope.write('SAVe:WAVEform:FILEFormat SPREADSheet\n')
scope.query('*OPC?\n')
#scope.write('SAVE:WAVEform ALL,\"C:/Temp.csv\"\n')
#scope.query('*OPC?\n')
dt = datetime.now()
fileName = dt.strftime("MSO5_%Y%m%d_%H%M%S.png")
# Wait for instrument to finish writing image to disk
time.sleep(2)
scope.write('FILESystem:READFile \"C:/Temp.png\"\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, "wb")
file.write(imgData)
file.close()
</pre>