C++ COM using fstream

I read data from the com port I did it using ReadFile, which I never seriously worked with before, the code is just incomprehensible. Then I thought if this is still a file reading technology, why not use the fstream I'm familiar with.

The port opened, the device responded to it and started sending data, but nothing was output to the console. how to organize reading from a com port using the fstream library

#include<Windows.h>
#include<stdio.h>
#include<iostream>
#include<fstream>
using namespace std;

void main()
{
    ifstream in("COM3");
        if(in) 
            cout<<"open ok";
        else
            cout<<"open noo";


        if(in)
        {
            char t;
            in.read(&t,sizeof(t));
            cout<<t;
        }

    }

The program stops at the line in. read(&t,sizeof(t)); and does not respond to input data.

Author: MSDN.WhiteKnight, 2019-05-16

2 answers

To read the file in the old-fashioned byte-by-byte way, you need to set the option:

ifstream in("COM3",std::ios::binary);
 1
Author: AlexGlebe, 2019-05-17 08:19:41

To work with the COM port, you must configure it after opening the file.

I recommend reading my article Arduino Host client on C (Linux) .

 -3
Author: Vanyamba Electronics, 2019-05-16 10:56:25