pyboard.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. #!/usr/bin/env python
  2. #
  3. # This file is part of the MicroPython project, http://micropython.org/
  4. #
  5. # The MIT License (MIT)
  6. #
  7. # Copyright (c) 2014-2016 Damien P. George
  8. # Copyright (c) 2017 Paul Sokolovsky
  9. #
  10. # Permission is hereby granted, free of charge, to any person obtaining a copy
  11. # of this software and associated documentation files (the "Software"), to deal
  12. # in the Software without restriction, including without limitation the rights
  13. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. # copies of the Software, and to permit persons to whom the Software is
  15. # furnished to do so, subject to the following conditions:
  16. #
  17. # The above copyright notice and this permission notice shall be included in
  18. # all copies or substantial portions of the Software.
  19. #
  20. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. # THE SOFTWARE.
  27. """
  28. pyboard interface
  29. This module provides the Pyboard class, used to communicate with and
  30. control a MicroPython device over a communication channel. Both real
  31. boards and emulated devices (e.g. running in QEMU) are supported.
  32. Various communication channels are supported, including a serial
  33. connection, telnet-style network connection, external process
  34. connection.
  35. Example usage:
  36. import pyboard
  37. pyb = pyboard.Pyboard('/dev/ttyACM0')
  38. Or:
  39. pyb = pyboard.Pyboard('192.168.1.1')
  40. Then:
  41. pyb.enter_raw_repl()
  42. pyb.exec('pyb.LED(1).on()')
  43. pyb.exit_raw_repl()
  44. Note: if using Python2 then pyb.exec must be written as pyb.exec_.
  45. To run a script from the local machine on the board and print out the results:
  46. import pyboard
  47. pyboard.execfile('test.py', device='/dev/ttyACM0')
  48. This script can also be run directly. To execute a local script, use:
  49. ./pyboard.py test.py
  50. Or:
  51. python pyboard.py test.py
  52. """
  53. import sys
  54. import time
  55. import os
  56. try:
  57. stdout = sys.stdout.buffer
  58. except AttributeError:
  59. # Python2 doesn't have buffer attr
  60. stdout = sys.stdout
  61. def stdout_write_bytes(b):
  62. b = b.replace(b"\x04", b"")
  63. stdout.write(b)
  64. stdout.flush()
  65. class PyboardError(BaseException):
  66. pass
  67. class TelnetToSerial:
  68. def __init__(self, ip, user, password, read_timeout=None):
  69. import telnetlib
  70. self.tn = telnetlib.Telnet(ip, timeout=15)
  71. self.read_timeout = read_timeout
  72. if b'Login as:' in self.tn.read_until(b'Login as:', timeout=read_timeout):
  73. self.tn.write(bytes(user, 'ascii') + b"\r\n")
  74. if b'Password:' in self.tn.read_until(b'Password:', timeout=read_timeout):
  75. # needed because of internal implementation details of the telnet server
  76. time.sleep(0.2)
  77. self.tn.write(bytes(password, 'ascii') + b"\r\n")
  78. if b'for more information.' in self.tn.read_until(b'Type "help()" for more information.', timeout=read_timeout):
  79. # login successful
  80. from collections import deque
  81. self.fifo = deque()
  82. return
  83. raise PyboardError('Failed to establish a telnet connection with the board')
  84. def __del__(self):
  85. self.close()
  86. def close(self):
  87. try:
  88. self.tn.close()
  89. except:
  90. # the telnet object might not exist yet, so ignore this one
  91. pass
  92. def read(self, size=1):
  93. while len(self.fifo) < size:
  94. timeout_count = 0
  95. data = self.tn.read_eager()
  96. if len(data):
  97. self.fifo.extend(data)
  98. timeout_count = 0
  99. else:
  100. time.sleep(0.25)
  101. if self.read_timeout is not None and timeout_count > 4 * self.read_timeout:
  102. break
  103. timeout_count += 1
  104. data = b''
  105. while len(data) < size and len(self.fifo) > 0:
  106. data += bytes([self.fifo.popleft()])
  107. return data
  108. def write(self, data):
  109. self.tn.write(data)
  110. return len(data)
  111. def inWaiting(self):
  112. n_waiting = len(self.fifo)
  113. if not n_waiting:
  114. data = self.tn.read_eager()
  115. self.fifo.extend(data)
  116. return len(data)
  117. else:
  118. return n_waiting
  119. class ProcessToSerial:
  120. "Execute a process and emulate serial connection using its stdin/stdout."
  121. def __init__(self, cmd):
  122. import subprocess
  123. self.subp = subprocess.Popen(cmd.split(), bufsize=0, shell=True, preexec_fn=os.setsid,
  124. stdin=subprocess.PIPE, stdout=subprocess.PIPE)
  125. # Initially was implemented with selectors, but that adds Python3
  126. # dependency. However, there can be race conditions communicating
  127. # with a particular child process (like QEMU), and selectors may
  128. # still work better in that case, so left inplace for now.
  129. #
  130. #import selectors
  131. #self.sel = selectors.DefaultSelector()
  132. #self.sel.register(self.subp.stdout, selectors.EVENT_READ)
  133. import select
  134. self.poll = select.poll()
  135. self.poll.register(self.subp.stdout.fileno())
  136. def close(self):
  137. import signal
  138. os.killpg(os.getpgid(self.subp.pid), signal.SIGTERM)
  139. def read(self, size=1):
  140. data = b""
  141. while len(data) < size:
  142. data += self.subp.stdout.read(size - len(data))
  143. return data
  144. def write(self, data):
  145. self.subp.stdin.write(data)
  146. return len(data)
  147. def inWaiting(self):
  148. #res = self.sel.select(0)
  149. res = self.poll.poll(0)
  150. if res:
  151. return 1
  152. return 0
  153. class ProcessPtyToTerminal:
  154. """Execute a process which creates a PTY and prints slave PTY as
  155. first line of its output, and emulate serial connection using
  156. this PTY."""
  157. def __init__(self, cmd):
  158. import subprocess
  159. import re
  160. import serial
  161. self.subp = subprocess.Popen(cmd.split(), bufsize=0, shell=False, preexec_fn=os.setsid,
  162. stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  163. pty_line = self.subp.stderr.readline().decode("utf-8")
  164. m = re.search(r"/dev/pts/[0-9]+", pty_line)
  165. if not m:
  166. print("Error: unable to find PTY device in startup line:", pty_line)
  167. self.close()
  168. sys.exit(1)
  169. pty = m.group()
  170. # rtscts, dsrdtr params are to workaround pyserial bug:
  171. # http://stackoverflow.com/questions/34831131/pyserial-does-not-play-well-with-virtual-port
  172. self.ser = serial.Serial(pty, interCharTimeout=1, rtscts=True, dsrdtr=True)
  173. def close(self):
  174. import signal
  175. os.killpg(os.getpgid(self.subp.pid), signal.SIGTERM)
  176. def read(self, size=1):
  177. return self.ser.read(size)
  178. def write(self, data):
  179. return self.ser.write(data)
  180. def inWaiting(self):
  181. return self.ser.inWaiting()
  182. class Pyboard:
  183. def __init__(self, device, baudrate=115200, user='micro', password='python', wait=0):
  184. if device.startswith("exec:"):
  185. self.serial = ProcessToSerial(device[len("exec:"):])
  186. elif device.startswith("execpty:"):
  187. self.serial = ProcessPtyToTerminal(device[len("qemupty:"):])
  188. elif device and device[0].isdigit() and device[-1].isdigit() and device.count('.') == 3:
  189. # device looks like an IP address
  190. self.serial = TelnetToSerial(device, user, password, read_timeout=10)
  191. else:
  192. import serial
  193. delayed = False
  194. for attempt in range(wait + 1):
  195. try:
  196. self.serial = serial.Serial(device, baudrate=baudrate, interCharTimeout=1)
  197. break
  198. except (OSError, IOError): # Py2 and Py3 have different errors
  199. if wait == 0:
  200. continue
  201. if attempt == 0:
  202. sys.stdout.write('Waiting {} seconds for pyboard '.format(wait))
  203. delayed = True
  204. time.sleep(1)
  205. sys.stdout.write('.')
  206. sys.stdout.flush()
  207. else:
  208. if delayed:
  209. print('')
  210. raise PyboardError('failed to access ' + device)
  211. if delayed:
  212. print('')
  213. def close(self):
  214. self.serial.close()
  215. def read_until(self, min_num_bytes, ending, timeout=10, data_consumer=None):
  216. data = self.serial.read(min_num_bytes)
  217. if data_consumer:
  218. data_consumer(data)
  219. timeout_count = 0
  220. while True:
  221. if data.endswith(ending):
  222. break
  223. elif self.serial.inWaiting() > 0:
  224. new_data = self.serial.read(1)
  225. data = data + new_data
  226. if data_consumer:
  227. data_consumer(new_data)
  228. timeout_count = 0
  229. else:
  230. timeout_count += 1
  231. if timeout is not None and timeout_count >= 100 * timeout:
  232. break
  233. time.sleep(0.01)
  234. return data
  235. def enter_raw_repl(self):
  236. self.serial.write(b'\r\x03\x03') # ctrl-C twice: interrupt any running program
  237. # flush input (without relying on serial.flushInput())
  238. n = self.serial.inWaiting()
  239. while n > 0:
  240. self.serial.read(n)
  241. n = self.serial.inWaiting()
  242. self.serial.write(b'\r\x01') # ctrl-A: enter raw REPL
  243. data = self.read_until(1, b'raw REPL; CTRL-B to exit\r\n>')
  244. if not data.endswith(b'raw REPL; CTRL-B to exit\r\n>'):
  245. print(data)
  246. raise PyboardError('could not enter raw repl')
  247. self.serial.write(b'\x04') # ctrl-D: soft reset
  248. data = self.read_until(1, b'soft reboot\r\n')
  249. if not data.endswith(b'soft reboot\r\n'):
  250. print(data)
  251. raise PyboardError('could not enter raw repl')
  252. # By splitting this into 2 reads, it allows boot.py to print stuff,
  253. # which will show up after the soft reboot and before the raw REPL.
  254. data = self.read_until(1, b'raw REPL; CTRL-B to exit\r\n')
  255. if not data.endswith(b'raw REPL; CTRL-B to exit\r\n'):
  256. print(data)
  257. raise PyboardError('could not enter raw repl')
  258. def exit_raw_repl(self):
  259. self.serial.write(b'\r\x02') # ctrl-B: enter friendly REPL
  260. def follow(self, timeout, data_consumer=None):
  261. # wait for normal output
  262. data = self.read_until(1, b'\x04', timeout=timeout, data_consumer=data_consumer)
  263. if not data.endswith(b'\x04'):
  264. raise PyboardError('timeout waiting for first EOF reception')
  265. data = data[:-1]
  266. # wait for error output
  267. data_err = self.read_until(1, b'\x04', timeout=timeout)
  268. if not data_err.endswith(b'\x04'):
  269. raise PyboardError('timeout waiting for second EOF reception')
  270. data_err = data_err[:-1]
  271. # return normal and error output
  272. return data, data_err
  273. def exec_raw_no_follow(self, command):
  274. if isinstance(command, bytes):
  275. command_bytes = command
  276. else:
  277. command_bytes = bytes(command, encoding='utf8')
  278. # check we have a prompt
  279. data = self.read_until(1, b'>')
  280. if not data.endswith(b'>'):
  281. raise PyboardError('could not enter raw repl')
  282. # write command
  283. for i in range(0, len(command_bytes), 256):
  284. self.serial.write(command_bytes[i:min(i + 256, len(command_bytes))])
  285. time.sleep(0.01)
  286. self.serial.write(b'\x04')
  287. # check if we could exec command
  288. data = self.serial.read(2)
  289. if data != b'OK':
  290. raise PyboardError('could not exec command (response: %r)' % data)
  291. def exec_raw(self, command, timeout=10, data_consumer=None):
  292. self.exec_raw_no_follow(command);
  293. return self.follow(timeout, data_consumer)
  294. def eval(self, expression):
  295. ret = self.exec_('print({})'.format(expression))
  296. ret = ret.strip()
  297. return ret
  298. def exec_(self, command):
  299. ret, ret_err = self.exec_raw(command)
  300. if ret_err:
  301. raise PyboardError('exception', ret, ret_err)
  302. return ret
  303. def execfile(self, filename):
  304. with open(filename, 'rb') as f:
  305. pyfile = f.read()
  306. return self.exec_(pyfile)
  307. def get_time(self):
  308. t = str(self.eval('pyb.RTC().datetime()'), encoding='utf8')[1:-1].split(', ')
  309. return int(t[4]) * 3600 + int(t[5]) * 60 + int(t[6])
  310. # in Python2 exec is a keyword so one must use "exec_"
  311. # but for Python3 we want to provide the nicer version "exec"
  312. setattr(Pyboard, "exec", Pyboard.exec_)
  313. def execfile(filename, device='/dev/ttyACM0', baudrate=115200, user='micro', password='python'):
  314. pyb = Pyboard(device, baudrate, user, password)
  315. pyb.enter_raw_repl()
  316. output = pyb.execfile(filename)
  317. stdout_write_bytes(output)
  318. pyb.exit_raw_repl()
  319. pyb.close()
  320. def main():
  321. import argparse
  322. cmd_parser = argparse.ArgumentParser(description='Run scripts on the pyboard.')
  323. cmd_parser.add_argument('--device', default='/dev/ttyACM0', help='the serial device or the IP address of the pyboard')
  324. cmd_parser.add_argument('-b', '--baudrate', default=115200, help='the baud rate of the serial device')
  325. cmd_parser.add_argument('-u', '--user', default='micro', help='the telnet login username')
  326. cmd_parser.add_argument('-p', '--password', default='python', help='the telnet login password')
  327. cmd_parser.add_argument('-c', '--command', help='program passed in as string')
  328. cmd_parser.add_argument('-w', '--wait', default=0, type=int, help='seconds to wait for USB connected board to become available')
  329. cmd_parser.add_argument('--follow', action='store_true', help='follow the output after running the scripts [default if no scripts given]')
  330. cmd_parser.add_argument('files', nargs='*', help='input files')
  331. args = cmd_parser.parse_args()
  332. # open the connection to the pyboard
  333. try:
  334. pyb = Pyboard(args.device, args.baudrate, args.user, args.password, args.wait)
  335. except PyboardError as er:
  336. print(er)
  337. sys.exit(1)
  338. # run any command or file(s)
  339. if args.command is not None or len(args.files):
  340. # we must enter raw-REPL mode to execute commands
  341. # this will do a soft-reset of the board
  342. try:
  343. pyb.enter_raw_repl()
  344. except PyboardError as er:
  345. print(er)
  346. pyb.close()
  347. sys.exit(1)
  348. def execbuffer(buf):
  349. try:
  350. ret, ret_err = pyb.exec_raw(buf, timeout=None, data_consumer=stdout_write_bytes)
  351. except PyboardError as er:
  352. print(er)
  353. pyb.close()
  354. sys.exit(1)
  355. except KeyboardInterrupt:
  356. sys.exit(1)
  357. if ret_err:
  358. pyb.exit_raw_repl()
  359. pyb.close()
  360. stdout_write_bytes(ret_err)
  361. sys.exit(1)
  362. # run the command, if given
  363. if args.command is not None:
  364. execbuffer(args.command.encode('utf-8'))
  365. # run any files
  366. for filename in args.files:
  367. with open(filename, 'rb') as f:
  368. pyfile = f.read()
  369. execbuffer(pyfile)
  370. # exiting raw-REPL just drops to friendly-REPL mode
  371. pyb.exit_raw_repl()
  372. # if asked explicitly, or no files given, then follow the output
  373. if args.follow or (args.command is None and len(args.files) == 0):
  374. try:
  375. ret, ret_err = pyb.follow(timeout=None, data_consumer=stdout_write_bytes)
  376. except PyboardError as er:
  377. print(er)
  378. sys.exit(1)
  379. except KeyboardInterrupt:
  380. sys.exit(1)
  381. if ret_err:
  382. pyb.close()
  383. stdout_write_bytes(ret_err)
  384. sys.exit(1)
  385. # close the connection to the pyboard
  386. pyb.close()
  387. if __name__ == "__main__":
  388. main()