Serial port takes a long time to respond
-
Hi, Im currently making an app on windows forms trying to program a micro over UART. I can achieve it, although it is taking a long time to carry out the program. I have used stopwatches to determine it is my read function taking up the majority of the time. When i try to read the micros response from each command I have to wait for all of it, which is why im using a while loop in the code below, if the returned message size is not what i expect. What im wondering is, if there is any way to speed up this process. The response from the micro should be pretty fast, its running at a baudrate of 115200, meaning the whole 512 kb file should in theory take just over 30 seconds to complete, at the moment it is more than double that, at 80 seconds.
private string CheckResponse(int mSize) { //ComPort.DataReceived += SerialPortDataReceived; string response; int intBuffer; intBuffer = ComPort.BytesToRead; byte\[\] byteBuffer = new byte\[intBuffer\]; ComPort.Read(byteBuffer, 0, intBuffer); response = Encoding.ASCII.GetString(byteBuffer); while(response.Length != mSize) { intBuffer = ComPort.BytesToRead; byteBuffer = new byte\[intBuffer\]; ComPort.Read(byteBuffer, 0, intBuffer); response += Encoding.ASCII.GetString(byteBuffer); } return response; }
Any help greatly appreciated. Blair
-
Hi, Im currently making an app on windows forms trying to program a micro over UART. I can achieve it, although it is taking a long time to carry out the program. I have used stopwatches to determine it is my read function taking up the majority of the time. When i try to read the micros response from each command I have to wait for all of it, which is why im using a while loop in the code below, if the returned message size is not what i expect. What im wondering is, if there is any way to speed up this process. The response from the micro should be pretty fast, its running at a baudrate of 115200, meaning the whole 512 kb file should in theory take just over 30 seconds to complete, at the moment it is more than double that, at 80 seconds.
private string CheckResponse(int mSize) { //ComPort.DataReceived += SerialPortDataReceived; string response; int intBuffer; intBuffer = ComPort.BytesToRead; byte\[\] byteBuffer = new byte\[intBuffer\]; ComPort.Read(byteBuffer, 0, intBuffer); response = Encoding.ASCII.GetString(byteBuffer); while(response.Length != mSize) { intBuffer = ComPort.BytesToRead; byteBuffer = new byte\[intBuffer\]; ComPort.Read(byteBuffer, 0, intBuffer); response += Encoding.ASCII.GetString(byteBuffer); } return response; }
Any help greatly appreciated. Blair
You might want to post this under Hardware & Devices[^], though I imagine someone who can help will find it nonetheless.
Robust Services Core | Software Techniques for Lemmings | Articles
-
Hi, Im currently making an app on windows forms trying to program a micro over UART. I can achieve it, although it is taking a long time to carry out the program. I have used stopwatches to determine it is my read function taking up the majority of the time. When i try to read the micros response from each command I have to wait for all of it, which is why im using a while loop in the code below, if the returned message size is not what i expect. What im wondering is, if there is any way to speed up this process. The response from the micro should be pretty fast, its running at a baudrate of 115200, meaning the whole 512 kb file should in theory take just over 30 seconds to complete, at the moment it is more than double that, at 80 seconds.
private string CheckResponse(int mSize) { //ComPort.DataReceived += SerialPortDataReceived; string response; int intBuffer; intBuffer = ComPort.BytesToRead; byte\[\] byteBuffer = new byte\[intBuffer\]; ComPort.Read(byteBuffer, 0, intBuffer); response = Encoding.ASCII.GetString(byteBuffer); while(response.Length != mSize) { intBuffer = ComPort.BytesToRead; byteBuffer = new byte\[intBuffer\]; ComPort.Read(byteBuffer, 0, intBuffer); response += Encoding.ASCII.GetString(byteBuffer); } return response; }
Any help greatly appreciated. Blair
There is no point "reading" when the "BytesToRead" equals 0. If you're expecting data, and BytesToRead == 0, you need to "wait" until the count > 0 and "time out" after a certain time if it doesn't. A loop of synchronous reads with no data may be your problem.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food