Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5845479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:26:26+00:00 2026-05-22T12:26:26+00:00

//#include StdAfx.h #include <stdio.h> #include <windows.h> #include <winbase.h> #include <iostream> #include <tchar.h> using namespace

  • 0
//#include "StdAfx.h"
#include <stdio.h> 
#include <windows.h> 
#include <winbase.h>
#include <iostream>
#include <tchar.h>
using namespace std;

int main()
{

    int com = 'COM2';
    string data = "\n 010400 \n";
    char output[32];
    //unsigned int length = 0;
    DCB config = {0};
    bool abContinue = true;
    DWORD dwBytesWritten;
    DWORD  dwBytesRead;
    int isRead = false;

    HANDLE m_hCommPort = ::CreateFile(L"COM2",
        GENERIC_READ|GENERIC_WRITE,//access ( read and write)

        0,    //(share) 0:cannot share the COM port                        

        0,    //security  (None)                

        OPEN_EXISTING,// creation : open_existing

        0,    // we dont want overlapped operation

        0// no templates file for COM port...

        );

    config.DCBlength = sizeof(config);


    if((GetCommState(m_hCommPort, &config) == 0))
    {
        printf("Get configuration port has a problem.");
        return FALSE;
    }

    config.BaudRate = 9600;
    config.StopBits = ONESTOPBIT;
    config.Parity = PARITY_NONE; 
    config.ByteSize = DATABITS_8;
    config.fDtrControl = 0;
    config.fRtsControl = 0;

    if (!SetCommState(m_hCommPort, &config))
    {

        printf( "Failed to Set Comm State Reason: %d\n",GetLastError());
        //return E_FAIL;
    }

    printf("Current Settings\n Baud Rate %d\n Parity %d\n Byte Size %d\n Stop Bits %d", config.BaudRate, 
        config.Parity, config.ByteSize, config.StopBits);

    int isWritten = WriteFile(m_hCommPort, &data,(DWORD) sizeof(data), &dwBytesWritten, NULL);


    //memset(output, 0, sizeof(output));
    while (abContinue) 
    {

        isRead = ReadFile(m_hCommPort, output, sizeof(output), &dwBytesRead, NULL);

        if(!isRead)
        {
            abContinue = false;
            break;
        }

    }

    cin.get();
}

I am having trouble reading from the com port. If I step through the code, it goes into “isRead = ReadFile(m_hCommPort, output, sizeof(output), &dwBytesRead, NULL);” and doesn’t come back out…. This is my first try at this with no success.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:26:26+00:00Added an answer on May 22, 2026 at 12:26 pm

    You might try some code something like this after you’ve opened the file, but before you try to use it:

    COMMTIMEOUTS timeouts;
    
    timeouts.ReadIntervalTimeout = 1;
    timeouts.ReadTotalTimeoutMultiplier = 1;
    timeouts.ReadTotalTimeoutConstant = 1;
    timeouts.WriteTotalTimeoutMultiplier = 1;
    timeouts.WriteTotalTimeoutConstant = 1;
    if (!SetCommTimeouts(m_hCommPort, &timeouts))
        // setting timeouts failed.
    

    Edit: perhaps it’s easier to start with some code that works, and make it do what you want rather than trying to get your code to work. Here’s a simple terminal program. It’s minimalist in the extreme, but does work (at least well enough to let me see output from my GPS, for one example). It’s a long ways from what anybody (least of all me) would call sophisticated, but should give at least some idea of how to get started.

    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    #define STRICT
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    
    void system_error(char *name) {
    // Retrieve, format, and print out a message from the last error.  The 
    // `name' that's passed should be in the form of a present tense noun 
    // (phrase) such as "opening file".
    //
        char *ptr = NULL;
        FormatMessage(
            FORMAT_MESSAGE_ALLOCATE_BUFFER |
            FORMAT_MESSAGE_FROM_SYSTEM,
            0,
            GetLastError(),
            0,
            (char *)&ptr,
            1024,
            NULL);
    
        fprintf(stderr, "\nError %s: %s\n", name, ptr);
        LocalFree(ptr);
    }
    
    int main(int argc, char **argv) {
    
        int ch;
        char buffer[1];
        HANDLE file;
        COMMTIMEOUTS timeouts;
        DWORD read, written;
        DCB port;
        HANDLE keyboard = GetStdHandle(STD_INPUT_HANDLE);
        HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
        DWORD mode;
        char port_name[128] = "\\\\.\\COM3";
        char init[] = ""; // e.g., "ATZ" to completely reset a modem.
    
        if ( argc > 2 )
            sprintf(port_name, "\\\\.\\COM%c", argv[1][0]);
    
        // open the comm port.
        file = CreateFile(port_name,
            GENERIC_READ | GENERIC_WRITE,
            0, 
            NULL, 
            OPEN_EXISTING,
            0,
            NULL);
    
        if ( INVALID_HANDLE_VALUE == file) {
            system_error("opening file");
            return 1;
        }
    
        // get the current DCB, and adjust a few bits to our liking.
        memset(&port, 0, sizeof(port));
        port.DCBlength = sizeof(port);
        if ( !GetCommState(file, &port))
            system_error("getting comm state");
        if (!BuildCommDCB("baud=19200 parity=n data=8 stop=1", &port))
            system_error("building comm DCB");
        if (!SetCommState(file, &port))
            system_error("adjusting port settings");
    
        // set short timeouts on the comm port.
        timeouts.ReadIntervalTimeout = 1;
        timeouts.ReadTotalTimeoutMultiplier = 1;
        timeouts.ReadTotalTimeoutConstant = 1;
        timeouts.WriteTotalTimeoutMultiplier = 1;
        timeouts.WriteTotalTimeoutConstant = 1;
        if (!SetCommTimeouts(file, &timeouts))
            system_error("setting port time-outs.");
    
        // set keyboard to raw reading.
        if (!GetConsoleMode(keyboard, &mode))
            system_error("getting keyboard mode");
        mode &= ~ ENABLE_PROCESSED_INPUT;
        if (!SetConsoleMode(keyboard, mode))
            system_error("setting keyboard mode");
    
        if (!EscapeCommFunction(file, CLRDTR))
            system_error("clearing DTR");
        Sleep(200);
        if (!EscapeCommFunction(file, SETDTR))
            system_error("setting DTR");
    
        if ( !WriteFile(file, init, sizeof(init), &written, NULL))
            system_error("writing data to port");
    
        if (written != sizeof(init))
            system_error("not all data written to port");
    
        // basic terminal loop:
        do {
            // check for data on port and display it on screen.
            ReadFile(file, buffer, sizeof(buffer), &read, NULL);
            if ( read )
                WriteFile(screen, buffer, read, &written, NULL);
    
            // check for keypress, and write any out the port.
            if ( kbhit() ) {
                ch = getch();
                WriteFile(file, &ch, 1, &written, NULL);
            }
        // until user hits ctrl-backspace.
        } while ( ch != 127);
    
        // close up and go home.
        CloseHandle(keyboard);
        CloseHandle(file);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

include "stdafx.h" #include <windows.h> #include <tchar.h> #include <stdio.h> void _tmain(int argc, TCHAR *argv[]) {
#include stdafx.h #include <windows.h> #include <winsock.h> #include <stdio.h> int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR IpCmdLine,int
I'm porting a small C++ console application from windows to linux, GCC 4.3.2. When
I usually use XCode but was having a problem opening a file with this
I want to send data from a c# application to a c++ application through
I want to check a file is present in C drive or not..? can
I am totally new in programming. For my first programm I tried to make
I am writing an application that needs to send data over a network. I
I have a simple managed C++ assembly which I am providing unmanaged wrappers for
This is a really strange issue. One day my project started to do a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.