Frage

Protocol

<STX>COMMAND<ETX><CHEKSUM>

a valid command is:

.004CT1000A.b

with STX = 0x02, ETX = 0x03, CHECKSUM = 0x0062, COMMAND = 004CT1000A

I want to send this command with powershell on a windows server to a serial port. However there are data type issues:

PS > [Char[]] $request = 0x02,0,0,4,"C","T",1,0,0,0,"A",0x03,0x0062
PS > $port.Write($request)

This logs a strange string with whitespace on the left and right of each character:

. . . . C T . . . . A . b 

instead of

.004CT1000A.b

Any ideas what I am doing wrong?


and I tried

PS > [Char[]] $request = 0x02,"0","0","4","C","T","1","0","0","0","A",0x03,0x0062
PS > $port.Write($request)

which printed the right command but with a lot of whitespaces

. 0 0 4 C T 1 0 0 0 A . b

.004CT1000A.b is created with:

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);
War es hilfreich?

Lösung

You are using the .Net char object type, which is unicode. You probably want to convert your characters to ascii. Some info is here.

Or you could just use the decimal values for your ASCII characters and store it all in a byte array:

> [Byte[]] $request = 0x02,0,0,4,67,84,1,0,0,0,65,0x03,0x0062

Andere Tipps

This doesn't work for me. There are still lots of white spaces in between. [Byte[]] $request = 0x02,0,0,4,67,84,1,0,0,0,65,0x03,0x0062

My solution is: $request =[char]2 + "004CT1000A" + [char]3 + [char]62

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top