数学建模社区-数学中国
标题:
串口转tcp工具
[打印本页]
作者:
2744557306
时间:
2024-3-31 17:19
标题:
串口转tcp工具
#coding=utf-8
8 ^6 [2 L, d9 k6 j9 t8 i
% ]7 w% h$ @9 ~6 h% V7 b- z
- u; R) D# m& z8 {
import sys,threading,time;
# a* ?+ W4 j5 _2 N, K7 q V+ {. q
import serial;
! ] d/ Z( q6 \& T; C6 g
import binascii,encodings;
" R1 p2 o( m6 ^" M. v. o& c
import re;
9 d* v# s! ]# ], { J5 ]
import os;
/ V$ ~4 w& K# y2 X0 j
from socket import *
) C0 D; `8 Z) r
from struct import *;
1 j4 A" f" ^1 H& I5 \9 L
#from myutil import *;
9 E, l' F; t; L, L5 I
#name: myutil.py
5 c. q# L" c5 |1 q9 P6 Y
& Z5 f; X, M2 ]
mylock = threading.RLock()
: c# {6 ]$ G+ k& i4 }
- W# A n5 g; c2 {' g
Server_IP = ''
: L5 V4 T/ f' E7 e. k/ G0 U% Y
Srever_Port = ''
1 w3 V" t$ X: N
; Q8 Y8 I& C: R4 Z, F$ n7 ?
def print_hex1(s,prev='0x'):
" K2 \- c% Z: B: i& w" r8 i5 i
for c in s:
/ K0 C! @$ T- d! x6 I
print '%s%02x' %(prev,ord(c)),
3 T8 N) B7 a; w4 w4 h% _/ m
print
3 q' Q& P" p1 }0 {8 O
def print_hex(s):
, p3 G% Z5 k2 W |
for c in s:
# D X# E5 I/ A. `/ r9 B. T$ K
print '%02x' %(ord(c)),
9 h- E/ ^+ A/ G, m& ? c
print
- O+ H# O$ @' m9 ]! \# @
2 O. p9 F) y( j
def hexto_str(s):
" i9 C, }8 w) l& ?/ I$ V; K
r =''
f' K- l, j' g3 \0 P4 A' N u
for c in s:
2 D& c) f$ U$ e: |7 X
r += '%02x' %(ord(c))
, V+ w- R& n5 F' y7 {/ V# ]0 I& z& ~
return r
# ?: I3 D1 t7 q9 d- ?* b# S! U1 C
def strto_hex(s):
& `$ l+ M; @/ b! S
r = s.decode('hex')
9 B& j, {. l0 I, h% j( F% r/ U
return r
! U @ _/ v0 c
#''代表服务器为localhost
h. \5 { D# Y2 ~% a+ R+ S7 Q# B
/ p; o2 w3 n" A( y, d
#在一个非保留端口号上进行监听
! w2 }5 t% c5 @$ ]( {0 U6 [5 r6 i9 W
1 K3 F7 p+ ~9 h+ U2 y# s5 y$ ^
; m$ S7 Z, [& D: ]; z$ C" X: R" D
class ComThread:
3 D3 X7 a& G/ y/ e6 i N2 V
def __init__(self, Port=0):
5 H' O! @. b; Z; \! s/ N! j
self.l_serial = None;
# @$ v9 P$ S. ?( `" _
self.alive = False;
0 m( }* e8 J2 R8 D8 E$ q
self.waitEnd = None;
7 w, f% H5 X, |/ L; b& C% Q; P
self.port = Port;
) [ ^7 W& h3 \
4 R$ d& t+ @" Q3 ?& a
#TCP部分
& f& L! a3 p* [0 G V, U+ _
#self.sockobj = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
9 i2 O. }; K. U/ x( ]. v! `1 w
self.connection = None
, ^2 }1 N h/ C$ H
#数据
* R, k1 Y. W* I
self.snddata = ''
3 R' {/ H, ?; c: C, V, ?
self.rcvdata = ''
% z R2 _3 ?# H: O. r2 K2 Y
6 S4 a1 n& N6 t4 ?7 M( b
def waiting(self):
+ n% g1 q2 a; C( @6 f1 z
if not self.waitEnd is None:
0 q& s+ P# _7 z. I/ a: F
self.waitEnd.wait();
* Y" a" Y* }# g& y; i
, O. p/ J" r, v! x7 r, b
* r$ _. w4 N: b8 G7 @2 t6 S
def SetStopEvent(self):
' }% _5 `4 Q# A2 y
if not self.waitEnd is None:
& V( ^2 o( h$ {# C4 E$ Z
self.waitEnd.set();
6 }6 I! R: ~: q5 L$ e, P
self.alive = False;
8 A( |5 ]* Z1 [$ g/ P0 e
self.stop();
: O2 ^+ c, r- ?. V
' @8 K' T5 P1 `0 }
' P- F O* d/ L i8 t" a
def start(self):
" B% Z1 _9 {% d# u5 |+ Z" D
self.l_serial = serial.Serial();
( f) L4 r, \5 L$ G1 r' N
self.l_serial.port = self.port;
$ g- x/ n% ]$ r, f
self.l_serial.baudrate = 115200;
0 H2 Y- A6 F% f, }: U9 d
self.l_serial.timeout = 2; #秒
* h9 @9 r, F- x: v5 t# u! ?2 `, x
self.l_serial.open();
1 j, P- m4 I& s- O% N
, i' w2 `4 l7 B9 H
if self.l_serial.isOpen():
: v Y+ f0 H! f6 u
self.waitEnd = threading.Event();
5 z$ H9 D: O' o2 T. a9 r) K# h
self.alive = True;
5 c4 S' j0 K3 S: a6 o! a
print 'open serial port %d ok!\n' %(self.port+1)
' q! b: R3 G- A4 c0 z. s
print 'baudrate:115200 \n'
; E+ z1 M6 b8 ]! K% {
self.thread_read = None;
# @5 x- A3 ]& d6 L
self.thread_read = threading.Thread(target=self.FirstReader);
5 F5 R- e" i9 }
self.thread_read.setDaemon(1);
0 e. V" Y9 [ w9 _. P- h
self.thread_read.start();
+ n+ u/ i7 S* M3 U9 B d8 v
* z' `) y# @, U- I: h/ W* S
self.thread_write = None;
/ ?, R; _+ H0 p7 [
self.thread_write = threading.Thread(target=self.FirstWriter);
; r" e6 Z- p7 i
self.thread_write.setDaemon(1);
9 K" w& P( L: _4 a' C# y4 f
self.thread_write.start();
# T+ f; Y, U' H0 Z* c/ k/ h. d. j
?+ M$ y8 f! M2 V
#TCP部分
0 u" `6 ?" [9 E# }% b! r, v/ i
self.thread_TcpClient = None;
- P0 L" q. z; N @ P
self.thread_TcpClient = threading.Thread(target=self.TcpClient);
6 t& m8 e+ v ^8 y3 g% r G f
self.thread_TcpClient.setDaemon(1);
/ p. x2 E& c: M( L# H. q$ u R
self.thread_TcpClient.start();
) z& ^" m8 ~2 V. F9 {* f h$ ~4 s
! b* x) v9 y* k6 r) ?) m2 y7 r. u( l
self.thread_TcpSend = None;
/ k3 s) ?9 [) a& J; `: O7 t
self.thread_TcpSend = threading.Thread(target=self.TcpSend);
( T3 {+ U5 P5 t$ N% f' [2 R/ R
self.thread_TcpSend.setDaemon(1);
_2 w' N% {7 R9 t
self.thread_TcpSend.start();
$ W) g8 U' }# r2 ]+ x
, R4 Y% z' K$ @0 @, U
return True;
1 x7 l6 ?& u6 c3 I3 O+ S, @
else:
. k" Q8 z. Z$ D1 f+ Y
return False;
# a; `0 P% ?) U$ d* g4 Y" f! [0 F) ]
# O, |/ q" I: t
- M' B4 Q4 _# A+ A9 G
def FirstReader(self):
! F5 O" T4 ~8 t! Y+ o
while self.alive:
7 w* V7 ^5 g' y/ _: \ r4 `
# 接收间隔
: `$ H" G g/ G8 v9 J1 }$ g
time.sleep(0.1);
( m- Z0 _' l$ ?9 X% ?
try:
# `2 l3 L$ i" q$ t5 x' q& [8 n
data = '';
) p7 i5 i) [1 p$ n0 t. P
n = self.l_serial.inWaiting();
) o; D( T) K: g1 W/ S d
if n:
' O( ~7 H$ {; p. Y
data = data+self.l_serial.read(n);
8 O7 U$ u0 s2 Q* \4 T0 T
#for l in xrange(len(data)):
- \6 P' c% y# p3 Z X" r9 q
#print '%02X' % ord(data[l]),
& E* z0 `$ [) F% S
# 发送数据
& y. `/ R+ O2 Y: x
print u'->请求:'
( |: D# b) E, X; o! L
print data;
$ a& U. y( a9 Y9 j4 P) F& Q; S
mylock.acquire()
, \8 M3 j& z7 V3 I! d U' x
self.snddata = data
0 q- _, p4 q) _: X
mylock.release()
$ g: i3 C* F/ Y: k$ ^
#print_hex(data);
' b! ?: |0 G4 y: q
4 L. r" |) \3 m/ L
) A0 a7 y, V) F) K$ s! C
# 判断结束
0 L2 _! D( ?, K. d9 F0 @
3 h" w- ]4 V/ U+ q- J, M% n4 U6 p0 J
except Exception, ex:
0 o+ M! W# u: E O) ]" U5 p: E5 M( h
print str(ex);
7 b9 G6 k' g! w! _8 `. n, f
" B# e8 [8 Y! j8 N
self.waitEnd.set();
) h0 Q' s: V r# I
self.alive = False;
% _8 ?, |8 x- L# g' h0 P
: W g [" T, w$ ?* L3 {) }3 e" h
def FirstWriter(self):
' r* w) a) ~7 s
while self.alive:
5 U, R3 N( m1 n
# 接收间隔
# D/ H G' b( e3 X& x+ d' Q! H2 W
time.sleep(0.1);
* i/ G; L2 a& e4 w+ H6 z+ w7 O
try:
" m2 ^5 [. u; T- j# h! F
#snddata = raw_input('\nenter data send:\n')
% P0 f2 ?( ]7 L. q6 u
if self.rcvdata!='':
- ?" @$ P' X# q- h( e9 Y+ r" t
self.l_serial.write(self.rcvdata);
) |( Q* Z; r$ N$ B4 W
print u'-<应答:'
( b" K2 [1 n6 X' ]- ]
print self.rcvdata;
; g" J# y, H8 j# P: Q2 G% W
mylock.acquire()
8 X/ q# Y1 _, x8 L; B% a
self.rcvdata = '';
: K* k9 u" [$ d4 g3 r* M
mylock.release()
8 t' J3 L5 @ z; @2 q( D7 R! @
#print_hex(snddata);
( j* [- a6 q, K, H* R. C
. q8 s9 l. w' H& i4 l. ~
except Exception, ex:
' A6 }$ `7 d# T/ I, ?1 ~7 @( Y0 Q" |, C
print str(ex);
( Z; C- f+ c# f: x* A
self.waitEnd.set();
( `# s1 `) O& D! A- q* V+ L4 h
self.alive = False;
) F3 }! L: Q0 O6 Q$ K& i4 \
! y8 H( U6 g7 ~3 C. h& k
def TcpClient(self):
% _8 M3 j7 b5 Y6 R1 [4 v# k5 A
while True:
: K& G% U8 O" y/ H( P
# 接收间隔
4 C1 ~" L% B% p C) A& e% N6 |% X
time.sleep(0.1);
% C! N; k1 s% P4 V1 e
self.connection = socket(AF_INET, SOCK_STREAM);
9 K0 a1 |/ h/ x$ J) k8 R
self.connection.connect((Server_IP, int(Server_Port)));
1 g, k- `8 i0 A. V" ~5 S
print 'Connect to Server OK!';
: `# P( G* x5 q9 R! F
self.snddata = ''
+ Z1 W1 Y8 k% m$ H: X3 f
self.rcvdata = ''
4 R5 w, J0 ^' ]$ ?; ]. H# t
while True:
8 p7 H9 n( ^0 _' _' M9 e* A$ B
#读取客户端套接字的下一行
; `: e5 ^: {5 @ B7 E* h! i: G
data = self.connection.recv(1024)
1 ~" F' N3 B* t* K8 {$ ^) W3 I& N
#如果没有数量的话,那么跳出循环
/ @$ S; f4 m' ~7 C* A; G
if not data: break
* x) @- {8 {3 r5 K+ I% D! V
#发送一个回复至客户端
+ u6 q& x" G* z$ A
mylock.acquire()
: I5 P, x& G+ f1 ?% j) ]
self.snddata = ''
$ l& k. q' R6 M! b- A+ \/ ` W
self.rcvdata = data
$ ~( t# n L( j3 V$ }( r/ X9 u
mylock.release()
! Q0 o$ g! X+ a0 e
#connection.send('Echo=>' + data)
- E1 ?* Q" m5 e9 n( a$ h
self.connection.close()
' ?9 _$ A! C% r. e. K8 M1 I% n# ^+ n
! |: R# _/ S/ O; P! X8 g& B
self.waitEnd.set();
R6 g/ G) r0 C, o9 D& R2 a d2 P( h
self.alive = False;
$ @- N& V. {8 M, @$ x; ~7 |
. t( Z6 `( x8 ^" T. @# E
def TcpSend(self):
0 X" Q, n% d& ?) b; @0 o
while True:
4 d) B! S5 \) _* s3 \+ b
# 接收间隔
7 i: y4 w9 G/ z& t+ l
time.sleep(0.1);
" c* ]% v9 f; O( X
while True:
) H( c# r0 v& {, `9 }3 ~ H. O4 `/ c
time.sleep(0.1);
8 A: Y) I; A1 n# |. A @+ j' I
try:
6 W! f2 @% g1 }( z+ ]+ @
if not self.connection is None:
6 f3 x8 ~0 B7 z% t9 U
if self.snddata != '':
4 G c7 n; L" w$ _
self.connection.send(self.snddata)
+ }7 M& C X, P. p! ]
mylock.acquire()
5 o( T0 Z0 ]$ }% [
self.rcvdata = ''
. d* |1 a. H- Y# A6 P$ r j
self.snddata = ''
8 W0 M1 \, H+ w( T; z. ~3 s
mylock.release()
$ D; O" }3 P! v- S7 s2 N
except Exception, ex:
6 z* T( Y+ J( T7 H/ T) L* U
pass
7 k1 f( r. m; p# G
def stop(self):
" a4 a$ T& v# W5 {. ~6 T
self.alive = False;
% H5 W( \2 ^' K$ I) V+ w* l
self.thread_read.join();
U, G2 A, M' b. @
if self.l_serial.isOpen():
$ P5 Q! E( {% p5 E, Q/ w
self.l_serial.close();
* `& D o( U5 T/ L4 m1 {' m! Q: J
* c9 S9 I3 Y; I% V3 l
4 V$ Y$ l, x7 q3 K1 w+ S$ x
' p/ D9 e5 q6 _7 ]3 y: y3 U) G
#测试用部分
0 N2 _) u+ W% g3 H1 e. L# N' Q: E1 T1 N
if __name__ == '__main__':
/ A W, {; D, t4 Z2 F
print 'Serial to Tcp Tool 1.00\n'
6 L( x2 U# a5 M4 z
print 'Author:yangyongzhen\n'
5 ^9 L0 o! u% D* U
print 'QQ:534117529\n'
; q5 I5 K8 L6 I; C1 `6 m+ x
print 'Copyright (c) **cap 2015-2016.\n'
; t0 f% z) N b' x6 @
/ @! ~+ @7 f+ r1 C
Server_IP = raw_input('please enter ServerIP:')
9 f: f. l$ g) D# J
print 'Server_IP: %s' %(Server_IP)
% o$ z# _& _& |, n& f" x2 P& N7 v
Server_Port = raw_input('please enter ServerPort:')
' ~: H6 D8 Y! y
print 'Server_Port: %s' %(Server_Port)
( V4 p4 z5 M! K) O8 f; B4 ~4 ^# M* p
com =raw_input('please enter com port(1-9):')
! S0 r" H) f3 a) Q2 x
rt = ComThread(int(com)-1);
, M/ t) v( E8 t& y" [; G3 J2 h
try:
( n+ ~$ F7 [* ~* y" U
if rt.start():
2 n3 |2 k; w Z" s) h9 G* \
rt.waiting();
, F+ q) g1 d2 S0 I% n: e1 m
rt.stop();
' l% e( i. C5 o" {
else:
C- f( Q( x& G3 {
pass;
2 G) w8 F6 k3 A( S. |2 e/ J
except Exception,se:
% C q ?+ ]# ^9 E
print str(se);
9 ^; N3 r: U0 i4 O
, |- ?1 F8 j" c6 m& m7 o
if rt.alive:
( J) R. U1 C) P! }
rt.stop();
, B, K3 r0 l8 C# i; Q' l" W5 Z
os.system("pause")
- k3 }) F) p! q) _
) x/ h1 v. n4 c9 ]
print '';
k1 k2 g) g5 V" ^+ F
print 'End OK .';
% B4 b$ D5 w+ l- ]& h6 A/ @
del rt;
复制代码
. T4 p+ g& s: k& p
欢迎光临 数学建模社区-数学中国 (http://www.madio.net/)
Powered by Discuz! X2.5