Solved

Digital I/O

Posted on 1998-12-04
7
271 Views
Last Modified: 2008-02-01
I'm writing an application using Borland's C++ Builder.  I've used the DCB structure in the past for serial communications, but how do I handle hardware devices such as digital/analog cards?  I'm using a CIO-DIO48 which has 48 digital inputs and a CIO-DAS16 which has 16 digital outputs.  I can easily write a DOS appl using outportb and inportb to send and receive bytes, but these aren't compatable with Builder.
What resources are there that will allow me to read the digital inputs and write to the digital outputs?
0
Comment
Question by:schmim04
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 14

Expert Comment

by:AlexVirochovsky
Comment Utility
Can you use Windows API: OpenComm, WriteComm, ReadComm ? If can , this is solution. I not, you can tray
OpenFile/_lopen to "COM1"/"LPT1" and standart
read/write functions.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Specialized I/O boards usually come with a device driver that makes the hardware accessible through a defined interface...
As you mentioned C++ Builder, i assume your target platform is Win32. There, the device is usually exposed through a driver which is accessible via 'CreateFile()'. After obtaining a file handle through this function call, you'll have to use 'DeviceIoControl()' using special control codes (see the boards' documentation) to read/write data from/to the device.
0
 
LVL 1

Author Comment

by:schmim04
Comment Utility
Thank you for the function you mentioned.  I never saw it before.  However your response doesn't get me anywhere.  The one card has 48 digital inputs and a base address of 300h.  There are 6 8-bit ports on this card which exist at this base address plus some number.  I need to be able to read these ports on an individual basis (if possible), (or does the function just read them all each time?) and I don't see any functions that would allow this.  What if the boards documentation doesn't say which control code to use off the list?  If there is a way, please tell.    
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Author Comment

by:schmim04
Comment Utility
Adjusted points to 300
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Well, is there _no_ driver plus documentation shipped with the board???
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
Comment Utility
Hi. Thid part of My programm test COM1/COM2 and
 read/write in case, that connectionb exists.
 Programm works with Kass(Kupa) and Weight(Mishkal).
 This is only my case, and can be
  szInitCupa or szInitMishkal you must set other,
  by Doc of you controller.
 Alex

static int idComDev[2] ={ -1,-1};      //no!
static DCB  dcb;
static int nPortMishkal = -1;
static int nPortCupa = -1;
#define MAX_LEN_INPUT    81
/*=========================================================================*/
/*  Name       : DefineController                                 */
/*  Parameters : None                                                        */
/*  Return     : None                                             */
/*=========================================================================*/
void DefineController(void)
{
  int err;
  char
    *szInitCupa    = "COM%d:9600, N, 8, 1",
    *szInitMishkal = "COM%d:1200, E, 8, 1";
 char buff[MAX_LEN_INPUT];            //Com Command

  for (int i = 0; i < 2;i++)
    if (idComDev[i] >= 0)            //Post opened?
      {
      CloseComm(idComDev[i]);      //close
      idComDev[i] = -1;            //no!
      }
      //read from ini File port of Weight(default 1)
  UINT bMishkal  = ReadSpsModeInt("Main", "PortMishkal",1);
      //read from ini File port of CASS(default 2)
  UINT bCupa  = ReadSpsModeInt("Main", "PortCupa",2);
  if (bMishkal >= 1 && bMishkal <= 4)
    nPortMishkal = bMishkal;
  if (bCupa >= 1 && bCupa <= 4)
    nPortCupa = bCupa;
  if (nPortCupa == nPortMishkal && nPortMishkal != -1)
    nPortMishkal = -1;                  //error
                              //mishkal
  if ( nPortMishkal > 0)
    {
      wsprintf(buff, "COM%d", nPortMishkal);
      idComDev[0] = OpenComm(buff,MAX_LEN_INPUT , MAX_LEN_OUTPUT);
      if (idComDev[0] >= 0)  //open?
      {
        wsprintf(buff, szInitMishkal,nPortMishkal);
        err = BuildCommDCB(buff, &dcb);
        if (err >= 0)
          err = SetCommState(&dcb);
        if (err < 0)                        //Error?
          {
            CloseComm(idComDev[0]);
            idComDev[0]  = -1;
          }
       }
      if (idComDev[0] >= 0)            //OK?
      TestMishkal(idComDev[0]);
    }
                              //Cupa
  if (nPortCupa > 0)
    {
      wsprintf(buff, "COM%d", nPortCupa);
      idComDev[1] = OpenComm(buff,MAX_LEN_INPUT , MAX_LEN_OUTPUT);
      if (idComDev[1] >= 0)  //open?
      {
        wsprintf(buff, szInitCupa,nPortCupa);
        err = BuildCommDCB(buff, &dcb);
        if (err >= 0)
          err = SetCommState(&dcb);
        if (err < 0)                        //Error?
          {
            CloseComm(idComDev[1]);
            idComDev[1]  = -1;
          }
       }
    }
} // end DefineController
void TestMishkal(int &idComDev)
{                              //send acknoledge
  char buff[MAX_LEN_INPUT];            //Com Command
  memset(buff, 0, sizeof(buff));
  buff[0] = buff[19] = 7;
  int err = WriteComm(idComDev,buff,20);
  if (err >= 0)                  //OK!
    {
      long lTime0,lTime1;

      time(&lTime0); lTime1 = lTime0; // delay
      while(lTime1 < lTime0+2) time(&lTime1);

      memset(buff, 0, sizeof(buff));
                                    //read reaction
      err = ReadComm (idComDev,buff,20);
      if (*buff == 0)
      err = -1;
      else if (err >= 0 && !TestCheckSum(buff))//test: must be from R
      err = -1;
      if (err < 0)             //close Bennet
      {
        CloseComm(idComDev);
        idComDev = -1;      //no!
      }
      if (err >= 0)                  //for next weight
      WriteComm(idComDev,buff,20);
    }
}

0
 
LVL 2

Accepted Solution

by:
ivi earned 300 total points
Comment Utility
It is a standard answer:

1. Use a software shipped with your board
2. Write your own device driver
3. Use a commercial tools named WinRT from Blue Water Systems
4. Use shareware TVicHW32 for Delphi/BCBuilder/VB/C++
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now