Home c++ Working with C++ port

Working with C++ port

Author

Date

Category

You need to write a program for exchanging data with a device virtual COM port. The program is stuck in readfile and does not go further. I tried different examples that I found on the network – the result is the same.

# include & lt; windows.h & gt;
#Include & lt; iostream & gt;
Using Namespace STD;
INT MAIN ()
{
  DCB DCB;
  Handle Port = CreateFile ("COM9",
    Generic_read | Generic_Write,
    0,
    NULL
    OPEN_EXISTING,
    0,
    Null);
  Buildcommdcb ("BAUD = 9600 PARITY = N DATA = 8 STOP = 1", & amp; dcb);
  Char buf_out [1024] = "$ 016";
  Char buf_in [1024];
  DWORD OSIZE = SIZEOF (BUF_OUT);
  DWORD BYTESWRITENT;
  DWORD ISIZE;
  While (1)
  {
    WriteFile (Port, Buf_out, Osize, & amp; Byteswritten, NULL);
    Readfile (port, & amp; buf_in, sizeof (buf_in), & amp; isize, 0);
    COUT & LT; & LT; buf_in & lt; & lt; Endl;
  }
  Closehandle (Port);
  SYSTEM ("PAUSE");
  Return 0;
}

Answer 1, Authority 100%

All right, readfile is a blocking operation. After her call, it expects data arrival. Since you do not send data, it expects them all the time.

Write a separate small application that will send data to the specified port.
The bonus will use not WinAPI, but boost.asio .

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions