C++ WriteFile() why dont I read the correct letter on the Hyperterminal?

This is a discussion on C++ WriteFile() why dont I read the correct letter on the Hyperterminal? within the Windows Programming forums, part of the Platform Specific Boards category; Hi, Im using this code to address my ComPort: Code: #pragma hdrstop #include <condefs.h> #pragma argsused #include <iostream.h> #include <stdio.h> ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    21

    Question C++ WriteFile() why dont I read the correct letter on the Hyperterminal?

    Hi,
    Im using this code to address my ComPort:

    Code:
    #pragma hdrstop
    #include <condefs.h>
    #pragma argsused
    #include <iostream.h>
    #include <stdio.h>
    #include <windows.h>
    
    int main()
    {
     HANDLE HdComm;
     DWORD DwError;
     DCB Dcb;
     unsigned char Msg[128], Data;
    
    
     //int RealNum;
     DWORD RealNum;
        Data='a';
     // Open the COMM port.
     HdComm = CreateFile( "COM1", GENERIC_READ | GENERIC_WRITE, 0,
      NULL, OPEN_EXISTING, 0, NULL );
     if ( HdComm == INVALID_HANDLE_VALUE /*cmaryus*/ ) // If COMM port can't be opened, then ...
     {
        DwError = GetLastError();  // Display error code, and abort.
        FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, 0, DwError, 0, (char *)Msg, 124, 0 );
        printf( "Cannot Open Port COM1 !\nError Code: %d, - %s.\n", DwError, Msg );
    
        return 1;
     }
    
     // Initialize COMM port.
     SetupComm( HdComm, 1024, 1024 );
     GetCommState( HdComm, &Dcb );
     Dcb.BaudRate = BAUD_9600;
     Dcb.Parity = NOPARITY;
     Dcb.ByteSize = 8;
     Dcb.StopBits = ONESTOPBIT;
     SetCommState( HdComm, &Dcb );
        //cout<<Data;
     do {
    //    ReadFile( HdComm, &Data, 1, &RealNum, NULL );    //Recieve the char from COM1
        WriteFile( HdComm, &Data, 1, &RealNum, NULL );    //Write the recieved data to COM1
        printf( "%02x    ", Data );                       //Display the data
    
     } while( Data != 0x1b );                             // Loop until the data is ESC
    
     CloseHandle( HdComm );
     return 0;
    }
    When I check the Com1Port with the other PC`s Hyperterminal I don`t get the a, I get squares.

    Im trying to send to a 8051 P , and would like to know what the ComPort sbuf really puts out.
    What do I have to send for Data and what does the Sbuf turn out?

    Thanks

    Johannes

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    >>I get squares

    sounds like string terminators. Is the data there to be read?

    Try testing the amount read (by looking at the values in dwBytesRead (RealNum) and the return.

    both == 0 is error in read

    (dwBytesRead==0)&&(iReturn!=0) == EOF or no data
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to i correct this code to read the file correctly?
    By funit49 in forum C++ Programming
    Replies: 4
    Last Post: 03-29-2008, 07:56 PM
  2. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  3. Replies: 1
    Last Post: 09-10-2005, 06:02 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM
  5. Help! Can't read decimal number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-07-2001, 02:09 AM

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