I am writing a program to control a machine, and this machine needs to have a central registry to keep track of all parameters about the machine (Inputs/outputs, configuration values, etc.). I've set up a list of zeros using the numpy library, and I am able to write to this list correctly. The problem, however, is when I attempt to then read that same list from another process or directly from my main code. I have verified that the data is updating to the list by performing a read action directly after writing. But reading from it from another function shows that it has not been changed.
Here is my code:
The final print(self.registry[reg_addr]) successfully prints the written values. However the following does NOT work:
Standby for edited post... For some reason it did not save my edits. The above post is not up to date
As you can see, the print statement inside the write16_register shows that the registry has been written to. But the print statement in read_register shows that it is unchanged and still zeroes.
Does anyone know why I might be running into this issue?
Here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#import numpy as np class Registry( object ): def __init__( self , registry_size = 128 , register_size = 16 ): self .registry_size = registry_size self .register_size = register_size self .registry = np.zeros((registry_size, register_size), dtype = int ) self .buff = np.zeros(( 1 , 16 )) # Buffer for data in/out def write16_register( self , reg_addr, data): # Convert data to 16 digit binary and sign extend data = int (data) # Split data into 8 bit MSB and LSB data_MSB = (data & 0xFF00 ) >> 8 data_LSB = (data & 0x00FF ) # Load MSBs to buff self .buff = format (data_MSB, '08b' ) # Write MSBs to registry for i in range ( 0 , 8 , 1 ): self .registry[reg_addr][i] = self .buff[i] # Load LSB to buffer self .buff = format (data_LSB, '08b' ) # Write LSB to registry for i in range ( 8 , 16 , 1 ): self .registry[reg_addr][i] = self .buff[i - 8 ] print ( self .registry[reg_addr]) if __name__ = = '__main__' : p1 = multithreading.Process(target = |
1 2 |
def read_register( self , reg_addr): print ( self .registry[reg_addr]) |
Standby for edited post... For some reason it did not save my edits. The above post is not up to date
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
import numpy as np import multiprocessing class Registry( object ): def __init__( self , registry_size = 128 , register_size = 16 ): self .registry_size = registry_size self .register_size = register_size self .registry = np.zeros((registry_size, register_size), dtype = int ) self .buff = np.zeros(( 1 , 16 )) # Buffer for data in/out def write16_register( self , reg_addr, data): # Convert data to 16 digit binary and sign extend data = int (data) # Split data into 8 bit MSB and LSB data_MSB = (data & 0xFF00 ) >> 8 data_LSB = (data & 0x00FF ) # Load MSB self .buff = format (data_MSB, '08b' ) for i in range ( 0 , 8 , 1 ): self .registry[reg_addr][i] = self .buff[i] # Load LSB self .buff = format (data_LSB, '08b' ) for i in range ( 8 , 16 , 1 ): self .registry[reg_addr][i] = self .buff[i - 8 ] print ( self .registry[reg_addr]) def read_register( self , reg_addr): print ( self .registry[reg_addr]) if __name__ = = '__main__' : Main_Registry = Registry() p1 = multiprocessing.Process(target = Main_Registry.write16_register, args = ( 0x00 , 0xB594 ,)) p2 = multiprocessing.Process(target = Main_Registry.read_register, args = ( 0x00 ,)) p1.start() p2.start() p1.join() p2.join() |
Quote:Output:
[1 0 1 1 0 1 0 1 1 0 0 1 0 1 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
As you can see, the print statement inside the write16_register shows that the registry has been written to. But the print statement in read_register shows that it is unchanged and still zeroes.
Does anyone know why I might be running into this issue?