Skip to content

Commit 47c3774

Browse files
authored
Merge pull request #6 from Ddoiron-cidco/master
sonar simulator update with test unit
2 parents 33fe0a1 + 55dd087 commit 47c3774

File tree

7 files changed

+121
-0
lines changed

7 files changed

+121
-0
lines changed

Install/install.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
sudo apt install python3 python-is-python3 python3-pip -y
2+
3+
sudo bash -c 'cat << EOF3 > /etc/systemd/system/nmea.service
4+
[Unit]
5+
Description=Launch NMEA Simulator on boot.
6+
7+
[Service]
8+
Type=simple
9+
ExecStart=/home/ubuntu/SonarSimulator/Script/autolaunch.sh
10+
11+
[Install]
12+
WantedBy=multi-user.target
13+
EOF3'
14+
15+
sudo chmod 755 /etc/systemd/system/nmea.service
16+
sudo systemctl enable nmea
17+
sudo systemctl start nmea
18+
sudo systemctl status nmea
19+

Script/DPT_unittest.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import unittest
2+
from unittest.mock import Mock, patch
3+
from io import StringIO
4+
5+
# Importez les fonctions du script principal ici
6+
from sonar_simulator_DPT import generate_nmea_dpt, checksum, main
7+
8+
class TestNMEAGenerator(unittest.TestCase):
9+
10+
def test_generate_nmea_dpt(self):
11+
depth = 15.5
12+
expected_output = "$SDDPT,15.5,0.1,100*7A"
13+
self.assertEqual(generate_nmea_dpt(depth), expected_output)
14+
15+
def test_checksum(self):
16+
sentence = "SDDPT,15.5,0.1,100"
17+
expected_checksum = "$SDDPT,15.5,0.1,100*7A"
18+
self.assertEqual(checksum(sentence), expected_checksum)
19+
20+
@patch('serial.Serial')
21+
@patch('time.time', side_effect=[0, 1, 2, 3]) # Pour contrôler le temps dans les tests
22+
def test_main(self, mock_time, mock_serial):
23+
# Créez un faux objet Serial pour les tests
24+
mock_ser = Mock()
25+
mock_serial.return_value = mock_ser
26+
27+
# Utilisez un StringIO pour capturer la sortie imprimée
28+
output = StringIO()
29+
with patch('sys.argv', ['script_principal.py', 'test_port_serie', '9600']), \
30+
patch('sys.stdout', output):
31+
main('test_port_serie', 9600)
32+
33+
# Vérifiez que la fonction Serial a été appelée avec les bons arguments
34+
mock_serial.assert_called_once_with('test_port_serie', 9600, timeout=1)
35+
# Vérifiez que la phrase NMEA a été correctement générée et envoyée
36+
expected_nmea_sentences = [
37+
"$SDDPT,10.0,0.1,100*7A\r\n",
38+
"$SDDPT,11.0,0.1,100*7B\r\n",
39+
"$SDDPT,12.0,0.1,100*7C\r\n",
40+
"$SDDPT,13.0,0.1,100*7D\r\n"
41+
]
42+
self.assertEqual(output.getvalue(), ''.join(expected_nmea_sentences))
43+
44+
if __name__ == '__main__':
45+
unittest.main()
46+
1.68 KB
Binary file not shown.
1.4 KB
Binary file not shown.

Script/autolaunch.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
python /home/ubuntu/SonarSimulator/Script/sonar_simulator_DPT.py /dev/ttyUSB0 9600

Script/sonar_simulator_DPT.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import serial
2+
import math
3+
import datetime
4+
import time
5+
6+
def generate_nmea_dpt(depth):
7+
# Génère une phrase NMEA DPT avec la profondeur spécifiée
8+
return checksum(f"SDDPT,{depth:.1f},0.1,100")
9+
10+
def checksum(sentence):
11+
# Calcule le checksum NMEA
12+
checksum_value = 0
13+
for char in sentence:
14+
checksum_value ^= ord(char)
15+
return f'${sentence}*{hex(checksum_value)[2:].upper().zfill(2)}'
16+
17+
def main(serial_port, baud_rate):
18+
try:
19+
ser = serial.Serial(serial_port, baud_rate, timeout=1)
20+
print(f"Connecté au port série {serial_port} à {baud_rate} baud.")
21+
22+
while True:
23+
# Génération de la valeur de profondeur en sinusoidale entre 10m et 60m
24+
current_time = datetime.datetime.now()
25+
depth = 10 + int(current_time.strftime('%S'))
26+
27+
# Envoi de la phrase NMEA DPT sur le port série
28+
nmea_sentence = generate_nmea_dpt(depth)
29+
ser.write((nmea_sentence + '\r\n').encode())
30+
print(nmea_sentence)
31+
32+
time.sleep(1) # Attendez une seconde avant de générer la prochaine valeur de profondeur
33+
34+
except KeyboardInterrupt:
35+
print("Arrêt du script.")
36+
except Exception as e:
37+
print(f"Une erreur s'est produite : {e}")
38+
finally:
39+
ser.close()
40+
41+
if __name__ == "__main__":
42+
import sys
43+
44+
if len(sys.argv) != 3:
45+
print("Utilisation : python nmea_generator.py <port_serie> <vitesse>")
46+
sys.exit(1)
47+
48+
serial_port = sys.argv[1]
49+
baud_rate = int(sys.argv[2])
50+
51+
main(serial_port, baud_rate)
52+

Script/unittest.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python3 -m unittest DPT_unittest.py

0 commit comments

Comments
 (0)