03-31-2011 12:08 PM - edited 03-31-2011 12:10 PM
Hi guys! I would like your opinion as to which method of RS232 communication is better from the two examples I have attached. The block diagram shows what I am trying to do.
Thanks!
03-31-2011 12:15 PM
Also, on another note. Are global variables better than references? Or is this comparison like apples and oranges.
I understand the EVIL of globals, but in my case variables are written only at one place and read from one place.
03-31-2011 12:43 PM
Globals are not EVIL. Much like duct tape they can be very useful at times. Variables don't cause race conditions - programmers cause race conditions.
If you have a single writer and a single reader you should be fine. You just have to worry about missing or stale data if that is a concern.
But I find that I never need to use global variables or local variables at all.
04-01-2011 02:45 PM
As Steve said, global variables can cause race conditions if not used properly. If used properly, there is no reason why one setup is better than the other (at least nothing noticeable from what you sent). Generally speaking, however, we try to warn customers of the possible errors that can occur when using them.
Thanks,
Sean
04-01-2011 03:12 PM
While there can be times when a global variable is useful I would recommend against using them. They can cause problems when used and are a lazy way of designing an application. By using other preferred methods (queues, notifiers, action engines) you have more control over their use and can provide more protections. In addition, these methods help to decouple the code. Global variables couple modules together and make reuse much more difficult.
I like to take that approach that anything I write will live for longer than I expect and there are good chances I will need to come back to it in the future to modify it or add functionality. By always using best practices my job is easier because I have less rework when I do touch the code again. Plan for maintainability, modularity and reuse and you will never go wrong.
04-04-2011 09:32 AM
any comments on the two programs itself?? Which way is better in terms of reading from serial port and writing back on same port.
04-04-2011 05:43 PM
VeeJay-
Again, I would recommend staying away from global variables when possible.
Thanks,
Sean
04-05-2011 09:19 AM - edited 04-05-2011 09:26 AM
Thanks Sean! I got the part about using Global variables.
My question was with regards to reading from COM port and writing back. Is the queue method efficient or does the constant polling for commands better?
what about using references? Please refer vi. Thanks!
V
04-05-2011 01:16 PM
VeeJay-
I couldn't really say which one is better or more efficient. In the constant polling method, it would seem that if there is no data, than the process is wasted, or doing nothing, where as in the queue (producer/consumer) method, the consumer loop is inactive until there is data in the queue. However, the producer loop is still constantly polling. Overall, the producer/consumer loops seem overly complex to accomplish the same thing.
Thanks,
Sean