Want to know how to control your light in VB? Please see - VB pairs IO port operation technology (repost)

xiaoxiao2021-03-06  62

Want to know how to control your light in VB? Please see - VB for I / O port operation technology

Time: 2004-8-10 Author: Unknown [obtain this document the absence of records, deeply sorry, this document is reproduced in full]

Want to know how to control your light in VB? See - VB For I / O Port Operation Technology 1. I / O port introduction signal acquisition and control is the most important part of industrial control. In industrial control, the signals typically collected have two categories: one is a voltage or current analog signal; the other is the amount of switching or digital pulse (switching, the amount of pulse belongs to a digital signal). If the signal usually detected is to be amplified, the transformation must be amplified, the conversion becomes 0 to 10 volts or currents 0 to 10mA, which must be converted into a number of modes / digital converters (ADC: Analog-Digital Cnverter) The amount can be accepted by the computer. The same output control quantity signal must also be a digital signal, and the digital signal is converted into an analog signal 0 to 10 volts (or 0 ~ 5 volts) or change to a current. 0 ~ 10mA, then send it to the controller to control the operation of the production equipment. In the computer, the CPU is connected to the input / output device via the bus. Many of the other devices in the system must pass through the bus, and the transmission or data of the data is performed with the CPU according to the time sequence specified by the CPU. Therefore, the input output device cannot be connected directly to the bus, and the I / O port is data transit station for data exchange between the bus and the input and output devices. We are actually associated with the I / O port with the input and output devices. Dialogue, the following detailed explains how to output and input operations in the Visual Basic environment. Second, the dynamic link library function Visual Basic does not provide the method of reading and writing I / O ports, so it can only be used to write dynamic link library DLL (Dynamic Link Library) or VXD with other languages. Conditioning in Visual Basic. So we have to learn how to write a dynamic link library function for operating I / O ports. 1. In assembly language, you can use I / O ports with in, OUT instructions, for example: input operation: MOV DX, port; port is port number IN AL, DX; read port data to AL register output: MOV DX Port; port is the port number MOV Al, Conbyte; Conbyte is the control word, send the control word to the Al register OUT DX, AL; send the control word to port 2. In C / C , use I / O ports with INP and _OUTP functions, write dynamic link libraries We can write two functions, compile it to generate DLL: / * roles: read a byte from the specified port , Parameter: portid port number, return value: read byte * / int _stdcall inport (short portid) {return INP (portid);} / * role: Write a byte to the specified port, parameter: portid port number * / Void _stdcall output (short portid, short value) {OUTP (portid, value);} When you need to operate two bytes, we must use two functions of INPW and OUTPW, specific usage with INP and OUTP. At the same time, it is necessary to explain that _inp and _outp functions and INP and OUTP functions are basically the same, just when using the header file, and I recommend using _inp and _outp, not using INP and OUTP . Because _inp and _outp can be used in Debug and Release, and INP and OUTP can only be used for Release settings.

If INP and OUTP are used when setting up, it is not wrong when compiling, but a link error is generated. Third, Visual Basic Operation I / O ports to this we can indirectly operate the I / O port in Visual Basic. The dynamic link library in Visual Basic must be declared in advance, which is default for public processes or functions. The format is: "Declare SUB process name lib" library name "[Alias ​​'alias" [(parameter table) ", or" declare function name lib "library name" [Alias' alias "[(parameter table) ] AS type. For example: Public Declare Function Outport Lib "vcDll.dll" (ByVal portid As Integer, ByVal value As Integer) As Integer // IO Output Function Public Declare Function Inport Lib "vcDll.dll" (ByVal portid As Integer) As Integer // The IO input function is defined after the program can use the function or procedure of the above-defined function or procedure in the program to operate the I / O port. Fourth, Visual Basic Medium Action The I / O port operation described above is a signal from the I / O port, and is often a signal acquisition or operation in the industrial control field. Therefore, we need to separate the signals read once when the signal is read, and other bit shields that do not need to be output when the output signal is output. 1. Separate read I / O port signals (need to read D0 bits) IOVALUE = Inport (Base) // First read the base address I / O port signal Iovaluer = Iovalue and & 1H // (shielded out other 7 bits, Leave only D0 bits) 2. Shield other bit signals and output the corresponding signal (D0 position 1) IOVALUE = inport (base) // First read the base address I / O port signal IOVALUE = IOVALUE OR & 01H / / (B00000001, blocking the other 7 bits, will D0 Location 1) Call Outport (Base, Iovalue) // Output The corresponding signal is in the above operation, we use the logical operator and or OR, the logic analysis table is as follows. And 1 01 1 00 0 0OR 1 01 1 10 1 0 Use the AND operator to specify the specified position 0, for example: sixteen-entered & h84's binary: 10000100, its third bit is 1, if you want to The position is 0, and the other bit is unchanged, and the binary value of the & H80 and the B10000000 can be used. The second bit of the specified position 1, for example: hexadecimal & H84 is 0, if the second position is 1, and other bit constant, can be used: & h84 or & h02 = & h86, & h86 The binary value is B10000110. 5. Using computer LPT print ports Make I / O port printheads with 3 ports: Data port, status port, and control port, commonly used print port is LPT1, its 3 ports of the 3 ports: 378h, 379h and 37ah .

Among them, 378h and 37ah readable can be written, and 379h is the only port of the read-only port computer 25 pin printing ports are defined as shown below. The related pictures of this topic are as follows: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 378h as a data output. Type instruction -O 378 00 (01, 02, 04, 08, 10, 20, 40, 80, FF), while detecting a voltage of 2 to 9 foots with a multimeter (25 feet), can be measured separately The corresponding position is compliant with the input data ("1" corresponding to 5V, "0" corresponds to 0V). Note 8 bits of this address can be used as eight output lines. 379h as an input port. In order to provide input levels, we will introduce an external 5V power supply. The power floor is connected to the 25th foot of the printer, and then, after 10, 11, 12, 13, 15 pins, 5V, 0V, and execute the -i 379 instruction in the DEBUG environment, read the data of this port. The data read should be in line with the input signal. 37ah port can only be done. The detection method is the same as 378h. The detection result is:  378 output levels and command signals, D0 ~ D8 bits can be a port output bit;  37A output level and instruction signals, D0 ~ D3 bits can be a port output bit;  379 DHEN In state: 11 pins are "0", 10, 12, 13, 15 is "1", so the port input bit is performed using D3 to D6. In summary, there is usually 12 output positions and 4 input bits for the printhead to do I / O ports. 6. Windows 2000 / XP / NT I / O Port Operation Windows 2000 / XP / NT The hardware implements a mask, which strictly controls the I / O port, and many user applications running normally under Windows 9X. Direct access hardware in Windows 2000 / XP / NT will cause system exception, so I / O ports cannot be accessed directly under Windows 2000 / XP / NT. There are usually two ways to solve the problem of I / O port operations under Windows 2000 / XP / NT, one is to write a virtual device driver (Virtual X device, VSD) running in Ring0 (device-driven driver level), it can If you are not restricted, you can capture I / O port operations and memory access, or even interception of hardware interrupts can be intercepted, so you can implement access operations of the I / O port under Windows 2000 / XP / NT in the User mode. Another method is to modify the I / O allowed bitmap setting, allowing a specific task to access a specific I / O port. This allows the User-mode program to allow bitmap settings in RING3 (application level) to allow bitmap settings, unrestricted access to I / O ports. Next, this article describes two methods, WinIO, and PortTalk that implements I / O port operations in Windows 2000 / XP / NT. Winio Library By using a core mode device drive and several other underlying programming techniques, the WinDoWD system has a protective mechanism of the Windows system, allowing the application to I / O ports and physical memory directly under Windows 2000 / XP / NT. Operation. It should be noted that in Windows 2000 / XP / NT, the Winio Library only allows applications with administrator privileges. The WinIO library contains the source code of the corresponding driver and the Visual Basic routine, the download address is: http://www.internals.com/utilities/winio.zip.

转载请注明原文地址:https://www.9cbs.com/read-88649.html

New Post(0)