Commit 63622290 authored by rtn's avatar rtn

now recoded the serial comms bit as a function

parent 50700f21
......@@ -6,105 +6,102 @@ import pprint
import sys
from colorconsole import terminal
# tty open now coded as a function
#
# message data now here
def page(ttydev,capcode,function,repeats,message):
#
capcode = 20008
function = 1
repeats = 2
# message = b'0123456789----------0123456789----------'
message = b'rtn: miauw naar de maan!<<<pagertest>>>'
#
# initialise the screen
# create msg bytearray (NB: there should be more error
# checking here
#
msg=(b'P ')
msg+=(b'%07d' % capcode)+(b' ')
msg+=(b'%01d' % function)+(b' ')
msg+=(b'%01d' % repeats)+(b' ')
msg+=(message)+(b'\r')
screen = terminal.get_terminal(conEmu=False)
screen.clear()
screen.xterm256_set_fg_color(108)
screen.xterm256_set_bk_color(8)
screen.print_at(0,0,"rtn telecom : pagertest 0.10")
screen.xterm256_set_bk_color(0)
screen.xterm256_set_fg_color(7)
screen.gotoXY(0,5)
#
# tty to open as string rather than hardcoded)
ttydev="/dev/ttyACM0"
#
print ("attempting to open tty:",ttydev)
print ("\n")
# serial port open with error checking this time
print ("attempting to open tty:",ttydev)
try:
# serial port open with error checking this time
try:
ser = serial.Serial(ttydev, timeout=3)
except serial.SerialException:
except serial.SerialException:
screen.reset_colors()
sys.exit("** ERROR: cannot open tty\n\n")
#if we got here everything is OK
# now flip RTS to reset the Arduino again
ser.setRTS(True)
ser.setRTS(False)
# get the lines from the buffer
screen.xterm256_set_fg_color(0x0b)
screen.xterm256_set_bk_color(0x15)
print ("** tty open OK: Connecting to Arduino and checking TX startup**")
screen.xterm256_set_fg_color(0xbe)
screen.xterm256_set_bk_color(0)
print ("\n")
ibuf = ser.readlines()
pprint.pprint(ibuf)
# check to see what came down comms line is expected
if ibuf[0] != (b'RF22 init OK\r\n'):
#if we got here everything is OK
# now flip RTS to reset the Arduino again
ser.setRTS(True)
ser.setRTS(False)
# get the lines from the buffer
screen.xterm256_set_fg_color(0x0b)
screen.xterm256_set_bk_color(0x15)
print ("** tty open OK: Connecting to Arduino and checking TX startup**")
screen.xterm256_set_fg_color(0xbe)
screen.xterm256_set_bk_color(0)
print ("\n")
ibuf = ser.readlines()
pprint.pprint(ibuf)
# check to see what came down comms line is expected
if ibuf[0] != (b'RF22 init OK\r\n'):
screen.reset_colors()
sys.exit("** ERROR: RF22 init error\n\n")
if ibuf[-1] != (b'F <freqmhz> <freq100Hz>\r\n'):
if ibuf[-1] != (b'F <freqmhz> <freq100Hz>\r\n'):
screen.reset_colors()
sys.exit("** ERROR: RF22 comms error\n\n")
screen.xterm256_set_fg_color(0x28)
# create message to send down comms line (warning, still no error checking!)
# msg = (b'P 0020008 1 2 0123456789*****-----01234567899876543210\r')
msg=(b'P ')
msg+=(b'%07d' % capcode)+(b' ')
msg+=(b'%01d' % function)+(b' ')
msg+=(b'%01d' % repeats)+(b' ')
msg+=(message)+(b'\r')
#import pdb; pdb.set_trace()
# now send message
ser.write(msg)
# get a line back from TTY, it should be exactly same as msg
# but with a \n after it
# if it hasn't something has gone wrong
check=ser.readline()
screen.xterm256_set_fg_color(0x50)
screen.xterm256_set_bk_color(0x15)
print('<< from tty:',check)
if check != (msg+b'\n'):
# now send message
ser.write(msg)
# get a line back from TTY, it should be exactly same as msg
# but with a \n after it
# if it hasn't something has gone wrong
check=ser.readline()
screen.xterm256_set_fg_color(0x50)
screen.xterm256_set_bk_color(0x15)
print('<< from tty:',check)
if check != (msg+b'\n'):
screen.reset_colors()
sys.exit("** ERROR: pager comms error\n\n")
screen.xterm256_set_bk_color(0)
screen.xterm256_set_fg_color(0x2d)
print("comms OK\n")
# get data returned over comms link
# and now echo it with some pretty colours :)
colindex=0x70
while True:
screen.xterm256_set_bk_color(0)
screen.xterm256_set_fg_color(0x2d)
print("comms OK\n")
# get data returned over comms link
# and now echo it with some pretty colours :)
colindex=0x70
while True:
data=ser.readline()
screen.xterm256_set_fg_color(colindex)
print('<< from tty:',data)
colindex += 1
if data==(b'F <freqmhz> <freq100Hz>\r\n'):
break
screen.xterm256_set_fg_color(0xbf)
print("\n**message sent correctly**\n")
screen.reset_colors()
screen.xterm256_set_fg_color(0xbf)
print("**message sent correctly**\n")
#
# main code now starts here
#
#
#
# initialise the screen
#
screen = terminal.get_terminal(conEmu=False)
screen.clear()
screen.xterm256_set_fg_color(108)
screen.xterm256_set_bk_color(8)
screen.print_at(0,0,"rtn telecom : pagertest 0.10")
screen.xterm256_set_bk_color(0)
screen.xterm256_set_fg_color(7)
screen.gotoXY(0,5)
screen.xterm256_set_fg_color(0x28)
#
# message now split into capcode, function (0-3), repeats
# and 40 character bytearray
#
capcode = 20008
function = 1
repeats = 2
message = b'rtn: miauw naar de maan!<<<pagertest>>>'
ttydev="/dev/ttyACM0"
page(ttydev,capcode,function,repeats,message)
screen.reset_colors()
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 to comment