Skip to content

Commit 1e5a9f1

Browse files
author
“Buğra
committed
Serial Connection Improved
1 parent 97476b6 commit 1e5a9f1

2 files changed

Lines changed: 80 additions & 88 deletions

File tree

MicroPython.sublime-settings

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//linux example: /dev/ttyUSBX
44
//windows example: COMXX
55
//macos example: /dev/tty.usbmodem*
6-
"port": "",
6+
"port": "/dev/ttyUSB0",
77

88
//type your local project full path
99
//we will copy files to there from

micropython.py

Lines changed: 79 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -5,68 +5,61 @@
55
from .modules.ampy import ampy, pyboard
66
from textwrap import indent
77
from os.path import basename
8+
from serial import SerialException
89

910

1011
class Settings(object):
1112
settings = sublime.load_settings("MicroPython.sublime-settings")
1213

13-
serial_conn = settings.get("port", False)
14+
port = settings.get("port", False)
1415
project_path = settings.get("project_path", None)
1516
notices = False if settings.get("notices") == "False" else True
1617

1718

1819
class MyBoard(Settings):
1920

20-
def __init__(self):
21-
self.port = self.serial_conn
22-
2321
def __enter__(self):
24-
self.connection = ampy.Files(pyboard.Pyboard(Settings.serial_conn))
25-
return self.connection
22+
return self
2623

2724
def __exit__(self, type, value, traceback):
28-
print("type", type)
29-
print("value", value)
30-
print("traceback", traceback)
31-
close(self.connection)
32-
33-
return None
34-
35-
36-
with MyBoard():
37-
pass
25+
if type or value:
26+
sublime.error_message(
27+
"Failed to access {}. \nPlease check connection or settings.".format(
28+
Settings.port
29+
)
30+
)
31+
return False
32+
self.pyboard.close()
33+
del self.connection
34+
return True
3835

39-
try:
40-
myBoard = ampy.Files(pyboard.Pyboard(Settings.serial_conn))
36+
def __repr__(self):
37+
return "<MicroPython @ {}>".format(self.port)
4138

42-
except:
43-
sublime.error_message(
44-
"Failed to access {}. \nPlease check connection or settings.".format(
45-
Settings.serial_conn
46-
)
47-
)
39+
def connect(self):
40+
self.pyboard = pyboard.Pyboard(Settings.port)
41+
self.connection = ampy.Files(self.pyboard)
4842

4943

5044
class MpGetFileCommand(sublime_plugin.WindowCommand, Settings):
5145

5246
def run(self):
53-
try:
54-
self.files = myBoard.ls(long_format=False)
55-
if self.files == []:
56-
sublime.message_dialog(
57-
"There is no file yet. You need to upload a file before want it."
47+
with MyBoard() as board:
48+
board.connect()
49+
try:
50+
self.files = board.connection.ls(long_format=False)
51+
if self.files == []:
52+
sublime.message_dialog(
53+
"There is no file yet. You need to upload a file before want it."
54+
)
55+
return
56+
except NameError:
57+
sublime.error_message(
58+
"Couldn't connect to {} .\n Please check port and re-open sublime text.".format(
59+
self.port
60+
)
5861
)
5962
return
60-
except NameError:
61-
sublime.error_message(
62-
"Couldn't connect to {} .\n Please check port and re-open sublime text.".format(
63-
self.serial_conn
64-
)
65-
)
66-
67-
except:
68-
sublime.error_message("Port is not open or wrong port.")
69-
return
7063

7164
self.files.append(self.files[0])
7265
self.files[0] = "Select a file:"
@@ -102,21 +95,23 @@ def do_it(self, id):
10295
elif id == -1:
10396
sublime.message_dialog("Please select a file!")
10497
else:
105-
try:
106-
out = myBoard.get(file_name)
107-
except RuntimeError as e:
108-
sublime.message_dialog(e)
109-
110-
except SerialException:
111-
sublime.error_message("Port is not open or wrong port.")
112-
return
113-
else:
114-
with open(Settings.project_path + file_name, "w+") as file:
98+
with MyBoard() as board:
99+
board.connect()
100+
try:
101+
out = board.connection.get(file_name)
102+
except RuntimeError as e:
103+
sublime.message_dialog(str(e))
104+
105+
except SerialException:
106+
sublime.error_message("Port is not open or wrong port.")
107+
return
108+
else:
109+
with open(Settings.project_path + file_name, "w+") as file:
115110

116-
out = out.decode("utf-8")
117-
out = indent(out, "", lambda line: True)
111+
out = out.decode("utf-8")
112+
out = indent(out, "", lambda line: True)
118113

119-
file.write(str(out))
114+
file.write(str(out))
120115

121116
self.window.open_file(self.project_path + file_name)
122117

@@ -142,48 +137,43 @@ def run(self, path=None):
142137
file_name = basename(file_path)
143138
file_data = open(file_path, "r").read()
144139

145-
try:
146-
o = myBoard.put(file_name, file_data)
147-
if not o:
148-
self.window.status_message(
149-
"File uploaded. Named as {}".format(file_name)
150-
)
151-
else:
152-
sublime.error_message("File couldn't uploaded. An error occurred.")
153-
154-
except NameError:
155-
sublime.error_message(
156-
"Couldn't connect to {} .\n Please check port and re-open sublime text.".format(
157-
self.serial_conn
140+
with MyBoard() as board:
141+
board.connect()
142+
try:
143+
o = board.connection.put(file_name, file_data)
144+
if not o:
145+
self.window.status_message(
146+
"File uploaded. Named as {}".format(file_name)
147+
)
148+
else:
149+
sublime.error_message(
150+
"File couldn't uploaded. An error occurred."
151+
)
152+
153+
except NameError:
154+
sublime.error_message(
155+
"Couldn't connect to board .\n Please check port and re-open sublime text."
158156
)
159-
)
160-
161-
except SerialException:
162-
sublime.error_message("Port is not open or wrong port.")
163-
return
164157

165158

166159
class MpDeleteFileCommand(sublime_plugin.WindowCommand):
167160

168161
def run(self):
169-
try:
170-
self.files = myBoard.ls(long_format=False)
171-
if self.files == []:
172-
sublime.message_dialog(
173-
"There is no file yet. You need to upload a file before want it."
174-
)
175-
return
162+
with MyBoard() as board:
163+
board.connect()
164+
try:
165+
self.files = board.connection.ls(long_format=False)
166+
if self.files == []:
167+
sublime.message_dialog(
168+
"There is no file yet. You need to upload a file before want it."
169+
)
170+
return None
176171

177-
except NameError:
178-
sublime.error_message(
179-
"Couldn't connect to {} .\n Please check port and re-open sublime text.".format(
180-
self.serial_conn
172+
except:
173+
sublime.error_message(
174+
"Excepted serial connection error. Probably port using by another app."
181175
)
182-
)
183-
184-
except SerialException:
185-
sublime.error_message("Port is not open or wrong port.")
186-
return
176+
return None
187177

188178
self.files.append(self.files[0])
189179
self.files[0] = "Select a file:"
@@ -215,7 +205,9 @@ def do_it(self, id):
215205
sublime.message_dialog("Please select a file!")
216206
else:
217207
try:
218-
out = myBoard.rm(file_name)
208+
with MyBoard() as board:
209+
board.connect()
210+
out = board.connection.rm(file_name)
219211
except RuntimeError as e:
220212
sublime.message_dialog(e)
221213
else:

0 commit comments

Comments
 (0)