Menu

Output Hex byte to parallel port

Help
2006-03-10
2013-04-02
  • Bill Littlejohn

    Bill Littlejohn - 2006-03-10

    I'm working on a project using a parallel port relay board. To control the board I need to simply send a hex byte to the parallel port.
    Anyone know if this is possible directly from Dialect?
    I have a simple exe (and C source) that will do it, but I dont want to pop-up DOS windows every execution.
    I'd rather do it directly.
    Any ideas?
    Thanks.

     
    • A.H. Banen

      A.H. Banen - 2006-03-10

      Bill,
      Take a look at http://www.lvr.com/parport.htm (look for inpout32.dll and go from there; there are several dll's that may work for you).

      Sincerely,
      André

       
      • Bill Littlejohn

        Bill Littlejohn - 2006-03-10

        Thanks André
        I think this will work great.
        I'm really bad at C conventions however, would you mind checking if I've done this correctly below?

        New Dialect DLL call:
        pport = system.dll("user32.dll")
        pport.__loadpfunc__("Inp","Inp32",'int,['int])
        pport.__loadpfunc__("Out","Out32",'int,['int,'int])

        Given VB example:
        Public Declare Function Inp Lib "inpout32.dll" _
        Alias "Inp32" (ByVal PortAddress As Integer) As Integer
        Public Declare Sub Out Lib "inpout32.dll" _
        Alias "Out32" (ByVal PortAddress As Integer, ByVal Value As Integer)

        Maybe I'm thinking about this all wrong, but why are the declarations of type Integer when it takes a hex argument?
        Out PortAddress, ValueToWrite
           Example: Out &h378, &h55

         
        • George Harth

          George Harth - 2006-03-10

          I believe the ByVal in the VB means that you need to pass by reference.  So for the args you'd want 'intp instead of 'int.  The Out32 function doesn't have a return value, but you declared an 'int return value.

          Also, you should probably use __loadcfunc__ instead of __loadpfunc__.  Most regular DLL's are compiled to the C function calling convention.  The exception is the Win32 API DLLs which use the Pascal calling convention.  If you use the wrong one, you will corrupt the stack and crash.

          Cheers... George

           
    • Bill Littlejohn

      Bill Littlejohn - 2006-03-10

      Oops.
      Should have been
      pport = system.dll("inpout32.dll")

       
    • George Harth

      George Harth - 2006-03-10

      Wait, wait... I'm a fool.  ByRef in VB means by reference.  Ignore my ByVal comment.  Doh.

      Cheers... George

       
    • A.H. Banen

      A.H. Banen - 2006-03-10

      Bill.
      Your (corrected) conversion of the VB code looks fine to me.

      Please note that Dialect has no unsigned integer but it overcomes this problem but allowing an - uinsigned! - hex format for the signed integer range by using the format 0x<hex number>.
      This allows the mapping of unsigned hex numbers on the signed integer range; after all it are only the bits tat matter.
      Notice that you always can use format to convert the integer to a hex number, e.g.
      a = 0xFF
      print a, cr
      print format("%0x", a), cr
      print format("%0X", a), cr

      resp.
      a = 0xFFFFFF80
      print a, cr
      print format("%0x", a), cr
      print format("%0X", a), cr

      You can call the example "Out &h378, &h55"
      in Dialect as:
      retval = pport.Out( 0x375, 0x55 )

      Have fun!
      Sincerely,
      André

       
    • Bill Littlejohn

      Bill Littlejohn - 2006-03-10

      Thanks George and André !

      I'll do some testing with my board tonight.
      I don't actually need the Input since my board handles output only. If your curious, http://electronickits.com/kit/complete/elec/ck1601.htm

      I've got this for the DLL call.
      Thanks again.

      pport = system.dll("inpout32.dll")
      pport.__loadcfunc__("Out","Out32",'void,['int,'int])

      Usage: retval = pport.Out( 0x375, 0x55 )

       
    • Bill Littlejohn

      Bill Littlejohn - 2006-03-11

      In the interest of Thread completion...

      Here's the test code I came up with.
      Hopefully the board won't screw it up too badly.
      The inpout.dll needs to be in the program directory.
      It works great with this kit: http://electronickits.com/kit/complete/elec/ck1601.htm

      Dialect source:

      import "system" as * ; import "gui"

      pport = dll("inpout32.dll")
      pport.__loadpfunc__("Out","Out32",'void,['int,'int])

      rstate = 0

      r1 = 1
      r2 = 2
      r3 = 4
      r4 = 8
      r5 = 16
      r6 = 32
      r7 = 64
      r8 = 128

      w = gui.window("Relay Control",[0,0,100,200])
      ck1 = gui.checkbox(w,"Relay 1",[10,10,100,20])
      ck2 = gui.checkbox(w,"Relay 2",[10,25,100,35])
      ck3 = gui.checkbox(w,"Relay 3",[10,40,100,50])
      ck4 = gui.checkbox(w,"Relay 4",[10,55,100,65])
      ck5 = gui.checkbox(w,"Relay 5",[10,70,100,80])
      ck6 = gui.checkbox(w,"Relay 6",[10,85,100,95])
      ck7 = gui.checkbox(w,"Relay 7",[10,100,100,110])
      ck8 = gui.checkbox(w,"Relay 8",[10,115,100,125])
      cko = gui.checkbox(w,"All Off",[10,130,100,140])
      ckc = gui.checkbox(w,"Cycle All",[10,145,100,155])

      ck1.onclick = func()
          If ck1.checked then
              rstate = rstate + r1
          Else
              rstate = rstate - r1
          Endif
          operate()
          endfunc
      ck2.onclick = func()
          If ck2.checked then
              rstate = rstate + r2
          Else
              rstate = rstate - r2
          Endif
          operate()
          endfunc
      ck3.onclick = func()
          If ck3.checked then
              rstate = rstate + r3
          Else
              rstate = rstate - r3
          Endif
          operate()
          endfunc
      ck4.onclick = func()
          If ck4.checked then
              rstate = rstate + r4
          Else
              rstate = rstate - r4
          Endif
          operate()
          endfunc
      ck5.onclick = func()
          If ck5.checked then
              rstate = rstate + r5
          Else
              rstate = rstate - r5
          Endif
          operate()
          endfunc
      ck6.onclick = func()
          If ck6.checked then
              rstate = rstate + r6
          Else
              rstate = rstate - r6
          Endif
          operate()
          endfunc
      ck7.onclick = func()
          If ck7.checked then
              rstate = rstate + r7
          Else
              rstate = rstate - r7
          Endif
          operate()
          endfunc
      ck8.onclick = func()
          If ck8.checked then
              rstate = rstate + r8
          Else
              rstate = rstate - r8
          Endif
          operate()
          endfunc
      cko.onclick = func()
          ck1.checked = false
          ck2.checked = false
          ck3.checked = false
          ck4.checked = false
          ck5.checked = false
          ck6.checked = false
          ck7.checked = false
          ck8.checked = false
          cko.checked = false
          rstate = 0
          operate()
          endfunc
      ckc.onclick = func()
          rstate = 0
          rstate = rstate + r1
          operate() ; sleep(300)
          rstate = rstate + r2
          operate() ; sleep(300)
          rstate = rstate + r3
          operate() ; sleep(300)
          rstate = rstate + r4
          operate() ; sleep(300)
          rstate = rstate + r5
          operate() ; sleep(300)
          rstate = rstate + r6
          operate() ; sleep(300)
          rstate = rstate + r7
          operate() ; sleep(300)
          rstate = rstate + r8
          operate() ; sleep(300)
          rstate = 0
          operate()
          ckc.checked = false
          endfunc

      operate = func()
          Try
              retval = pport.Out( 0x378, eval( "0x"~str$(format("%X",rstate) ) ) )
          Catch *, e
              print "Exception: "~e, cr
          Endtry
          endfunc

      w.onclose = func()
          rstate = 0
          operate()
          return true
          endfunc

      operate()
      gui.enter()

       

Log in to post a comment.

MongoDB Logo MongoDB