First off, this will probably be long winded so bear with me and sorry in advance if my description may not be correctly worded, I wasn't sure how to explain it.
I am new here and new to programming(except for a high school course in Pascal and a college course in C++, both many moons ago.) I am currently teaching myself Python and trying to code a small project for a problem I am having at work. Surprisingly, I am faring pretty well so far with my project and pretty much have it working but I am trying to make it slightly better. Basically I am taking a reading from the serial port, stripping the carriage return from said reading, then I try to match my serial output to a record in a SQL database table and depending on the results perform necessary action. I have all of that working reasonably well but I have run into an issue which I am not sure how to solve.
I am using "ctypes" to interface with a DLL file provided by a hardware manufacturer. The hardware in question is a digital I/O card which has 8 digital inputs and 8 relay outputs. The DLL allows me to read the status of the inputs and outputs and also I can control the outputs(turn relays on and off). Basically this is done like so:
mydll = cdll.LoadLibrary("acces32.dll")
input_status = mydll.InPortB(0xDC7C+1)
Where DC7C is the base address of the card in HEX, and the +1 is how the input status is returned. The input status is returned as a decimal from 0 - 255, which when converted to binary represents the status of the 8 inputs, i.e.:
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
MY equipment is currently using the first two inputs at the moment, so my possible outcomes are 0, 1, 2 or 3. But it would be nice to account for all possibilities but I am not wrapping my head math involved to easily determine which inputs are on or off. What I am trying to accomplish is to monitor input one's status to see if it is 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 very obviously is not ideal and doesn't account for what happens if the state of input 1 changes after the "bar_code_reader" function is run. So more or less what is the proper way to achieve a constant monitoring of these inputs so even if I am in bar_code_reader mode and then input 1 changes to off the program will go back to a "System is idle state".
Secondly I am not accounting for all possible values of "input_status" where input 1 is on, currently I am just accounting for the use of two inputs so I know the value will be 0, 1, 2 or 3 but if I happen to use other inputs in the future I should account for this now I suppose. Is there a simple way to account for all the possible values when input 1 is on? I am sure there is some simple math for this that I am forgetting. I was thinking of converting the decimal output to binary and checking that way but wasn't sure if that was the best way to go about it. I did figure out how to convert to binary but that's as far as I went with it.
Basically to sum it up:
While input 1 is 0 then the system remains idle, While input 1 is 1 then proceed with gathering user input(in the form of a bar code) but if input 1 returns to 0 return back to an idle state.
If you need to see more code or whatever please let me know. I know this was long and I am probably doing everything backwards....
Thanks in advance,
Kevin
input_status != 1 or 3is always truewhile input_status != 1 or input_status != 3:bar_code_reader()is a function: it does something. It's not a "mode" that you can switch out of! Are you saying you want to interrupt it if the mode changes halfway? That's complicated, because it means you need to do two things at once, which leads you to Python'sthreadingormultiprocessinglibraries.while input_status not in (1,3).