c++ 在使用串行端口发送之前将十六进制转换为 ASCII
c++ Convert hexa to ASCII before send with the serial port
我想与C++中的电子负载通信。我使用 win32.h。要将电子负载放入遥控器中,我需要发送:
"AA 00 20 01
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 CB"但在发送之前,我需要将其转换为 ASCII 代码。
我的代码是:
HANDLE hCom;
DWORD dwError;
BOOL fSuccess;
DWORD dwEvtMask;
int i;
int NbOctet;
char *Message;
unsigned long nBytesWrite;
LPCWSTR Port = L"COM14";
Message = new char[200];
std::string Test;
/*-----------------------------------------------*/
/* Ouverture du port de communiucation */
/*-----------------------------------------------*/
hCom = CreateFile(Port,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL
);
Message = "AA 00 20 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 CB";
NbOctet = strlen(Message);
WriteFile(hCom,Message,NbOctet,&nBytesWrite,NULL);
CloseHandle(hCom);
delete[] Message;
我的问题是:如何在发送之前转换 ASCII 字符中的消息?
我在python中有一个我想要的例子:
# Construct a set to remote command
cmd = chr(0xaa) + chr(0x00) + chr(0x20) # First three bytes
cmd += chr(0x01) + chr(0x00)*(length_packet - 1 - 4)
cmd += chr(CalculateChecksum(cmd))
sp.write(cmd)
我的新代码是:
void main(int argc, TCHAR *argv[])
{
HANDLE hCom;
DWORD dwError;
BOOL fSuccess;
DWORD dwEvtMask;
int i;
int NbOctet;
unsigned long nBytesWrite;
LPCWSTR Port = L"\\.\COM14";
/*-----------------------------------------------*/
/* Ouverture du port de communiucation */
/*-----------------------------------------------*/
hCom = CreateFile(Port,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL
);
char Message[] = {0xAA,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xCB};
NbOctet = strlen(Message);
qDebug() << Message;
WriteFile(hCom,Message,NbOctet,&nBytesWrite,NULL);
CloseHandle(hCom);
}
但它不起作用
这可以通过std::ostringstream
来完成,以将单独的值转换为字符串,std::stoi
将字符串解析为整数:
std::ostringstream os("AA 00 20 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 CB");
std::vector<uint8_t> values;
std::string value_string;
while (os >> value_string)
values.push_back(static_cast<uint8_t>(std::stoi(value_string, nullptr, 16)));
WriteFile(hCom, values.data(), sizeof(uint8_t) * values.size(), &nBytesWrite, NULL);
如果你想
使用十六进制,我不建议使用十六进制字符串。您可以改为执行以下操作:
char message_bytes[] = {0xAA, 0x00, 0x20, 0x01, 0x00, 0x00, 0x00};
int message_length = 7; /* determine the size using either an argument or manually */
此外,您的打开文件功能将失败,因为当 COM 编号> 9 时,您需要执行以下操作才能让窗口识别它们:
LPCWSTR Port = L"\\.\COM14";
如本文所述
您没有正确设置COM端口,请考虑将DCB与BuildCommDCB一起使用,然后使用SetcommState:
DCB portConfig;
memset(&portConfig, 0, sizeof(portConfig));
portConfig.DCBlength = sizeof(portConfig);
/* 9600 baud, Even parity, 8 data bits, 1 stop bit */
BuildCommDCB(TEXT("9600,E,8,1"), &portConfig);
SetCommState(hCom, &portConfig);
确保将端口设置为与硬件相同。请注意,虽然我没有包括错误检查,但我强烈建议您这样做。
相关文章:
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 将十六进制转换为 DEC
- 将字符指针中的十六进制转换为十进制
- 使用 std::hex 将十六进制转换为十进制
- 将40个字节长的数据框从十六进制转换为二进制,然后转换为十进制
- 如何将十六进制转换为十进制
- 在C++中将十六进制转换为uint8_t
- 从十六进制转换为LPCVoid,切断了一半地址
- 将十六进制转换为可打印的字符串/字符
- 将 QString 十六进制转换为 ASCII 值
- 我如何在 C++ 中将变量从十六进制转换为 dec
- 混淆在C++中将十六进制转换为二进制
- C++中的十进制到十六进制转换代码
- 如何在C++中将十六进制转换为IEEE 754 32位浮点
- 十进制到十六进制转换错误
- 十进制到十六进制转换c++内置函数
- 如何将十六进制转换为字符串?C++
- 将 3d 数组元素从十六进制转换为二进制以获得 bmp
- C++:将十六进制转换为十进制
- 将十六进制转换为签名的十二月