Announcement

Collapse
No announcement yet.

Port IO problem

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Port IO problem

    Windows NT was designed so that (theoretically) no application could crash the operating system. Part of that security model is that NT will not allow any user-mode (Ring 3) application to access hardware ports. For this reason, INP and OUT was removed from the PowerBASIC language in PB/DLL 32-bit and PB/CC.
    However, Windows 95/98 is not a true 32-bit operating system and does not support the NT security model. So technically, an application *can* directly access a hardware port (although we highly recommend that you NOT do it). To do so, you simply use the "in" and "out" assembler instructions.
    The following code encapsulates this for those of you not familiar with assembler:

    SUB pbOut(BYVAL port AS INTEGER, BYVAL value AS INTEGER)
    ! mov AX, value
    ! mov DX, port
    ! out DX, AL
    END SUB

    FUNCTION pbInp(BYVAL port AS INTEGER) AS INTEGER
    ! mov DX, port
    ! in AL, DX
    ! mov FUNCTION[0], AL
    END FUNCTION

    I have read this message and try to use the function,
    everything fine when compiling, but when I run it
    the windows illegal operation window come-up, I am
    using Windows 98 SE.
    What's wrong, anybody can help.

  • #2
    It is unlikely anyone is going to be able to suggest a solution, because you have not posted enough information on the problem.

    If you single-step your program in the debugger, where *exactly* does the Illegal Operation occur? In otherwords, how do we know that it is occuring within these functions? (ie, you may have a problems elsewhere in your code that is causing this problem).

    What port(s) are you trying to access? Does this code fail on the 1st attempt, or do they work for a while?

    What exactly does your program do?

    Also, which version of PB/DLL are you using?

    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]ic.com</A>
    Lance
    mailto:[email protected]

    Comment


    • #3
      I am using Pbdll6 on P3-500 machine with Windows 98 Second edition OS, below are the source code for my control program,
      using ISA bus, port address 608(Decimal),this program was transfer from VB6(working!) and modify to suit PBDll6, it compile,but have run time trouble with illegal call.
      Code:
      '================================================================
      'Motion control by 8255
      '
      '
      '================================================================
      ' Set ID for all the dialog control items
      
      #COMPILE EXE
      #INCLUDE "Win32Api.Inc"
      
      %IDMOVE_X_MINUS          = 1
      %IDMOVE_X_PLUS           = 2
      %IDEXIT                  = 3
      %BS_DEFAULT              = 1      'Set the button appearance
      
      '------------------------------------------------------------
      ' Global variable to recieve the user input and variable
      
      GLOBAL PORTBASE AS INTEGER
                                                        
      '-------------------------------------------------------------
      'Routine for outport data to port
      
      SUB pbOut(BYVAL oport AS INTEGER, BYVAL value AS INTEGER)
        '! mov DX, oport
        '! mov AL, value
        '! out DX, AL
      
        ! mov AX, value
        ! mov DX, oport
        ! out DX, AL
      END SUB
      
      '------------------------------------------------------------
      'Routine for replace doevents in VB
      SUB DOEVENT() EXPORT
         STATIC Msg AS tagMsg
         IF PeekMessage(Msg, %NULL, 0, 0, %PM_REMOVE) THEN
            TranslateMessage Msg
            DispatchMessage Msg
            END IF
            END SUB
      '----------------------------------------------------------------
      'Routine for delay timing
      SUB Delay(DelayInMilliseconds AS SINGLE)
      DIM tmout AS SINGLE
      tmout = DelayInMilliseconds + timeGetTime()
      DO UNTIL timeGetTime() >= tmout
      CALL DOEVENT
      LOOP
      END SUB
      
      '----------------------------------------------------------------
      'Routine for Pulsing the X-Axis motor in clockwise direction
      SUB Move_X_minu(STEPP AS SINGLE)
      
      DIM X AS INTEGER
          FOR X = 1 TO STEPP
      
          CALL pbOut(PORTBASE+1,3)           'turn on portB bit 2
      
          'now bring control port bit 2 to high and low
          'to latch the data to it's output.
      
          CALL pbOut(PORTBASE+2,2)
          CALL pbOut(PORTBASE+2,0)
      
              Delay(1)                   'off time if current sinking
      
                 CALL pbOut(PORTBASE+1,2)      'turn off portB bit 2
      
      
                 CALL pbOut(PORTBASE+2,2)       'but keep bit 1 low
                 CALL pbOut(PORTBASE+2,0)
      
              Delay (1)                 'on time if current sinking
      
      NEXT X
      END SUB
      '--------------------------------------------------------------
      'Routine for pulsing the x-axis motor anti-clockwise
      SUB Move_X_PLUS(STEPP AS SINGLE)
      
      DIM X AS INTEGER
      
          FOR X = 1 TO STEPP
      
          CALL    pbOut(PORTBASE+1,3)        'turn on portB bit 2
      
          'now bring control port bit 2 to high and low
          'to latch the data to it's output.
      
          CALL pbOut(PORTBASE+2,2)
          CALL pbOut(PORTBASE+2,0)
      
              Delay(1)               'off time if current sinking
      
                  CALL pbOut(PORTBASE+1,1)     'turn off portB bit 2
                  CALL pbOut(PORTBASE+2,2)     'but keep bit 1 low
                  CALL pbOut(PORTBASE+2,0)
      
              Delay (1)                 'on time if current sinking
      
      NEXT X
      END SUB
      
      '----------------------------------------------------------------
      CALLBACK FUNCTION MoveXminus()
      
      Move_X_minu (200)
      
      END FUNCTION
      
      '--------------------------------------------------------
      CALLBACK FUNCTION MoveXplus()
      
      Move_X_plus (200)
      
      END FUNCTION
      
      '--------------------------------------------------------
      CALLBACK FUNCTION Exit_prog()
      
        DIALOG END CBHNDL, 0
      
      END FUNCTION
      
      '--------------------------------------------------------------
      
      FUNCTION PBMAIN () AS LONG
      
        $REGISTER NONE
      
        LOCAL hDlg   AS LONG
      
      ' ** Create a new dialog template
        DIALOG NEW 0, "Motion of X-Y Table", ,, 300, 200, 0, 0 TO hDlg
      
      ' ** Add controls to it
      '-------------------------------------------------------------
      CONTROL ADD BUTTON, _
      hDlg, _
      %IDMOVE_X_MINUS, _
      "MOVE X-", _
      100, _
      10, _
      50, _
      12, _
      CALL MOVE_X_MINU
      
      '--------------------------------------------------------------
      CONTROL ADD BUTTON, _
      hDlg, _
      %IDMOVE_X_PLUS, _
      "MOVE X+", _
      100, _
      30, _
      50, _
      12, _
      CALL MOVE_X_PLUS
      
      '--------------------------------------------------------------
      
      CONTROL ADD BUTTON, hDlg, %IDEXIT, "Exit", _
      110, 75, 40, 14, 0 CALL exit_prog
      
      '------------------------------------------------------------
          
          PORTBASE=608                      'address IN ISA bus
          CALL pbOut(PORTBASE+3,128)
      '8255 control word,send data 128 to port 611 
      'for set all port are output
      
                                              
      ' ** Display the dialog
        DIALOG SHOW MODAL hDlg
      
      
      END FUNCTION

      Comment


      • #4
        I have had no problems with 95/98 with the following:
        '------------------------------------------------------------------------------------------------
        ' TITLE: Out32
        ' DESC: Writes data (ByteValue)to I/O port (PortAddress).
        ' SYNTAX: Out32 PortAddress, ByteValue
        ' NOTES: This procedure is for use with I/O ports using 8 bit data ie. ISA expansion cards.
        ' This procedure may also be used to write data to SPP mode parallel ports

        DECLARE SUB Out32(BYVAL PortAddress AS INTEGER,BYVAL ByteValue AS BYTE)

        SUB Out32(BYVAL PortAddress AS INTEGER,BYVAL ByteValue AS BYTE)

        ! push dx
        ! mov dx,PortAddress
        ! mov al,ByteValue
        ! out dx,al
        ! pop dx

        END SUB


        '------------------------------------------------------------------------------------------------
        ' TITLE: In32
        ' DESC: Returns data from I/O port (PortAddress).
        ' SYNTAX: In32 PortAddress TO Variable?
        ' NOTES: This procedure is for use with I/O ports using 8 bit data ie. ISA expansion cards.
        ' This procedure may also be used to return the last data written to a SPP mode parallel
        ' port. It will NOT work with EPP mode parallel ports.


        DECLARE FUNCTION In32(BYVAL PortAddress AS INTEGER) AS BYTE

        FUNCTION In32(BYVAL PortAddress AS INTEGER) AS BYTE
        LOCAL ByteValue AS BYTE

        ! push dx
        ! mov dx,PortAddress
        ! in al,dx
        ! mov ByteValue,al
        ! pop dx

        FUNCTION = ByteValue
        END FUNCTION

        ------------------

        Comment

        Working...
        X