• C

port control in watcom c/c++ under QNX

I am trying to modify a program written in Watcom c/c++ under QNX 4. I want to let it communicate with another computer using serial port. So far I have just found functions like outp(), outw() to access the hardware,
but to communicate successfully one needs to specify the Baud rate, number of data bits, etc. Is there any function in Watcom c/c++ to do that? I couldn`t find the version of which I am using, I just know its accompanied manuals
were written in 1993. Thanks in advance
zhao040597Asked:
Who is Participating?
 
claudebConnect With a Mentor Commented:
The following is from some code I wrote under QNX 4.2 for a communications program.  You should be able to get the info you need from this.  Also, take a look at the code for the qtalk program (you can get the source off the QNX ftp site).


int       CommPort;
char      PollPort;

strcpy(PollPort, "/dev/ser3");

/* Open the serial port */
if((CommPort = open(PollPort, O_RDWR)) == -1)
{
  /* Can't open port! */
  printf("Unable to open port '%s'", PollPort);
  exit(0);
}

/* Set device to RAW mode */
if(dev_mode(CommPort, 0, _DEV_MODES) == -1)
{
  /* Can't set RAW mode on port */
  printf("Unable to set RAW mode on port.");
  close(CommPort);
  exit(0);
}

/* Drop DTR */
tcdropline(Port, 2000);
tcflush(Port, TCIOFLUSH);
sleep(2);

/* Read something form port */
int Read_Data(int Chars, int Timeout, unsigned char *buf)
{
  int i, n = 0;
  unsigned char c;

  /* Return number of characters waiting? */
  if(!Chars) return(dev_ischars(CommPort));

  if(Chars > 0)
  {
    while(Chars && (i = dev_read(CommPort, buf, Chars, Chars, 0,       Timeout, 0, 0)))
    {
      if(i == -1)
      {
        perror("Read_Data() get -1!");
        *buf = NAK;
        return(0);
      }
      buf += i;
      Chars -= i;
      n += i;
    }
  }
  else
  {
    /* Read until I get a '\r' or a timeout */
    while((i = dev_read(CommPort, &c, 1, 1, 0, Timeout, 0, 0)))
    {
      if(i == -1)
      {
         perror("Read_Data() got -1");
         *buf = NAK;
         return(0);
      }
      if(c == '\r') return(n);
      *buf = c;
      buf += i;
      n += i;
    }
  }
  return(n);
}

/* Send some data */
int Send_Data(char *Data, int Size, int WaitFor)
{
  int  i;
  unsigned char      c;

  if(Size == 0) Size = strlen(Data);
  /* Send data to port */
  write(CommPort, Data, Size);

  /* Do I need to wait for something? */
  if(WaitFor == NULL) return(TRUE);
  do
  {
    i = Read_Data(1, 100, &c);
  } while(i != 0 && c != WaitFor && c != NAK);
  return(c == WaitFor ? TRUE : FALSE);
}

0
 
icdCommented:
Specifying baud rate etc is a function of the serial device. This device can be programmed by writing to its output control ports using outp() and outw(). All you need to know is the port number and the specification for the device.


0
 
zhao040597Author Commented:
Thanks for your answer.

But first, is this written in Watcom c/c++?
Second, what header file should I include?
Third, the serial ports of the two computers are 9-pin, and I connected them in this way:
        a            b
       2(RD)-----------3(TD)
       3(TD)-----------2(RD)
       5(Ground)-------5(Ground)
       4(DTR)          4(DTR)
       |               |
       |               |
       6(DSR)          6(DSR)
       7(RTS)          7(RTS)
       |               |
       |               |
       8(CTS)          8(CTS)

In this way, I have no problem to use Qbasic to communicate with each other(send and receive), but do you think your program will work in this way of connection? (Both computers are PC)

Since I just have some basic knowledge of c and totally unfamiliar with QNX, I want to make sure it should works before I look through a whole bunch of manuals.

Thank you.
0
 
claudebCommented:
Yes, it was written under QNX using Watcom C 10.6.  The header files you'll need are:

#include <sys/dev.h>
#include <sys/kernel.h>
#include <sys/types.h>
#include <sys/qioctl.h>

I think those should do.  As far as your cable goes, it will work ONLY if you have XON/XOFF flow control.  You need a proper null modem cable which (I think) should be:

2(RX)-----3(TX)
3(TX)-----2(RX)
4(DTR)----6(DSR)
6(DSR)----4(DTR)
7(RTS)----8(CTS)
8(CTS)----7(RTS)
5(GND)----5(GND)

Hope this helps!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.