cmd=""" # ---------------- # setup as root !! # ---------------- /usr/bin/umount /ram-dir # create ramfs mkdir -p /ram-dir /usr/bin/mount ramfs -t ramfs /ram-dir ## or tmpfs ## /usr/bin/mount tmpfs -t tmpfs -o uid=bob -o gid=bob /ram-disk mkdir /ram-dir/user chown user:user /ram-dir/user dd if=/dev/zero of=/ram-dir/user/x bs=100M count=10 status=progress dd if=/dev/zero of=/ram-dir-x bs=100M count=10 status=progress mount | grep ram-dir # ---------------- """ #print(cmd) import time #fname = "/ram-dir/user/data.bin" #fname = "/media/user/FAT2G-BLACK/data.bin" fname = "/tmp/data.bin" print(fname) def init_bin(f): f.seek(0) for i in range(512): f.write(b'\x00') f.flash() data = [0]*512 #bytearray import struct import os def patch(i,v): try: #f=open(fname,"a+b", buffering=0) f=open(fname,"r+b", buffering=0) r=f.seek(0) #,whence=os.SEEK_SET) #f.truncate() print(r) r=f.seek(i) #,whence=os.SEEK_SET) #f.truncate() print(r) if v > 255: v=255 v=struct.pack('B',v) print("write",round(time.time(),3),":",i,v) f.write(v) f.flush() except Exception as e: print("server-err",e) def server(): try: f=open(fname,"wb", buffering=0) #f.seek(0) init_bin(f) except Exception as e: print("server-err",e) time.sleep(1) print(dir(f)) print(data) while 1: try: f.seek(0) for i in range(200,0,-1): i+=20 if i >= len(data): continue f.seek(i) v=data[i] #print(data) v+=1+i if v > 255: v=1 data[i]=v #v=bytes(v) v=struct.pack('B',v) print(round(time.time(),3),":",i,v) f.write(v) f.flush() time.sleep(0.02) time.sleep(10) except Exception as e: print("server-err",e) time.sleep(1) def client(): try: f=open(fname,"rb", buffering=0) except Exception as e: print("client-err",e) time.sleep(1) v='' i=0 while 1: try: start = time.time() f=open(fname,"rb", buffering=0) i=0 f.seek(0) v = f.read(1) while v: v2=b'' if v: v2=struct.unpack('B',v) if v != data[i]: print(round(time.time(),4),i,v,v2) data[i] = v v = f.read(1) i+=1 time.sleep(0.01) except Exception as e: print("client-err",e) time.sleep(1) import sys if __name__ == "__main__": print("ARGV", sys.argv) if "server" in sys.argv: server() elif "client" in sys.argv: client() elif "patch" in sys.argv: patch(int(sys.argv[2]),int(sys.argv[3]))