Author |
Topic: What is good practice for reading com ports? (Read 414 times) |
|
DJ-Dwayne
New Member
member is offline


Gender: 
Posts: 4
|
 |
What is good practice for reading com ports?
« Thread started on: Jan 8th, 2016, 08:55am » |
|
I have a ARDUINO board constantly sending data to the com port. I can probably workout how to sort the data ok, but when I go read the data should/can I keep the port open 24/7 or should I close it then reopen it straight away to get the next lot of data? (I'm not to familiar with the concepts of buffer over run or over flow in windows and loosing some data isn't a big deal) And keeping in mind, I will once in a blue moon need to write/send data as well.
Should I maybe "handshake" the board or just read its constant stream?
|
« Last Edit: Jan 8th, 2016, 09:04am by DJ-Dwayne » |
Logged
|
|
|
|
DDRM
Administrator
member is offline


Gender: 
Posts: 321
|
 |
Re: What is good practice for reading com ports?
« Reply #1 on: Jan 13th, 2016, 08:26am » |
|
Hi There,
I preface my comments with the caution that I have never used the serial ports via BB4W, and possibly never at all, so what I say should be taken with a large pinch of salt (or possibly a small spoonful, in a glass of hot water with honey, and preferably a shot of rum. It won't help the programming, but it shifts catarrh wonderfully!). However, since no-one else has offered an opinion publicly, I hope the following is helpful (or will provoke a correction from someone who knows better!).
The answer probably depends on whether any other program needs access to the port - in that case you should probably close it and give them a chance. Otherwise I think I'd leave it open, and poll it frequently (the help explains about using EXT# to check whether there is any data to read), and then reading in whatever is there.
MSDN says that if you close it, you need to wait a bit before re-opening it, as it may not get closed instantly:
https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.open%28v=vs.110%29.aspx
but I don't know if that means a few milliseconds or a few seconds! I certainly wouldn't close it, whip through a few other instructions, and then re-open it.
MSDN also says somewhere that the input buffer is (at least) 4096 bytes, so depending on the frequency with which the Arduino is sending, you may not need to check that often, depending what you are doing with the data. I think I'd try to avoid letting the buffer overflow if possible, because then you wouldn't know how old the data was - but of course that may depend on the application.
I believe the output buffer is separate from the input one, but I don't know how they interleave/ interact.
Handshaking: if I'm honest I've no idea, but I don't think you need to: you can just treat it like a data file.
Hope that's useful, and at least mostly correct!
D
|
|
Logged
|
|
|
|
|