c# - What is equivalent to serialPort.ReadExisting() in Windows 10 app? -
i have used serialport class in previous desktop application using following method read response serialport
var response = serialport.readexisting();
now implementing same thing in windows 10 app using following method
public static async task<string> readasync(cancellationtoken cancellationtoken,datareader datareaderobject) { string response = string.empty; try { var flag = false; task<uint32> loadasynctask; uint readbufferlength = 1024; cancellationtoken.throwifcancellationrequested(); datareaderobject.inputstreamoptions = inputstreamoptions.partial; loadasynctask = datareaderobject.loadasync(readbufferlength).astask(); uint32 bytesread = await loadasynctask; if (bytesread > 0) { byte[] bytes = new byte[bytesread]; datareaderobject.readbytes(bytes); //response = encoding.ascii.getstring(bytes); // response = convert.tostring(bytes); response= asciiencoding.ascii.getstring(bytes); } } catch (exception ex) { response = string.empty; } return response; }
but , looks getting response in different encoding format. when copied both responses in notepad++ found following difference:
going wrong here? equivalent serialport.readexisting() in windows 10 app?
the serialport.readexisting method ()
returns contents of stream , internal buffer of serialport
object string, equivalent readexisting()
in windows 10 app should datareader.readstring | readstring method.
but it's ok use datareader.readbytes | readbytes method, problem how convert byte array string. byte here uses uint8 encoding, possible can string this:
encoding.utf8.getstring(bytes);
you can refer japaneses blog, writes ascii encoding data still need read utf8.
Comments
Post a Comment