Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
R
Raumfahrt
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ida Reinfried
Raumfahrt
Commits
95d80956
Commit
95d80956
authored
5 months ago
by
Kateryna Makarova
Browse files
Options
Downloads
Patches
Plain Diff
Update Sender_Encoder.ino
parent
c99ca3ad
Branches
main
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Sender_Encoder.ino
+43
-14
43 additions, 14 deletions
Sender_Encoder.ino
with
43 additions
and
14 deletions
Sender_Encoder.ino
+
43
−
14
View file @
95d80956
...
@@ -5,11 +5,13 @@ The primary board (Board 1) sends data to a secondary board (Board 2) via serial
...
@@ -5,11 +5,13 @@ The primary board (Board 1) sends data to a secondary board (Board 2) via serial
const
int
ledPin
=
PE1
;
// LED pin to indicate data sent (Blink Yellow)
const
int
ledPin
=
PE1
;
// LED pin to indicate data sent (Blink Yellow)
HardwareSerial
mySerial
(
D0
,
D1
);
// RX = D0, TX = D1
HardwareSerial
mySerial
(
D0
,
D1
);
// RX = D0, TX = D1
const
int
maxRetransmissions
=
3
;
// Maximum number of retransmissions
const
unsigned
long
responseTimeout
=
2000
;
// Timeout in milliseconds
void
setup
()
{
void
setup
()
{
pinMode
(
ledPin
,
OUTPUT
);
// Set LED pin as output
pinMode
(
ledPin
,
OUTPUT
);
// Set LED pin as output
Serial
.
begin
(
9600
);
// Communication with PC
Serial
.
begin
(
9600
);
// Communication with PC
mySerial
.
begin
(
9600
);
// Communication with Board 2 via USART
mySerial
.
begin
(
9600
);
// Communication with Board 2 via USART
}
}
void
loop
()
{
void
loop
()
{
...
@@ -23,24 +25,51 @@ void loop() {
...
@@ -23,24 +25,51 @@ void loop() {
digitalWrite
(
ledPin
,
HIGH
);
digitalWrite
(
ledPin
,
HIGH
);
delay
(
1000
);
// LED on for 1000ms
delay
(
1000
);
// LED on for 1000ms
digitalWrite
(
ledPin
,
LOW
);
digitalWrite
(
ledPin
,
LOW
);
delay
(
2000
);
// Wait 2 second before sending the next message
// Wait for the acknowledgment or error message
if
(
!
waitForResponse
(
encodedMessage
))
{
Serial
.
println
(
"Error: No response from Board 2, reached retransmission limit."
);
}
}
}
}
if
(
mySerial
.
available
()
>
0
)
{
String
receivedData
=
mySerial
.
readStringUntil
(
'\n'
);
// Read response from Board 2
bool
waitForResponse
(
String
lastMessage
)
{
Serial
.
println
(
"Received from Board 2: "
+
receivedData
);
int
retransmissionCount
=
0
;
unsigned
long
startTime
=
millis
();
if
(
receivedData
.
startsWith
(
"Error"
))
{
// Handle retransmission if necessary
while
(
retransmissionCount
<
maxRetransmissions
)
{
String
lastMessage
=
receivedData
.
substring
(
receivedData
.
indexOf
(
':'
)
+
1
);
if
(
mySerial
.
available
()
>
0
)
{
lastMessage
.
trim
();
// Remove leading and trailing whitespace
String
receivedData
=
mySerial
.
readStringUntil
(
'\n'
);
// Read response from Board 2
Serial
.
println
(
"Received from Board 2: "
+
receivedData
);
if
(
receivedData
.
startsWith
(
"Error"
))
{
// Handle retransmission if necessary
retransmissionCount
++
;
Serial
.
println
(
"Error detected. Retransmitting..."
);
sendMessageToBoard2
(
lastMessage
);
delay
(
1000
);
// Delay before retransmitting
}
else
{
// Valid response received, no need to retransmit
return
true
;
}
}
// Timeout check
if
(
millis
()
-
startTime
>
responseTimeout
)
{
Serial
.
println
(
"Response timeout. Retrying..."
);
retransmissionCount
++
;
sendMessageToBoard2
(
lastMessage
);
sendMessageToBoard2
(
lastMessage
);
delay
(
1000
);
// Delay before retransmitting
startTime
=
millis
();
// Reset timeout counter
}
}
}
}
// Retransmission limit reached
return
false
;
}
}
void
sendMessageToBoard2
(
String
message
)
{
void
sendMessageToBoard2
(
String
message
)
{
mySerial
.
println
(
message
);
// Send message to Board 2
mySerial
.
println
(
message
);
// Send message to Board 2
}
}
String
encodeData
(
String
data
)
{
String
encodeData
(
String
data
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment