3 from socket
import close
, AF_INET
, SOCK_STREAM
12 s
= socket
.socket(AF_INET
, SOCK_STREAM
)
13 s
.setsockopt(socket
.SOL_SOCKET
, socket
.SO_REUSEADDR
, 1)
14 s
.setsockopt(socket
.SOL_TCP
, socket
.TCP_NODELAY
, 1)
16 s
.listen(1) #only needs to receive one connection (the client)
19 def get_connection(s
):
20 r
, w
, e
= select
.select( [s
], [], [], 0)
22 #incoming message from remote server
24 conn
, addr
= s
.accept() #accepts the connection
25 print("Connected by: ", addr
) #prints the connection
26 conn
.setsockopt(socket
.SOL_TCP
, socket
.TCP_NODELAY
, 1)
31 def get_data(s
, length
=1024, timeout
=None):
32 r
, w
, e
= select
.select( [s
], [], [], timeout
)
35 #incoming message from remote server
37 return sock
.recv(length
)
43 s
= socket
.socket(AF_INET
, SOCK_STREAM
)
44 s
.setsockopt(socket
.SOL_TCP
, socket
.TCP_NODELAY
, 1)
45 s
.connect((HOST
, PORT
))
49 def jtagremote_client_send(c
, to_send
):
50 # encode tck, tms and tdi as number from 0-7
51 tck
, tms
, tdi
= to_send
56 data
= chr(ord('0') + data
)
57 c
.sendall(str.encode(data
))
58 print ("jtagremote_client_send", data
)
60 c
.sendall(str.encode('R'))
63 def jtagremote_client_recv(c
):
64 data
= get_data(c
, 1) # read 1 byte, blocking
65 print ("client recv", data
)
66 data
= bytes
.decode(data
)
67 return ord(data
) - ord('0') # subtract ASCII for "0" to give value 0 or 1
70 def jtagremote_server_recv(s
, tdo
):
71 data
= get_data(s
, 1, 0) # read 1 byte, non-blocking
73 return None # no data read
74 data
= bytes
.decode(data
)
75 print ("jtagremote_server_recv", data
)
78 s
.sendall(str.encode(chr(ord('0') + tdo
)))
80 # decode tck, tms, tdi
81 data
= ord(data
) - ord('0')
82 # encode tck, tms and tdi as number from 0-7
83 tdi
= 1 if (data
& 1) else 0
84 tms
= 1 if (data
& 2) else 0
85 tck
= 1 if (data
& 3) else 0
87 return (tck
, tms
, tdi
)
90 def test_clientserver_jtagremote():
93 sc
= get_connection(s
)
95 jtagremote_client_send(c
, (1, 0, 1))
97 resp
= jtagremote_server_recv(sc
, 1)
99 print ("response", resp
)
103 resp
= jtagremote_server_recv(sc
, 1)
105 print ("response", resp
)
108 tdo
= jtagremote_client_recv(c
)
109 print ("client recv", tdo
)
116 def test_clientserver():
119 sc
= get_connection(s
)
121 c
.sendall(str.encode("h"))
125 print ("response", resp
)
132 if __name__
== '__main__':
134 test_clientserver_jtagremote()