各位好, 目前遇到serialport問題, 當首次open時, read/write都沒問題, close後再open卻無法write(對方沒收到data, 過程也完全沒有錯誤訊息)
發生問題時把APP關掉重開也沒用, 一定要把USB COM Port重插才可正常動作
請問這有可能是什麼問題? 資源沒有釋放嗎
以下是我test code, 第二個Read()會timeout
SerialPort MySerialPort = new SerialPort("COM5", 115200, Parity.None, 8, StopBits.One);
MySerialPort.ReadTimeout = 2000;
MySerialPort.Open();
byte[] buffer = new byte[] { 0, 1, 2, 3 };
MySerialPort.Write(buffer, 0, buffer.Length);
byte[] ReadBuffer = new byte[MySerialPort.ReadBufferSize + 1];
int length = MySerialPort.Read(ReadBuffer, 0, ReadBuffer.Length);
Thread.Sleep(2000);
MySerialPort.Close();
Thread.Sleep(2000);
MySerialPort.Open();
buffer = new byte[] { 1, 2, 3, 4 };
MySerialPort.Write(buffer, 0, buffer.Length);
ReadBuffer = new byte[MySerialPort.ReadBufferSize + 1];
length = MySerialPort.Read(ReadBuffer, 0, ReadBuffer.Length);
Thread.Sleep(2000);
MySerialPort.Close();