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 strings or chars, 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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -