Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import serial
- import sys
- import argparse
- parser = argparse.ArgumentParser()
- parser.add_argument("-a", help="numer of cell", type=str)
- parser.add_argument("-d", help="data", type=str)
- parser.add_argument("-s", help="speed", type=int)
- parser.add_argument("-p", help="port", type=str)
- parser.add_argument("rw", type=str)
- args = parser.parse_args()
- def ipToByte(ip):
- splitIp = ip.split('.')
- res = ''
- for num in splitIp:
- tmp = hex(int(num))
- if len(tmp) == 3:
- num = '0' + tmp[-1]
- else:
- num = tmp[-2:]
- res +=num
- return res
- command = None
- ser = serial.Serial(args.p, args.s)
- if args.rw == "write":
- command = 'S0' + args.a
- if args.a == '2':
- ip = ipToByte(args.d)
- command += ip
- elif args.a == '3':
- command += args.d
- else:
- sys.stdout.write("Have not permission to write\n")
- sys.exit(1)
- ser.write(command)
- ser.close()
- elif args.rw == "read":
- command = 'G0' + args.a
- numBytes = 0
- if args.a == '1' or args.a == '0':
- numBytes = 4
- elif args.a == '0':
- numBytes = 8
- else:
- numBytes = 32
- ser.write(command)
- inf = ser.read(numBytes)
- ser.close()
- print inf
- else:
- sys.stdout.write("Wrong command\n")
- sys.exit(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement