When using the serial port via POSIX, it is recommended that you save the original attributes with tcgetattr()before changing them with tcsetattr()and then restore them before closing the port. How about when the program terminates by pressing the control-C button or when the program receives SIGINT? I have not seen this in any of the series.
Apparently, the function atexit()will be insufficient because it is not called SIGINTby the default handler . Therefore, it seems that you will need to install a signal handler that restores the attributes for any serial ports that are still open. Is it even safe to call tcsetattr()from a signal handler?
You can simply discard this problem as minor, but usually a control-C program ends, especially one that can take several tens of seconds to complete operations. If in this case you do not save the serial port settings, then there is no reason to save them. In any case, it is better not to worry, and not to do it inconsistently.
I found some examples of the source code made above , but nothing is documented. I guess I'm interested in discussing whether this is good. Thank.
source
share