每天都去711 2022-04-17 17:33 采纳率: 0%
浏览 55
已结题

c++ 串口通讯readfile只能接收8个字符,这是为什么?


HANDLE hComm;
    std::string port_name = "\\\\.\\COM19";  //change port name

    char write_buffer[100];
    char read_buffer[100];
    DWORD dNoOFBytestoWrite;         // No of bytes to write into the port
    DWORD dNoOfBytesWritten = 0;     // No of bytes written to the port
    DWORD bytes_read ;

    hComm = CreateFileA(port_name.c_str(),                //port name
        GENERIC_READ | GENERIC_WRITE, //Read/Write
        0,                            // No Sharing
        NULL,                         // No Security
        OPEN_EXISTING,// Open existing port only
        0,            // Non Overlapped I/O
        NULL);        // Null for Comm Devices

    if (hComm == INVALID_HANDLE_VALUE)
    {
        std::cerr << "Error in opening serial port”";
        return -1;
    }
    else
        std::cerr << "opening serial port successful";

    DCB dcbSerialParams = { 0 }; // Initializing DCB structure
    dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
    GetCommState(hComm, &dcbSerialParams);

    dcbSerialParams.BaudRate = CBR_9600;  // Setting BaudRate = 9600
    dcbSerialParams.ByteSize = 8;         // Setting ByteSize = 8
    dcbSerialParams.StopBits = ONESTOPBIT;// Setting StopBits = 1
    dcbSerialParams.Parity = NOPARITY;  // Setting Parity = None
    SetCommState(hComm, &dcbSerialParams);


    COMMTIMEOUTS timeouts = { 0 };
    timeouts.ReadIntervalTimeout = 20; // in milliseconds
    timeouts.ReadTotalTimeoutConstant = 100; // in milliseconds
    timeouts.ReadTotalTimeoutMultiplier = 10; // in milliseconds
    timeouts.WriteTotalTimeoutConstant = 100; // in milliseconds
    timeouts.WriteTotalTimeoutMultiplier = 10; // in milliseconds 
    SetCommTimeouts(hComm, &timeouts);  //   将超时参数写入设备控制

    dNoOFBytestoWrite = sizeof(write_buffer);

    WriteFile(hComm,        // Handle to the Serial port
        write_buffer,     // Data to be written to the port
        dNoOFBytestoWrite,  //No of bytes to write
        &dNoOfBytesWritten, //Bytes written
        NULL);

    Sleep(100);

    do
    {
        ReadFile(hComm,         //Handle of the Serial port
            &read_buffer,       //Temporary character
            100,                //Size of TempChar
            &bytes_read,        //Number of bytes read
            NULL);

    } while (bytes_read <= 0);


    char dst[100];
    ascii2hex(read_buffer, dst, 100);

    cout << endl;
    cout << "Read Data : "  <<dst << endl;
    CloseHandle(hComm); 

readfile无论怎么设置都只能接收8个字符,不知道是啥情况?

img

我用串口调试助手,却没有问题

img

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 4月25日
    • 创建了问题 4月17日

    悬赏问题

    • ¥15 征集Python提取PDF文字属性的代码
    • ¥15 有偿求苍穹外卖环境配置
    • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
    • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
    • ¥15 clousx6整点报时指令怎么写
    • ¥30 远程帮我安装软件及库文件
    • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
    • ¥20 深信服vpn-2050这台设备如何配置才能成功联网?
    • ¥15 Arduino的wifi连接,如何关闭低功耗模式?
    • ¥15 Android studio 无法定位adb是什么问题?