协议
<STX>COMMAND<ETX><CHEKSUM>
有效命令是:
.004CT1000A.b
STX = 0x02, ETX = 0x03, CHECKSUM = 0x0062, COMMAND = 004CT1000A
我想将此命令与PowerShell一起发送到Windows服务器上的串口。但是有数据类型问题:
PS > [Char[]] $request = 0x02,0,0,4,"C","T",1,0,0,0,"A",0x03,0x0062
PS > $port.Write($request)
这会在每个字符的左侧和右侧记录一个带有空格的奇怪字符串:
. . . . C T . . . . A . b
而不是
.004CT1000A.b
任何想法我做错了什么?
我试过
PS > [Char[]] $request = 0x02,"0","0","4","C","T","1","0","0","0","A",0x03,0x0062
PS > $port.Write($request)
打印了正确的命令,但有很多空格
. 0 0 4 C T 1 0 0 0 A . b
.004CT1000A.b创建于:
unsigned char checksum = 0x02;
nrComm1->SendChar(0x02);
while(*TX_String)
{
nrComm1->SendChar(*TX_String);
checksum ^= *TX_String++;
}
nrComm1->SendChar(0x03);
checksum ^= 0x03;
nrComm1->SendChar(checksum);
答案 0 :(得分:2)
您使用的是.Net char对象类型,即unicode。您可能希望将字符转换为ascii。有些信息是here。
或者您可以只使用ASCII字符的十进制值并将其全部存储在字节数组中:
> [Byte[]] $request = 0x02,0,0,4,67,84,1,0,0,0,65,0x03,0x0062
答案 1 :(得分:0)
这对我不起作用。中间仍然有很多空白区域。 [Byte []] $ request = 0x02,0,0,4,67,84,1,0,0,0,65,0x03,0x0062
我的解决方案是: $ request = [char] 2 +&#34; 004CT1000A&#34; + [char] 3 + [char] 62