[英]Sending wchar_t message through serial port
the problem that I have is that when sending a message L"Second line" to my application to read serial port I am receiving "S econd L ine", but at putty I do received "Second line" I think that is because wchar_t is coded in 16 bit so I have a 00 between every letter. 我的问题是,当向我的应用程序发送消息L“第二行”以读取串行端口时,我收到“ S econd L ine”,但是在腻子上我确实收到了“第二行”,我认为这是因为wchar_t为以16位编码,因此每个字母之间都有一个00。 But still don't know how I can fix this, I am new with all these things so it is kind of confusing.
但是仍然不知道如何解决这个问题,我对所有这些东西都是陌生的,所以有点令人困惑。
I am not sure if I need to set my byte size to 16 in my application? 我不确定是否需要在应用程序中将字节大小设置为16?
I want to send that LPCWSTR LogpszMessage, because I am sending some logs messages from an application. 我想发送该LPCWSTR LogpszMessage,因为我正在从应用程序发送一些日志消息。 Which is another code, working here must work there.
这是另一个代码,在这里工作必须在这里工作。
putty configuration is to 8 bits and this is what I am sending; 油灰配置为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;
}
Thanks in advance. 提前致谢。
I could not do it with WideCharToMultiByte(), yield me error when trying to use LPCWSTR LogpszMessage, so I did the job with wcstombs_s following microsoft documentation and it worked, now when reading the COM port there is no spaces caused by 00 of 16 bit characters. 我无法使用WideCharToMultiByte()进行操作,在尝试使用LPCWSTR LogpszMessage时出现错误,因此我按照Microsoft文档的说明通过wcstombs_s完成了工作,现在读取COM端口时没有空格,这是16位00引起的字符。
Thank you Hans Passant ! 谢谢Hans Passant !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.