Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 KB | None | 0 0
  1. import serial
  2. import sys
  3. import argparse
  4.  
  5. parser = argparse.ArgumentParser()
  6. parser.add_argument("-a", help="numer of cell", type=str)
  7. parser.add_argument("-d", help="data", type=str)
  8. parser.add_argument("-s", help="speed", type=int)
  9. parser.add_argument("-p", help="port", type=str)
  10. parser.add_argument("rw", type=str)
  11. args = parser.parse_args()
  12.  
  13. def ipToByte(ip):
  14.     splitIp = ip.split('.')
  15.     res = ''
  16.     for num in splitIp:
  17.         tmp = hex(int(num))
  18.         if len(tmp) == 3:
  19.             num = '0' + tmp[-1]
  20.         else:
  21.             num = tmp[-2:]
  22.         res +=num
  23.     return res
  24.  
  25. command = None
  26. ser = serial.Serial(args.p, args.s)
  27. if args.rw == "write":
  28.     command = 'S0' + args.a
  29.     if args.a == '2':
  30.         ip = ipToByte(args.d)
  31.         command += ip
  32.     elif args.a == '3':
  33.         command += args.d
  34.     else:
  35.         sys.stdout.write("Have not permission to write\n")
  36.         sys.exit(1)
  37.     ser.write(command)
  38.     ser.close()
  39. elif args.rw == "read":
  40.     command = 'G0' + args.a
  41.     numBytes = 0
  42.     if args.a == '1' or args.a == '0':
  43.         numBytes = 4
  44.     elif args.a == '0':
  45.         numBytes = 8
  46.     else:
  47.         numBytes = 32
  48.     ser.write(command)
  49.     inf = ser.read(numBytes)
  50.     ser.close()
  51.     print inf
  52. else:
  53.     sys.stdout.write("Wrong command\n")
  54.     sys.exit(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement