Commit 93e982cf by source_reader

initial commit

parents
desktop.ini
.idea
\ No newline at end of file
import serial
import json
from binascii import b2a_hex
# create Serial object
DEVICE = 'COM7'
ser = serial.Serial(DEVICE)
# Data to send to server (change this)
tx_obj = [8, 10, -12, 100, 2]
print("Sending %s to server" % type(tx_obj))
# L3: convert Python object into string
tx_str = json.dumps(tx_obj)
print("\tjson: %s" % tx_str)
# L2: convert string into binary data
tx_data = (tx_str + '\n').encode('ascii')
print("\tbinary: %r" % b2a_hex(tx_data))
# L1: tell operating system to send data
nbytes = ser.write(tx_data)
ser.flush()
# -------- SERVER PROCESSES MESSAGE --------
print("waiting...")
# L0: hardware listens for voltage changes on RX line
# L1: operating system delivers binary data
rx_data = ser.readline()
print("\tbinary: %r" % b2a_hex(rx_data))
# L2: convert binary data into a string
rx_str = rx_data.decode('ascii')
print("\tjson: %s" % rx_str.rstrip())
# L3: convert string into Python object
rx_obj = json.loads(rx_str)
print("Received %s from server" % type(rx_obj))
# display result (is it correct?)
print("Max: %d | Min %d" % (rx_obj['max'], rx_obj['min']))
# close connection
ser.close()
\ No newline at end of file
import serial
import json
DEVICE = 'COM7'
ser = serial.Serial(DEVICE)
tx_obj = [8, 10, -12, 100, 2]
print(tx_obj)
tx_str = json.dumps(tx_obj)
tx_data = (tx_str + '\n').encode('ascii')
ser.write(tx_data)
ser.flush()
rx_data = ser.readline()
rx_str = rx_data.decode('ascii')
rx_obj = json.loads(rx_str)
print("Max: %d | Min %d" % (rx_obj['max'], rx_obj['min']))
ser.close()
import serial
import json
from binascii import b2a_hex
# create Serial object
DEVICE = 'COM6'
ser = serial.Serial(DEVICE)
print("waiting...")
# L0: hardware listens for voltage changes on RX line
# L1: operating system delivers binary data
rx_data = ser.readline()
print("\tbinary %r" % b2a_hex(rx_data))
# L2: convert binary data into a string
rx_str = rx_data.decode('ascii')
print("\tjson: %s" % rx_str.rstrip())
# L3: convert string into Python data structure
rx_obj = json.loads(rx_str)
print("Received %s from client" % type(rx_obj))
tx_obj = {'max': max(rx_obj),
'min': min(rx_obj)}
input("Press [enter] to send response")
print("Sending %s to client" % type(tx_obj))
# L3: convert Python data structure into string
tx_str = json.dumps(tx_obj)
print("\tjson: %s" % tx_obj)
# L2: convert string into binary data
tx_data = (tx_str + '\n').encode('ascii')
print("\tbinary: %r" % b2a_hex(tx_data))
# L1: tell operating system to send data
ser.write(tx_data)
ser.flush()
# L0: serial hardware produces voltage waveform on TX line
# close connection
ser.close()
import serial
import json
DEVICE = 'COM6'
ser = serial.Serial(DEVICE)
print("waiting...")
rx_data = ser.readline()
rx_str = rx_data.decode('ascii')
rx_obj = json.loads(rx_str)
tx_obj = {'max': max(rx_obj), 'min': min(rx_obj)}
tx_str = json.dumps(tx_obj)
tx_data = (tx_str + '\n').encode('ascii')
ser.write(tx_data)
ser.flush()
ser.close()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment