Solved

port control in watcom c/c++ under QNX

Posted on 1997-05-13
4
553 Views
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
0
Comment
Question by:zhao040597
  • 2
4 Comments
 
LVL 5

Expert Comment

by:icd
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.


0
 

Accepted Solution

by:
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);
  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
 

Author Comment

by:zhao040597
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
       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
 

Expert Comment

by:claudeb
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:

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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

760 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

20 Experts available now in Live!

Get 1:1 Help Now