Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mods
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
pub
mods
Commits
31b892ae
Commit
31b892ae
authored
Jan 17, 2020
by
Neil Gershenfeld
Browse files
Options
Downloads
Patches
Plain Diff
start serialserver.py
parent
ab75a55c
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
py/serialserver.py
+153
-0
153 additions, 0 deletions
py/serialserver.py
with
153 additions
and
0 deletions
py/serialserver.py
0 → 100755
+
153
−
0
View file @
31b892ae
#!/usr/bin/env python3
#
# serialserver.py
# send a serial file with handshaking
#
# Neil Gershenfeld 1/17/20
#
# This work may be reproduced, modified, distributed,
# performed, and displayed for any purpose, but must
# acknowledge the mods project. Copyright is
# retained and must be preserved. The work is provided
# as is; no warranty is provided, and users accept all
# liability.
#
import
sys
,
serial
,
time
,
threading
,
os
,
time
,
asyncio
,
websockets
#from Tkinter import *
async
def
hello
(
websocket
,
path
):
while
(
1
):
name
=
await
websocket
.
recv
()
print
(
"
< {}
"
.
format
(
name
))
start_server
=
websockets
.
serve
(
hello
,
'
localhost
'
,
1234
)
asyncio
.
get_event_loop
().
run_until_complete
(
start_server
)
asyncio
.
get_event_loop
().
run_forever
()
WINDOW
=
400
# window size
RUN
=
0
PAUSE
=
1
state
=
RUN
#
# send routine
#
#def send(parent,canvas,data,s):
def
send
(
canvas
,
data
,
s
):
global
state
,
tstart
n
=
0
N
=
len
(
data
)
for
c
in
data
:
#
# check for pause
#
if
(
state
==
PAUSE
):
while
(
state
==
PAUSE
):
time
.
sleep
(
0.001
)
#
# check for flow control handshaking
#
if
(
flow
==
"
dsrdtr
"
):
while
(
s
.
getDSR
()
!=
True
):
time
.
sleep
(
0.001
)
elif
(
flow
==
"
rtscts
"
):
while
(
s
.
getCTS
()
!=
True
):
time
.
sleep
(
0.001
)
#
# send next char
#
s
.
write
(
c
)
s
.
flush
()
#
# update
#
n
+=
1
percent
=
(
100.0
*
n
)
/
len
(
data
)
dt
=
(
time
.
time
()
-
tstart
)
/
60.
totalt
=
(
dt
/
n
)
*
len
(
data
)
canvas
.
itemconfigure
(
"
text
"
,
text
=
"
sending %.1f%% (%.0f/%.0f min)
"
%
(
percent
,
dt
,
totalt
))
canvas
.
update
()
s
.
close
()
os
.
_exit
(
0
)
#
# pause routine
#
def
pause
():
global
state
if
(
state
==
RUN
):
state
=
PAUSE
pause_button
.
config
(
text
=
"
continue
"
)
elif
(
state
==
PAUSE
):
state
=
RUN
pause_button
.
config
(
text
=
"
pause
"
)
#
# cancel routine
#
def
cancel
():
global
s
s
.
close
()
sys
.
exit
()
#
# quit routine
#
def
quit
():
s
.
close
()
sys
.
exit
()
#
# command line
#
if
(
len
(
sys
.
argv
)
!=
4
):
print
(
"
command line: mod_serial.py port speed flow
"
)
print
(
"
port = serial port
"
)
print
(
"
speed = comm speed
"
)
print
(
"
flow = flow control (none | xonxoff | rtscts | dsrdtr )
"
)
sys
.
exit
()
port
=
sys
.
argv
[
1
]
speed
=
sys
.
argv
[
2
]
flow
=
sys
.
argv
[
3
]
filename
=
sys
.
argv
[
4
]
"""
#
# open file
#
f = open(filename)
data = f.read()
f.close()
#
# open port
#
if (flow ==
"
xonxoff
"
):
s = serial.Serial(port, baudrate=speed, xonxoff=True, timeout=0)
elif (flow ==
"
rtscts
"
):
s = serial.Serial(port, baudrate=speed, rtscts=True, timeout=0)
elif (flow ==
"
dsrdtr
"
):
s = serial.Serial(port, baudrate=speed, dsrdtr=True, timeout=0)
elif (flow ==
"
none
"
):
s = serial.Serial(port, baudrate=speed, timeout=0)
s.flushInput()
s.flushOutput()
#
# set up GUI
#
root = Tk()
root.title(
'
mod_serial.py
'
)
canvas = Canvas(root, width=WINDOW, height=.25*WINDOW, background=
'
white
'
)
canvas.create_text(.5*WINDOW,.1*WINDOW,text=
""
,font=(
"
Helvetica
"
,24),tags=
"
text
"
,fill=
"
#0000b0
"
)
canvas.pack()
pause_button = Button(root,text=
"
pause
"
,command=pause)
pause_button.pack()
cancel_button = Button(root,text=
"
cancel
"
,command=cancel)
cancel_button.pack()
#
# start sending thread
#
tstart = time.time()
t = threading.Thread(target=send,args=(canvas,data,s))
t.start()
#
# start UI loop
#
root.mainloop()
"""
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment