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"
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