-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserial read.py
More file actions
42 lines (30 loc) · 1 KB
/
serial read.py
File metadata and controls
42 lines (30 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 13 13:53:17 2015
@author: Steph
This script creates a serial port on windows machines "com" at the
specified baud rate. It will loop over a command sent to the port
and print to a text file the full port response. It also tracks
the time between writes.
"""
import datetime
import time
import serial
#Open Serial Port -- update COM# and baud rate as needed
ser = serial.Serial('COM5', 115200)
start = time.time()
while ser.isOpen():
try:
f = open('your file name.txt', 'a') #Update output file name
ser.write("some text to serial port\r") #data to serial port
info = ser.read(ser.inWaiting()) #read from serial port
f.write('datetime: %s \n%s \n' %
(datetime.datetime.now(), info)) #write to text file
print info
time.sleep(10) #sleep until next write
end = time.time()
elapsed = end - start
print elapsed
except Exception, e:
print "error: " + str(e)
raise