繁体   English   中英

通过串行端口发送wchar_t消息

[英]Sending wchar_t message through serial port

我的问题是,当向我的应用程序发送消息L“第二行”以读取串行端口时,我收到“ S econd L ine”,但是在腻子上我确实收到了“第二行”,我认为这是因为wchar_t为以16位编码,因此每个字母之间都有一个00。 但是仍然不知道如何解决这个问题,我对所有这些东西都是陌生的,所以有点令人困惑。

我不确定是否需要在应用程序中将字节大小设置为16?

我想发送该LPCWSTR LogpszMessage,因为我正在从应用程序发送一些日志消息。 这是另一个代码,在这里工作必须在这里工作。

油灰配置为8位,这就是我要发送的内容;

#include "stdafx.h"

#include <windows.h>
#include <stdio.h>

typedef _Null_terminated_ CONST WCHAR *LPCWSTR, *PCWSTR;

int main()
{

    LPCWSTR LogpszMessage = L"Second line";
    char bytes_to_send[] = "test1 y test2";

    // Declare variables and structures
    HANDLE hSerial;
    DCB dcbSerialParams = { 0 };
    COMMTIMEOUTS timeouts = { 0 };

    // Open the highest available serial port number
    fprintf(stderr, "Opening serial port...");
    hSerial = CreateFile(
        L"\\\\.\\COM24", GENERIC_READ | GENERIC_WRITE, 0, NULL,
        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hSerial == INVALID_HANDLE_VALUE)
    {
        fprintf(stderr, "Error\n");
        return 1;
    }
    else fprintf(stderr, "OK\n");

    dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
    if (GetCommState(hSerial, &dcbSerialParams) == 0)
    {
        fprintf(stderr, "Error getting device state\n");
        CloseHandle(hSerial);
        return 1;
    }

    dcbSerialParams.BaudRate = CBR_115200;
    dcbSerialParams.ByteSize = 8;
    dcbSerialParams.StopBits = ONESTOPBIT;
    dcbSerialParams.Parity = NOPARITY;
    if (SetCommState(hSerial, &dcbSerialParams) == 0)
    {
        fprintf(stderr, "Error setting device parameters\n");
        CloseHandle(hSerial);
        return 1;
    }

    // Set COM port timeout settings
    timeouts.ReadIntervalTimeout = 50;
    timeouts.ReadTotalTimeoutConstant = 50;
    timeouts.ReadTotalTimeoutMultiplier = 10;
    timeouts.WriteTotalTimeoutConstant = 50;
    timeouts.WriteTotalTimeoutMultiplier = 10;
    if (SetCommTimeouts(hSerial, &timeouts) == 0)
    {
        fprintf(stderr, "Error setting timeouts\n");
        CloseHandle(hSerial);
        return 1;
    }

    // Send specified text (remaining command line arguments)
    DWORD bytes_written, total_bytes_written = 0;
    fprintf(stderr, "Sending bytes...");
    /*if (!WriteFile(hSerial, bytes_to_send, sizeof(bytes_to_send), &bytes_written, NULL))
    {
        fprintf(stderr, "Error\n");
        CloseHandle(hSerial);
        return 1;
    }*/

    if (!WriteFile(hSerial, LogpszMessage, wcslen(LogpszMessage) * sizeof(wchar_t), &bytes_written, NULL))
    {
        fprintf(stderr, "Error\n");
        CloseHandle(hSerial);
        return 1;
    }
    fprintf(stderr, "%d bytes written\n", bytes_written);

    // Close serial port
    fprintf(stderr, "Closing serial port...");
    if (CloseHandle(hSerial) == 0)
    {
        fprintf(stderr, "Error\n");
        return 1;
    }
    fprintf(stderr, "OK\n");
    getchar();

    // exit normally
    return 0;
}

提前致谢。

我无法使用WideCharToMultiByte()进行操作,在尝试使用LPCWSTR LogpszMessage时出现错误,因此我按照Microsoft文档的说明通过wcstombs_s完成了工作,现在读取COM端口时没有空格,这是16位00引起的字符。

谢谢Hans Passant

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM