我在以前的桌面應用程序中使用SerialPort類,我用下面的方法來讀取的SerialPort什麼等同於Windows 10應用程序中的serialPort.ReadExisting()?
響應var response = serialPort.ReadExisting();
我現在用下面的方法實現在Windows 10應用程序一樣的東西
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;
}
但是,看起來我正在以不同的編碼格式獲得響應。 當我在記事本++複製這兩個響應時,我發現以下區別:
這裏發生了什麼問題? 什麼等效於Windows 10應用程序中的serialPort.ReadExisting()?
如果您可以粘貼舊版SerialPort代碼,這將有所幫助。你發送什麼字節? – Jackie