Solved

Digital I/O

Posted on 1998-12-04
7
274 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
ID: 1179222
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
ID: 1179223
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
ID: 1179224
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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 1

Author Comment

by:schmim04
ID: 1179225
Adjusted points to 300
0
 
LVL 86

Expert Comment

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

Expert Comment

by:AlexVirochovsky
ID: 1179227
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
ID: 1179228
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to understand recursion 12 226
Question of running the program built from sample in codeproject website 3 70
computer science syllabus 3 81
Android development question 2 36
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

776 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