C or C++ program to read data from serial port?

C or C++ program to read data from serial port?
hello

i need a c or c++ program to read data from serial port connected to sick pls laser scanner.model is 200-113.

I have tried several programs in vc++,c,c++ but no one is able to read data serial port.

In one program i am getting msg
xxx.exe:MFC42D.dll:0xc000000: Access violation

When debugged i get
No matching symbol found :kernel32.dll
user32.dll
msvcrt60.dll
and so on.
And finally i get the program ends witih 0×0 code.

in another program it is saying bios.h not found

Whiel in anotehr it says could not able to find outportb()

I dont know the problem is.

But i need one program for this special case ie reading data from serial port connected to sick pls 200-114 laser scanner.
Eagerly waiting for answer
.Any help would be greatly appreciated.

Best answer:

i guess both can be used.. there is some library which provides some methode to read data from serial port

Tags: , , , , ,

Under Forum

3 Comments for C or C++ program to read data from serial port?

  • 1. yogesh_baraiya  |  May 6th, 2009 at 1:23 pm

    will have 2 machines hooked up via the COM1 ports.
    1 machine running windows having a hyperterminal session opened and the other machine running Linux having my program running.
    I want to able to type a character into the hyperterminal window and have the character displayed on the Linux machine.

    Im very new to C programming but heres what i have for my program so far, just not sure how to read the input and display it:

    #include /*Standard input/output definitions*/
    #include /*String function definitions*/
    #include /*Unix standard function definitions*/
    #include /*File control definitions*/
    #include /*Error number definitions*/
    #include /*POSIX terminal control definitions*/

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

    fd = open(“/dev/ttyS0″, O_RDWR | O_NOCTTY | O_NDELAY);
    if(fd == 0)
    {
    perror(“open_port: Unable to open /dev/ttyS0 – “);
    }
    else
    fcntl(fd, F_SETFL, 0);

    return (fd);
    }

    /*Configire port*/

    struct termios options;

    /*Get the current options for the port*/

    tcgetattr(fd, &options);

    /*Set the Baud rates to 19200*/

    cfsetispeed(&options, B19200);
    cfsetospeed(&options, B19200);

    /*Enable received and set local mode*/

    options.c_cflag |= (CLOCAL | CREAD);

    /*Set new options for port*/

    tcsetattr(fd, TCSANOW, &options);

    /*Set data bits*/

    options.c_cflag &= ~CSIZE; /*Mask the character size bits*/
    options.c_cflag |= CS8; /*Select 8 data bits*/

    /*Set RAW input*/

    options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);

    /*Set Raw output*/

    options.c_oflag &= ~OPOST;

    /*Set timeout to 1 sec*/

    options.c_cc[VMIN] = 0;
    options.c_cc{VTIME] = 10;

    /*Code to read input goes here*/

    close(fd);

    Can anyone advise on this?

    Thanks

  • 2. irf  |  May 6th, 2009 at 1:49 pm

    Here is one program:
    fogma.co.uk/foggylog/archive/140.html
    also this article should help:
    codeproject.com/system/serial.asp

    You should know what is expected from your input device, though.

  • 3. justme  |  May 6th, 2009 at 2:40 pm

    I have some c++ serial port code that does what you want. Email me and I will send it to you. I have used it for a few apps. and it seems to work just fine.

Leave a Comment for C or C++ program to read data from serial port?

You must be logged in to post a comment.

Trackback this post  |  Subscribe to the comments via RSS Feed


Categories