-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserial_write_sim.py
36 lines (32 loc) · 1.15 KB
/
serial_write_sim.py
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
#!/usr/bin/python
import serial
import time
import random
import os
#sdev = open("/dev/ttyUSB0", "r")
serial_dev = os.getenv("HOST_DEV1")
if serial_dev is None:
print "Location of serial device file is not provided in the env context.."
serial_dev="/dev/ttyUSB0"
sdev = serial.Serial(port=serial_dev, baudrate=9600)
sdev.bytesize = serial.EIGHTBITS #number of bits per bytes
sdev.parity = serial.PARITY_NONE #set parity check: no parity
sdev.stopbits = serial.STOPBITS_ONE #number of stop bits
sdev.timeout = 5
print "Serial: %s\n" % sdev
while True:
# send the character to the device
# (note that I happend a \r\n carriage return and line feed to the characters - this is requested by my device)
temp = random.uniform(20.0,50.0)
temp=round(temp, 2)
humidity=random.randint(30,100)
humidity=round(humidity,2)
pressure=random.uniform(14.0, 20.0)
pressure=round(pressure,2)
out = ''
out=str(temp) + "," + str(humidity) + "," + str(pressure)
print "Writing: %s\n" % out
sdev.write(out + '\r\n')
# let's wait 3 second before reading output (let's give device time to answer)
time.sleep(5)
sdev.close()