NO

Author Topic: InpOut32 for PellesC  (Read 36069 times)

tiwag

  • Guest
InpOut32 for PellesC
« on: April 16, 2005, 10:41:26 AM »
There recently was a thread regarding direct hardware port access
http://smorgasbordet.com/phpBB2/viewtopic.php?t=494

and some proposed the WinIO library as solution.
I also have done several projects accessing hardware ports directly and
i found the InpOut32 library more useful than the WinIO stuff.

Download the Inpout32 original files from http://www.logix4u.net
there you get also the source code for all drivers and libraries

This library is incredible, under XP it works not only with administrator rights,
it works also when logged on as a normal user wihout any special permissions.

If someone is interested, in the attachement you can find the PellesC
sample projects ready to use InpOut32, one project (test) is from the original supplied samples,
the other version (testlib) uses an import library and header file.

try it out it's really esay to use, when using the import library version.

--tiwag

drakoh

  • Guest
InpOut32 for PellesC
« Reply #1 on: April 17, 2005, 03:34:59 AM »
nice ! this could be pretty useful, thx !

sven

  • Guest
InpOut32
« Reply #2 on: April 22, 2005, 04:44:04 PM »
Hi tiwag,
I have tested InpOut32 and it is really very easy to handle. The bad news is that it doesn't seem to work on serial ports. Is that a fact or am I doing something wrong? Best regards

tiwag

  • Guest
InpOut32 for PellesC
« Reply #3 on: April 23, 2005, 07:40:43 AM »
Hi sven

as far as i have tested and know InpOut32 works with all known
IO mapped hardware registers and addresses,
i think you should be able to access the uart registers in the same way
as you access the parallel port registers, but i haven't tried this.

anyhow, the serial port is much more convenient to handle
with win api function calls ... see the following sample

Code: [Select]

/********************************************************************/
/*                                                                  */
/*  Terminal.cpp: Sample UART Loopback Terminal Program             */
/*                                                                  */
/*  This is a sample terminal emulator for the UART based serial    */
/*  communications card. Please view the file abstract.txt for      */
/*  more information.                                               */
/*                                                                  */
/*  (c)Copyright Sealevel Systems, Inc., 1999                       */
/*                                                                  */
/*  SEALEVEL SYSTEMS INCORPORATED.                                  */
/*  155 Technology Place                                            */
/*  P.O. Box 830                                                    */
/*  Liberty, SC 29657  USA                                          */
/*  (864) 843-4343                                                  */
/*  (864) 843-3067 FAX                                              */
/*                                                                  */
/********************************************************************/


#include <windows.h>
#include <stdio.h>
#include <conio.h>

#define TIMEOUT_CONSTANT    50
#define ESC                 27


int main(int argc, char* argv[])
{
    int          key_pressed   = 0;
    char         outchar       = 0;
    char         inchar        = 0;
    DWORD        bytes_written = 0;    // Number of bytes written to port
    DWORD        bytes_read    = 0;    // Number of bytes read from port
    COMMTIMEOUTS tempComTimeouts;      // Our temporary time-outs for COM1
    COMMTIMEOUTS savedComTimeouts;     // Stores the original time-outs
    HANDLE       comport       = NULL; // Handle for COM port
    DCB          comSettings;          // Contains various port settings

    printf("Sample UART Loopback Terminal Program v1.00.\n");
    printf("(c)Copyright Sealevel Systems, Inc., 1999.\n\n");
   
    // Open COM port
    if ((comport =
         CreateFile("COM1",
                    GENERIC_READ | GENERIC_WRITE, // for reading and writing
                    0,                            // exclusive access
                    NULL,                         // no security attributes
                    OPEN_EXISTING,              
                    FILE_ATTRIBUTE_NORMAL,
                    NULL)) == INVALID_HANDLE_VALUE)
    {
        printf("Unable to open COM1.\n\n");
        printf("Press any key to exit.");
        getch();
        return(1);
    }

    printf("COM1 opened.\n\n");

    // Save time-out parameters for COM1
    GetCommTimeouts(comport,&savedComTimeouts);
   
    // Set our time-outs
    tempComTimeouts.ReadIntervalTimeout         = TIMEOUT_CONSTANT;
    tempComTimeouts.ReadTotalTimeoutMultiplier  = TIMEOUT_CONSTANT;
    tempComTimeouts.ReadTotalTimeoutConstant    = TIMEOUT_CONSTANT;
    tempComTimeouts.WriteTotalTimeoutMultiplier = TIMEOUT_CONSTANT;
    tempComTimeouts.WriteTotalTimeoutConstant   = TIMEOUT_CONSTANT;
    SetCommTimeouts(comport,&tempComTimeouts);

    // Set Port parameters.
    // We make a call to GetCommState() first in order to fill
    // the comSettings structure with all the necessary values.
    // Then we change the ones we want and call SetCommState().
    GetCommState(comport, &comSettings);
    comSettings.BaudRate = 9600;
    comSettings.StopBits = ONESTOPBIT;
    comSettings.ByteSize = 8;
    comSettings.Parity   = NOPARITY;
    comSettings.fParity  = FALSE;
    SetCommState(comport, &comSettings);
   
 
    printf("Ready to send/receive data. Hit ESC to exit.\n\n");
           
    while(key_pressed != ESC)
    {
        if (kbhit())
        {
            key_pressed = getch();
            outchar = (char)key_pressed;

            if (key_pressed != ESC)
            {
                // Send data. if succesful, WriteFile() returns non-zero
                WriteFile(comport,        // Handle
                          &outchar,       // Outgoing data
                          1,              // Number of bytes to write
                          &bytes_written, // Number of bytes written
                          NULL);
            }
        }
       
        // Read data. if succesful, ReadFile() returns non-zero
        ReadFile(comport,     // Handle
                 &inchar,     // Incoming data
                 1,           // Number of bytes to read
                 &bytes_read, // Number of bytes read
                 NULL);
           
        if (bytes_read == 1)
            if (inchar == 13)
                printf("\n");
            else
                printf("%c", inchar);
    }

    // Restore time-out parameters
    SetCommTimeouts(comport,&savedComTimeouts);
    CloseHandle(comport);
   
    printf("\n");

    return(0);

}


-tiwag

Timppa

  • Guest
InpOut32 for PellesC
« Reply #4 on: April 23, 2005, 09:42:17 AM »
InpOut32 is fine for reading/writing ports,
but it uses DeviceIoControl API for that in NT ( not _inp()/_out() ).

But reading memory with that  isn't supported, I think.

That's why in Andrew's case, it's needed something like WinIO.

tiwag

  • Guest
InpOut32 for PellesC
« Reply #5 on: April 23, 2005, 09:54:27 AM »
Quote from: "Timppa"
InpOut32 is fine for reading/writing ports,
...


exactly, thats what i've done with InpOut32,
access to user-defined hardware by means of
IO mapped registers (ports) ...

tiwag

  • Guest
InpOut32 for PellesC
« Reply #6 on: April 23, 2005, 09:59:00 AM »
Quote from: "Timppa"
...That's why in Andrew's case, it's needed something like WinIO.


i don't think so,

in respect of Andrew's first posting, he want's to access
the IO mapped ports (registers) of a parallel port.

Quote
Where these registers are usually ?
           In an IBM PC, these registers are IO mapped and will have unique address. We have to find these addresses to to work with parallel port. For a typical PC , the base address of LPT1 is 0x378 and of LPT2 is 0x278. The data register resides at this base address , status register at baseaddress + 1 and the control register is at baseaddress + 2. So once we have the base address , we can calculate the address of each registers in this manner. The table below shows the register addresses of LPT1 and LPT2


Register LPT1 LPT2
data registar(baseaddress + 0) 0x378 0x278
status register (baseaddress + 1) 0x379 0x279
control register (baseaddress + 2) 0x37a 0x27a


and this can be done with InpOut32 with ease
see samples

but i know what you mean, you can't
get the base address of the IO port with this functions.

InpOut32 two functions are identically with
WinIO's functions GetPortVal and SetPortVal

but WinIO additionally has functions to
access direct hardware memory addresses too.

sven

  • Guest
Port read/write
« Reply #7 on: April 27, 2005, 02:43:15 PM »
hi guys,
I have tested WinIo and inpout32 and both ways seems to work nicely but I still can't get any reaction from my com ports registers, they read FF no matter what I try to write . Has anybody any idea what's wrong? I also tried the UART loopback program but the compiler broke down, don't I have the rignt windows library ? I am greatful for any suggestions.
Sven

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
InpOut32 for PellesC
« Reply #8 on: April 27, 2005, 03:24:15 PM »
Loopback program Terminal, as PellesC project
May the source be with you

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: Port read/write
« Reply #9 on: April 27, 2005, 04:02:25 PM »
Quote from: "sven"
I also tried the UART loopback program but the compiler broke down, don't I have the rignt windows library ?

Can you explain "broke down"?!

Pelle
/Pelle

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
InpOut32 for PellesC
« Reply #10 on: April 28, 2005, 01:16:38 PM »
Hi Sven,
In my experience if you read 0xFF from an I/O this simply means that there is no device there!
I'm sure you are not trying to access serial ports on an USB dock, but on some PC there are new serial devices that are not legacy compliant (means that they don't respect the standard I/O mapping and/or registers and programming). So I would suggest you to check that the device you are trying to access is a classic one (why don't boot old glorious DOS and check with a standard program?)  :wink: .
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

sven

  • Guest
Serial interface
« Reply #11 on: April 29, 2005, 07:50:41 AM »
Hello Pelle,
So sorry for my sloppy language, the compiler didn't break down ofcourse,
it only reached more than 100 warnings and faults and stopped.
Later I tried to compile the UART loopback program with the lcc compiler and it worked fine so I guess it has to do with libraries, question is how ? Anyway, now I am able to manipulate the comSettings with the SetCommState command and the next step is to find out the structure of the CommMask which is what I really wanted to utilize.
But still I am curious about why I can't access the  serial port regs with _imp/_out
Best regards Sven

tiwag

  • Guest
Re: Serial interface
« Reply #12 on: April 29, 2005, 08:07:16 AM »
Quote from: "sven"

... it only reached more than 100 warnings and faults and stopped...


rule of thumb for this behaviour :
check compiler option "Enable Micro$oft Extensions"   -->   -Ze


ad uart registers:
of course one can access these register with inpout,
but to do something meaningful with it is a pain,
beside reading the hardware manual for the actually implemented uart on your mainboard, you have to regard 25+ other important things,
you have to download 5+ versions (if available) to find the last bug fixed versions of the datasheet and application notes documents, and so on ...

use the winAPI instead and don't reinvent the wheel.


inpout is mainly used to access hardware, which is user-defined built
and connected to your pci bus and has IO-mapped registers to control this hardware. i do such things in my office and it works really !

-tiwag

Timppa

  • Guest
InpOut32 for PellesC
« Reply #13 on: April 29, 2005, 10:00:22 AM »
Quote
I guess it has to do with libraries, question is how ?

Check compiler option "Define compatibility name" -> Go

sven

  • Guest
Re: Serial interface
« Reply #14 on: April 29, 2005, 07:36:22 PM »
Quote from: "tiwag"
Quote from: "sven"

... it only reached more than 100 warnings and faults and stopped...


rule of thumb for this behaviour :
check compiler option "Enable Micro$oft Extensions"   -->   -Ze


-tiwag


of course, typical beginner's misstake. I downloded Pelle's latest version the other day and forgot to tick those boxes. Now it works " som ett spjut "  - like a spear. Thank you. Now my next step in this daVinci puzzle is to find out how to handle the CommMask. Where can I find info about that ?