Serial port read..can someone tell me..

This is a discussion on Serial port read..can someone tell me.. within the C Programming forums, part of the General Programming Boards category; Below I have pasted my program. The purpose of the program is to read data from a serial port (in ...

  1. #1
    Unregistered
    Guest

    Unhappy Serial port read..can someone tell me..

    Below I have pasted my program. The purpose of the program is to read data from a serial port (in this case COM2) and print it out to the screen. I can't seem to get it to print to the screen. I don't know if the port is really configured properly. Can someone out there take a look at my code and run it to see what the problem is? Thnx in advance! Rachael

    #include <windows.h>
    #include <string.h>
    #include <memory.h>
    #include <commdlg.h>
    #include <io.h>
    #include <stdio.h>
    #include <stdlib.h>

    #define MAX 130

    // Flow control flaags
    #define FC_DTRDSR 0x01
    #define FC_RTSCTS 0x02
    #define FC_XONXOFF 0x04

    char port[5] = "COM2:" ; /* Comm port name. */
    char msg[MAX] ; /* Message received from port. */
    int n_msg = 0 ; /* Counts bytes to read. */
    int n_in ; /* Counts bytes read. */
    int ok ; /* Return code from function calls. */
    HANDLE hPort ; /* Handle to IO port object. */
    DCB dcb ;
    COMMTIMEOUTS CommTimeOuts ;
    BYTE bSet ;


    int main(void) {

    // Open comm port
    hPort = CreateFile(port,
    GENERIC_READ,
    0,
    NULL,
    OPEN_EXISTING,
    0,
    NULL) ;

    if (hPort == INVALID_HANDLE_VALUE) {
    printf("Cannot open IO port (error %d)\n", GetLastError()) ;
    return 1 ;
    }

    // Setup comm port connection
    dcb.DCBlength = sizeof(dcb) ;

    GetCommState(hPort, &dcb) ;

    dcb.BaudRate = 9600 ;
    dcb.ByteSize = 8 ;
    dcb.Parity = 0 ;
    dcb.StopBits = 1 ;

    // setup hardware flow control

    bSet = (BYTE) ((FC_XONXOFF & FC_DTRDSR) != 0) ;
    dcb.fOutxDsrFlow = bSet ;
    if (bSet)
    dcb.fDtrControl = DTR_CONTROL_HANDSHAKE ;
    else
    dcb.fDtrControl = DTR_CONTROL_ENABLE ;

    bSet = (BYTE) ((FC_XONXOFF & FC_RTSCTS) != 0) ;
    dcb.fOutxCtsFlow = bSet ;
    if (bSet)
    dcb.fRtsControl = RTS_CONTROL_HANDSHAKE ;
    else
    dcb.fRtsControl = RTS_CONTROL_ENABLE ;

    // setup software flow control

    bSet = (BYTE) ((FC_XONXOFF & FC_XONXOFF) != 0) ;

    dcb.fInX = dcb.fOutX = bSet ;

    dcb.XonChar = 0x11 ;
    dcb.XoffChar = 0x13 ;
    dcb.XonLim = 100 ;
    dcb.XoffLim = 100 ;
    dcb.fBinary = TRUE ;
    dcb.fParity = TRUE ;

    SetCommState(hPort, &dcb) ;

    // read data from port
    do {
    n_msg = (n_msg <= MAX ? n_msg : MAX) ;
    ok = ReadFile(hPort, msg, n_msg, &n_in, NULL) ;
    printf("%s", msg) ;
    } while(ok) ;

    if (!ok) {
    printf("Cannot read message (error %d)\n", GetLastError()) ;
    return 1 ;
    }

    // Cleanup
    ok = CloseHandle(hPort) ;
    if (!ok) {
    printf("Cannot close port handle (error %d)\n", GetLastError()) ;
    }

    return 0 ;
    }

  2. #2
    Unregistered
    Guest

    Wink

    Is this code so specialized that there are no C and win32 experts out there...if you're out there please look at my code to see why I can't seem to read data from a serial port. Thanks!
    Rachael

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Originally posted by Unregistered
    Is this code so specialized that there are no C and win32 experts out there...if you're out there please look at my code to see why I can't seem to read data from a serial port. Thanks!
    Rachael
    It's not that the problem is difficult............personally I have no way to test your code as the com port on my computer is redundant.......I think there's a small spider living in there at the moment and he's welcome to it! Go USB!!!!


    Oh...and by the way....didnt you post this question a few days ago but under the name "Fred"?

  4. #4
    Unregistered
    Guest

    Wink

    No, but I did use your link to develop this program. Anyway, I rewrote the code based off of information from the MSDN website and fixed the problem. Rachael

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Serial Port Questions
    By valaris in forum Tech Board
    Replies: 2
    Last Post: 05-22-2009, 08:26 AM
  2. FTP program
    By jakemott in forum Linux Programming
    Replies: 14
    Last Post: 10-06-2008, 01:58 PM
  3. Replies: 2
    Last Post: 05-22-2008, 11:43 PM
  4. Serial Port Issues (again!)
    By HalNineThousand in forum Linux Programming
    Replies: 6
    Last Post: 04-09-2008, 08:26 PM
  5. HELP with storing serial port data into txt file
    By inmaterichard in forum C Programming
    Replies: 2
    Last Post: 04-02-2008, 02:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21