View previous topic :: View next topic |
Author |
Message |
MCUprogrammer
Joined: 08 Sep 2020 Posts: 221
|
rs232 communication |
Posted: Thu Dec 28, 2023 9:17 am |
|
|
How can I create a control code to detect if the connection is lost during communication via RS232? Would you help me with this topic? _________________ Best Regards...
MCUprogrammer
_______________________________
Work Hard |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Thu Dec 28, 2023 9:27 am |
|
|
Of itself, serial does not have any detection of the 'state' of the connection.
A lot depends on the nature of what is being sent, and how the wiring is done?.
So (for example), you can wire a connection so that if the connection is not
present the receiver is biased to the 'break' state. This will then give
continuous received zero characters with the framing error bit set.
Alternatively, your friend is the timeout. If you know a packet should arrive
every second, then have a timer interrupt set to timeout at (say) 1.2 seconds,
and reset this whenever a packet arrives. If this triggers the connection has
been lost. |
|
|
MCUprogrammer
Joined: 08 Sep 2020 Posts: 221
|
|
Posted: Thu Dec 28, 2023 9:57 am |
|
|
There are 3 connections on the device. RX,TX,GND. For example, can I do something like the following? or what can i do.
Code: |
if (kbhit(UART1)) {
printf("There is a connection\r\n");
} else {
printf("No connection\r\n");
} |
_________________ Best Regards...
MCUprogrammer
_______________________________
Work Hard |
|
|
gaugeguy
Joined: 05 Apr 2011 Posts: 303
|
|
Posted: Thu Dec 28, 2023 10:55 am |
|
|
What you posted would probably not work well on its own. You are really missing the point of the advice Ttelmah was giving.
The answer relies on information you did not provide. How does your device define a working connection? Are the messages formatted or random? Are messages sent at a regular interval or can the arrive at any random time? |
|
|
MCUprogrammer
Joined: 08 Sep 2020 Posts: 221
|
|
Posted: Thu Dec 28, 2023 11:04 am |
|
|
I solved the problem with timeout. thanks _________________ Best Regards...
MCUprogrammer
_______________________________
Work Hard |
|
|
|