store changes

This commit is contained in:
S Groesz 2021-04-06 22:40:28 +00:00
parent 85aaf1385e
commit 003b9fa9ba
1 changed files with 92 additions and 3 deletions

View File

@ -70,6 +70,87 @@ MAX_ATTEMPTS = 10
MAX_TIMEOUT = 0.5 MAX_TIMEOUT = 0.5
class nf_header():
"""
Wrapper for netfind header packets
"""
def __init__(self, hdr=None):
if hdr is None:
# create a default header
hdr = bytes([NF_MAGIC, 255, 255, 255, 255, 255, 255, 255, 255, 255,
0, 0])
if type(hdr) != bytes:
raise TypeError("hdr must by bytes object")
if len(hdr) != bytes:
raise ValueError("hdr must be 12 bytes long")
if hdr[0] != NF_MAGIC:
raise ValueError("First byte of hdr must be NF_MAGIC byte")
if hdr[10] != 0 and hdr[11] != 0:
raise ValueError("Last two bytes of hdr should be NULL")
self.__header = bytearray(hdr)
@property
def id(self):
return self.__header[0]
@property
def command(self):
return self.__header[1]
@command.setter
def command(self, cmd):
if type(cmd) not in (bytes, int):
raise TypeError("cmd must be <int> or <bytes>")
if type(cmd) == bytes:
if len(cmd) != 1:
raise ValueError("cmd must be between 0 and 16")
cmd = ord(cmd) # convert bytes to int
if type(cmd) == int:
if cmd < 0 or cmd > 16:
raise ValueError("cmd must be > 0 and < 16")
self.__header[1] = cmd
@property
def seq(self):
return int.from_bytes(self.__header[2:4], "big")
@seq.setter
def seq(self, seq):
if type(seq) not in (bytes, int):
raise TypeError("Seq must be int or bytes object")
if type(seq) == bytes:
if len(seq) != 2:
raise ValueError("Seq of type <bytes> must be 2 bytes")
seq = int.from_bytes(seq, "big")
if type(seq) == int and (seq < 1 or seq > 65535):
raise ValueError("Seq must be greater than 0 and less than 65536")
# by this point seq should be a valid int between 1 and 65535
self.__header[2:4] = seq.to_bytes(length=2,
byteorder="big"
signed=False)
@property
def target(self):
return self.__header[4:10]
@target.setter
def target(self, addr):
if type(addr) != bytes:
raise TypeError("addr must be bytes object")
if len(addr) != 6:
raise ValueError("addr must be 6 bytes")
self.__header[4:10] = addr
@property
def pad(self):
return self.__header[10:]
@property
def bytes(self):
return bytes(self.__header)
class gpib_eth(): class gpib_eth():
""" """
Prologix GPIB Ethernet Simulator Version 1.0 Prologix GPIB Ethernet Simulator Version 1.0
@ -118,7 +199,11 @@ class gpib_eth():
Send data (msg) Send data (msg)
""" """
def receive(self): def monitor_broadcast(self):
broadcast_msg = self.receive('<broadcast>', NETFINDER_SERVER_PORT)
return broadcast_msg
def receive(self, addr, port, buffer=1024):
""" """
Receive data (msg) Receive data (msg)
""" """
@ -129,8 +214,12 @@ class gpib_eth():
# 6 byte - Eth addr ((b'\xFF' * 6) for broadcast) # 6 byte - Eth addr ((b'\xFF' * 6) for broadcast)
# 2 byte - Null byte x2 (pad bytes) [EOM ?] # 2 byte - Null byte x2 (pad bytes) [EOM ?]
rcv_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) rcv_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
rcv_sock.bind(('<broadcast>', NETFINDER_SERVER_PORT)) rcv_sock.bind((addr, port))
msg = rcv_sock.recvfrom(1024) netpack = rcv_sock.recvfrom(buffer)
if len(netpack) => 12 and netpack[0] == NF_MAGIC:
packet = {}
header = nf_header(netpack[0:12])
# Header format: 2 char (bytes) - NF_MAGIC + Command
return msg return msg