I need to write a small program which collects (20-36 string) data from several com (serial ports) and sends them out on one. For example com2,3,4 to com1.
It should be on Windows Xp workstation. (It is possible that I will do it later on linux, it seems to me simpler to use /dev/ttyS0, but my boss prefers MS)

I find several several examples where I can define my own classes, header files for opening serial port.
I tried the attached one. It defines a CSerialPort class for functions to open and read from serial port.
There is also a header file cserial.h. (attachements)
There is a note:
I am using DllImport to import the kernel32.dll for this header file.

I tried to use the attached cserial.h header file. But I get several errors while compile:
expected constructor destructor before class
expected , or ; before class

... more in serial.jpg attached

Note:
Maybe I am trying to use those source codes totally the wrong way, sorry if so.
Any suggestion that can align me in to the right direction will help.
If somebody knows better example codes for serial programming (for WinXp, DevC++), please post it for me.

Recommended Answers

All 4 Replies

I find a simpler one:

#include <stdio.h>
#include <dos.h>

void main(void)
{
 unsigned int far *ptraddr;  /* Pointer to location of Port Addresses */
 unsigned int address;       /* Address of Port */
 int a;

 ptraddr=(unsigned int far *)0x00000400;

 for (a = 0; a <  4; a++)
   {
    address = *ptraddr;
    if (address == 0)
                printf("No port found for COM%d \n",a+1);
    else
                printf("Address assigned to COM%d is %Xh\n",a+1,address);
    *ptraddr++;
   }
}

But I have troubles with the far variable.

It seems to me that this is a direct access to the serial port for example on address 3F8.
What is this usigned int far *ptraddr?

You have to be very careful about the code you get off the net because a lot of it is written for ancient 16-bit compilers on MS-DOS version 6.X and earlier operating systems. You might as well scrap that second code you found because it won't work on modern 32-bit compilers or MS-Windows.

As for the first program you posted, I don't know what compiler/os that was written for because it contains a lot of stuff I've never encountered before. So you might as well scrap that code too.

For MS-Windows, all you need to do is use the communications functions that are described in Communications Resources. You will want to create a thread for each port you want to monitor and do the reading from there.

I don´t want to discard the MS environment, but for now it is easier to me to go on with the "Linux's file descriptors":

/*
     * 'open_port()' - Open serial port 1.
     *
     * Returns the file descriptor on success or -1 on error.
     */

    int
    open_port(void)
    {
      int fd; /* File descriptor for the port */


      fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
      if (fd == -1)
      {
       /*
	* Could not open the port.
	*/

	perror("open_port: Unable to open /dev/ttyS0 - ");
      }
      else
	fcntl(fd, F_SETFL, 0);

// and write(); read(); for data processing

      return (fd);
    }

It is very simple to use the serial port on this OS.

I need to test (generate) the outgoing character strings mainly, for now. (the device wich receives it on the other side is more importan to me). I will search for the trick how to work with serial ports on the MS, but a litle later (when I will have enough time)
This is different on this documentation:
http://www.easysw.com/~mike/serial/serial.html

In this documentation I find really fast what I need.
Thanks, I understand that there are lot of scrap codes on the internet.

MS-Windows is totally different from *nix. Expect to do major rewrites when porting to MS-Windows.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.