[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

port control in watcom c/c++ under QNX

Posted on 1997-05-13
4
Medium Priority
?
614 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 270 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 opening and reading files in the C programming language.
Suggested Courses

873 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