## 3 System Design and Implementation
### Software Design
#### 3.3.2 Implementation of the AUDP Mechanism
(2) The network exhibits random delays, and the response arrival time follows a probability density function \(f(t)\), and \(f(t)\) follows an exponential distribution. (3) The network has a \(P_{loss}\) rate \(p\), where each transmission request is lost with probability \(p\), necessitating retransmission. A logical error occurs when the delayed response of request \(n\) arrives after the timeout period and is incorrectly matched to the subsequent request \(n+1\). The probability that the response delay to request \(n\) exceeds \(T\) is  
\[P_{\text{delay}}=\int_{T}^{\infty}f(t)\,dt \tag{11}\]  
If the response to request \(n\) exceeds the timeout period \(T\), and request \(n+1\) is transmitted within the time window \([T,T+\delta t]\), the response \(n\) may be incorrectly attributed to request \(n+1\). Assuming the request interval is a fixed period \(\tau\), the probability of erroneous matching is given by  
\[P_{\text{error}}=P_{\text{delay}}\cdot\frac{\Delta t}{\tau}\overset{\Delta t \sim U(T,T+\tau)}{\approx}\frac{1}{2}P_{\text{delay}} \tag{12}\]  
Assume each request can be retransmitted up to \(N\) times with a retransmission interval of \(T\) seconds, where retransmitted requests carry identical payloads to the original request but lack Transaction ID. The response to the \(k\)-th retransmission may be erroneously matched to any subsequent request by the error-prone mechanism. Let \(P_{\text{delay}}\) denote the probability that the response delay exceeds \(T\) for both the original request and each retransmission. The total erroneous matching probability \(P_{\text{error\_total}}\) is then given by  
\[P_{\text{error\_total}}=\sum_{k=0}^{N}P_{\text{delay}}^{k+1}\cdot\frac{1}{2}= \frac{1}{2}P_{\text{delay}}\frac{1-P_{\text{delay}}^{R+1}}{1-P_{\text{delay}}} \tag{13}\]  
Figure 12: Surface plot of the effect of \(\beta\) and \(\gamma\) on \(\pi_{2}\). When Transaction IDs are implemented, the correct matching criteria are defined as follows: (1) Each request is assigned a unique Transaction ID, and a response must carry the matching Transaction ID to be accepted. (2) Let the bit length of the Transaction ID be \(b\), then the ID space size is \(R=2_{b}\). (3) Erroneous matching occurs only if the response Transaction ID coincidentally matches the currently pending request ID. Assuming the ID collision probability caused by malicious attacks or coincidental matches is \(\frac{1}{R}\), the error probability per individual request becomes  
\[P_{\text{error\_id}}=\frac{1}{R} \tag{14}\]  
For \(N\) retransmissions, the total error probability is expressed as  
\[P_{\text{error\_total}}=\sum_{k=0}^{N}P_{\text{delay}}^{k+1}\cdot\frac{1}{2}= \frac{1}{2}P_{\text{delay}}\frac{1-P_{\text{delay}}^{R+1}}{1-P_{\text{delay}}} \tag{15}\]  
When \(\frac{1}{R}\ll 1\), Equation (17) can be approximated using the [PERSON] expansion formula:  
\[P_{\text{error\_total\_id}}\approx\frac{N+1}{R} \tag{16}\]  
For \(b=16\) and \(R=2^{16}\), assuming (\(P_{delay}=0.1\)), substituting the parameters into Equations (13) and (16) yields a total erroneous matching probability of \(P_{\text{error\_total}}\approx 5.55\%\) without Transaction ID, while the implementation of Transaction ID reduces this probability to \(P_{\text{error\_total\_id}}\approx 0.0061\%\). The significant disparity between \(P_{\text{error\_total}}\) and \(P_{\text{error\_total\_id}}\) (\(P_{\text{error\_total}}\gg P_{\text{error\_total\_id}}\)) demonstrates that embedding a Transaction ID matching mechanism at the application layer over UDP is essential to enhance transmission reliability, even with timeout-based retransmissions already in place. According to the above derivation, a 16-bit Transaction ID is assigned to each packet to match the request and response, and the request sent by the upper computer is resent if it does not receive the uplink message from the slave device within the specified time to prevent the loss of the request frame.