gpt4 book ai didi

c++通过串行通信发送ascii 254

转载 作者:行者123 更新时间:2023-11-30 01:58:16 25 4
gpt4 key购买 nike

我使用的是 Arduino 网站上的 Serial.cpp 代码。

我现在正在做的是使用 R220HPRS 继电器开关。问题是示例代码在 VB 中,而我使用的是 C++。所以,我通读了手册,发现通过发送 ASCII 254 和 ASCII 1 将打开继电器 1 开关

VB 示例

  MSComm1.Output = Chr$(254) 'Enter Command Mode 
MSComm1.Output = Chr$(1) 'Turn On Relay 1

然后我从 Arduino 的网站上找到了 Serial.cpp 并决定尝试使用它。当我这样做时,我与设备建立了连接(或者它在我运行程序时说它连接了),但我不知道如何发送 ASCII 254 和 ASCII 1

代码如下来自 Serial.cpp

bool Serial::WriteData(char *buffer, unsigned int nbChar)
{
DWORD bytesSend;

//Try to write the buffer on the Serial port
if(!WriteFile(this->hSerial, (void *)buffer, nbChar, &bytesSend, 0))
{
//In case it don't work get comm error and return false
ClearCommError(this->hSerial, &this->errors, &this->status);

return false;
}
else
return true;
}

主要

 int _tmain(int argc, _TCHAR* argv[])
{
printf("Welcome to the serial test app!\n\n");

Serial* SP = new Serial("\\\\.\\COM3"); // adjust as needed

if (SP->IsConnected())
printf("We're connected");

while(SP->IsConnected())
{
char *chr0 = "254";
SP->WriteData(chr0, 1);
SP->WriteData(chr0, 1);
Sleep(500);
}
return 0;
}

我知道 chr0 = "254"不是 ASCII,但我不知道如何发送 ASCII 254 和 ASCII 1。

最佳答案

要发送值为 245 的单个 char,您需要将其设为 char:

char chr0 = 254; 

SP->WriteData(&chr0, 1);

关于c++通过串行通信发送ascii 254,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17653949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com