c# - Can I use SerialPort.Write to send byte array -
documentation of serialport
write
says that
by default, serialport uses asciiencoding encode characters. asciiencoding encodes characters greater 127 (char)63 or '?'. support additional characters in range, set encoding utf8encoding, utf32encoding, or unicodeencoding.
also see here. mean can't send byte array using write
?
by default, serialport uses asciiencoding encode characters
you're confusing methods, read/write string
s or char
s, methods, read/write bytes
.
e.g., when you'll call this:
port.write("абв")
you'll "???" (0x3f
0x3f
0x3f
) in port buffer default. on other hand, call:
// equivalent of sending "абв" in windows-1251 encoding port.write(new byte[] { 0xe0, 0xe1, 0xe2 }, 0, 3)
will write sequence 0xe0
0xe1
0xe2
directly, without replacing bytes 0x3f
value.
upd.
let's source code:
public void write(string text) { // preconditions checks omitted byte[] bytes = this.encoding.getbytes(text); this.internalserialstream.write(bytes, 0, bytes.length, this.writetimeout); } public void write(byte[] buffer, int offset, int count) { // preconditions checks omitted this.internalserialstream.write(buffer, offset, count, this.writetimeout); }
do see difference?
method, accepts string
, converts strings byte
array, using current encoding port. method, accepts byte
array, writes directly stream, wrapper around native api.
and yes, documentation fools you.
Comments
Post a Comment