Modifications

Sauter à la navigation Sauter à la recherche
448 octets ajoutés ,  30 octobre 2018 à 21:26
Ligne 250 : Ligne 250 :     
=== With Python ===
 
=== With Python ===
 +
The following Python script will capture a serial port (see {{fname|baud_rate}} variable) and write the content to a file (see {{fname|write_to_file_path}} variable).
 +
 +
The content of the file is reset when the script is started.
 +
 +
{{ambox-stop|text=Openning the Serial Port will issue the automatic Reset feature of the board.}}
 +
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
 
import serial
 
import serial
    
serial_port = '/dev/ttyACM0';
 
serial_port = '/dev/ttyACM0';
baud_rate = 9600; #In arduino, Serial.begin(baud_rate)
+
# depend on Serial.begin(baud_rate) in Arduino
 +
baud_rate = 115200;
 
write_to_file_path = "output.txt";
 
write_to_file_path = "output.txt";
   −
output_file = open(write_to_file_path, "w+");
+
output_file = open(write_to_file_path, "w+")
 
ser = serial.Serial(serial_port, baud_rate)
 
ser = serial.Serial(serial_port, baud_rate)
while True:
+
try:
    line = ser.readline();
+
    while True:
    line = line.decode("utf-8") #ser.readline returns a binary, convert to string
+
        line = ser.readline()
    print(line);
+
        line = line.encode("utf-8") #ser.readline returns a binary, convert to string
    output_file.write(line);
+
        print(line)
 +
        output_file.write(line)
 +
except KeyboardInterrupt:
 +
    print( 'User abord' )
 +
output_file.close()
 +
ser.close()
 
</syntaxhighlight>
 
</syntaxhighlight>
    
{{ENG-CANSAT-TRAILER}}
 
{{ENG-CANSAT-TRAILER}}
29 836

modifications

Menu de navigation