Monitoring a variable to change in Python

Firstly, it will probably be a long time, so bear with me and excuse me in advance if my description may be incorrectly worded, I was not sure how to explain it.

I am new here and new to programming (with the exception of a high school course in Pascal and a college course in C ++, both many years ago.) I now teach Python myself and try to code a small project for the problem I'm at work. Surprisingly, so far I am satisfied with my project and to a large extent work, but I try to make it a little better. Basically, I take a read from the serial port, removing the carriage return from the specified read, and then try to match my serial output with the record in the SQL database table and perform the necessary actions depending on the results. I have everything that works quite well, but I ran into a problem that I don’t know how to solve.

I use "ctypes" to interact with the DLL file provided by the hardware manufacturer. The hardware is a digital input / output board that has 8 digital inputs and 8 relay outputs. The DLL allows me to read the status of inputs and outputs, as well as control the outputs (turn relays on and off). This is mainly done as follows:

mydll = cdll.LoadLibrary("acces32.dll") input_status = mydll.InPortB(0xDC7C+1) 

Where DC7C is the base address of the card in HEX, and +1 is how the input status is returned. The input state is returned as a decimal number from 0 to 255, which when converted to a binary file represents the state of 8 inputs, that is:

 0 0 0 0 0 0 0 0 - decimal=0 - all inputs off 0 0 0 0 0 0 0 1 - decimal=1 - input 1 on 1 0 0 0 0 0 0 1 - decimal=129 - input 8 and 1 on etc, etc, etc 

Currently, my equipment uses the first two inputs, so my possible results are 0, 1, 2 or 3. But it would be nice to take into account all the possibilities, but I do not collect my mathematics to determine which inputs are turned on or off. What I'm trying to accomplish is to control the input of one state to see if it is turned off or on (0 or 1). Here is my current code snippet:

 def input(): mydll = cdll.LoadLibrary("acces32.dll") input_status = mydll.InPortB(0xDC7C+1) while input_status != 1 or 3: arming = mydll.InPortB(0xDC7C+1) print "System is idle" #input 1 off #added the sleep as without it was using alot of CPU time time.sleep(0.5) print "Waiting for user input..." # then call bar_code function #calls another function which reads data from a serial connected bar code reader bar_code_reader() 

While this more or less works, it is very obviously not ideal and does not take into account what happens if input state 1 changes after the bar_code_reader function is run. Thus, more or less, what is the proper way to constantly monitor these inputs, even if I am in bar_code_reader mode, and then input 1 is changed to a turned off program, it will return to “System in standby mode”.

Secondly, I do not take into account all possible values ​​of "input_status", where input 1 is turned on, currently I just take into account the use of two inputs, so I know that the value will be 0, 1, 2 or 3, but if in the future I I will use other resources, I have to take this into account now, I suppose. Is there an easy way to account for all possible values ​​when input 1 is turned on? I am sure there is simple math for this, which I forget. I was thinking about converting decimal output to binary and checking this path, but was not sure if this is the best way to do this. I figured out how to convert to binary, but as for me.

Basically, to summarize:

While input 1 is 0, the system remains in standby mode. While input 1 is 1, then proceed to collect user input (in the form of a bar code), but if input 1 returns to 0, it returns to the idle state.

If you need to see more code or something else, let me know. I know that it was a long time, and I probably do everything back.

Thanks in advance,

Kevin

+4
source share
3 answers

It looks like you have two problems: how to find out if input 1 turned input 1 , and abort bar_code_reader if input 1 turned off.

The first is easy: input_status & 1 will evaluate to True , if this first bit is turned on, False otherwise.

The second is a bit more complicated.

Under normal circumstances, will bit 0 still enabled onter bar_code_reader() returned successfully?

  • If so, just check the bit after trying to read the barcode, and if it is still on you, you have good data.

  • If not, you will need to put the check in bar_code_reader itself (possibly several times), and then bar_code_reader to return a good value when bit 0 remained on all the time, and None otherwise. (You can use any donor other than None , this is simply the most common.)

Oh, and therefore you do not need to remember which bit field is equal to the number:

 BIT0 = 2 ** 0 BIT1 = 2 ** 1 BIT2 = 2 ** 2 BIT3 = 2 ** 3 BIT4 = 2 ** 4 BIT5 = 2 ** 5 BIT6 = 2 ** 6 BIT7 = 2 ** 7 
+2
source

To check if input 1 is enabled, you could input_status & 0x1 if input_status is an integer.

+2
source

Given that you only have 8 possible bits, I would suggest that the simplest task is to just bitwise and and or with masks that you format as binary integers in your code (ex 0b00000001). You can configure a number of values ​​if you want, for example:

 inputs = [] for i in range(0, 8): input[i] = 2**i 

then use inputs[i] in bitwise operations instead of an explicit value.

As for your other problem, you will have to process the scan method inside the scan method, as others have pointed out. You would put all the clutter in the while True , and then if inputs[0] & input_status: bar_code_reader() in the loop body.

+1
source

Source: https://habr.com/ru/post/1400445/


All Articles