port control in watcom c/c++ under QNX

Posted on 1997-05-13
Last Modified: 2010-08-05
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
Question by:zhao040597
  • 2

Expert Comment

ID: 1250357
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.


Accepted Solution

claudeb earned 90 total points
ID: 1250358
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);

/* 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.");

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

/* 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;
      buf += i;
      Chars -= i;
      n += i;
    /* 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;
      if(c == '\r') return(n);
      *buf = c;
      buf += i;
      n += i;

/* 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);
    i = Read_Data(1, 100, &c);
  } while(i != 0 && c != WaitFor && c != NAK);
  return(c == WaitFor ? TRUE : FALSE);


Author Comment

ID: 1250359
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
       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.

Expert Comment

ID: 1250360
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:


Hope this helps!

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

830 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