code:

 import serial,time,itertools

 try:
ser=serial.Serial(2,115200,timeout=0)
except:
print 'Open COM2 Failed!!' #transno=(1,100,500,1380,1381)
#transno=(1,20)
indexsets=[[0],[1],[2],[3],[0,1],[0,2],[0,3],[1,2],[1,3],[2,3],[0,1,2],[0,1,3],[0,2,3],[1,2,3],[0,1,2,3]]
indexsets=[[0,1,3],[0,1,2]]
#indexsets_ts=[] def gene():
for n in range(1,5):
indexsets_ts+=list(itertools.combinations([0,1,2,3],n)) #forget what is itertool now
return indexsets_ts #sig functions
def sigsnd():
for actmod in ('diract','apnact'):
act(actmod)
for connmod in ('TCP','UDP'):
conn(connmod)
connt=0
for dismod in ('dirsht','closht'):
for sndmod in ('uctl','fixl'):
snd(sndmod)
dis(dismod)
if connt<1:
conn(connmod)
connt+=1 def init():
swrt(chr(0x27),1)
swrt('\r\n',1)
ser.write('+++')
swrt('\r\n',1)
def shutConn():
swrt('at+cipshut',4)
def cloShut():
swrt('at+cipclose',4)
swrt('at+cipshut',4)
def cipmod():
swrt('at+cipmode=0',2)
def olmod():
swrt('at+cipmode=1',2)
def muxmod():
swrt('at+cipmux=1',2)
def sigmod():
swrt('at+cipmux=0',2)
def srd():
srd=ser.readlines()
for line in srd:
print line
fid=open('tcp_snd.log','a')
fid.write(line)
fid.close()
return srd
def swrt(atcmd_str,wt):
#fid=open('tcp_snd.log','a')
#fid.write(atcmd_str)
#fid.close()
ser.write(atcmd_str+'\r\n')
time.sleep(wt)
srd()
def wrtmsg(transtr,wstm):
ser.write(transtr+chr(0x1a))
time.sleep(wstm)
srd()
def act(actmod):
if actmod=='apnact':
swrt('at+cstt="CMNET","",""',2)
swrt('at+ciicr',4)
if actmod=='diract':
pass
def conn(connmod):
if connmod=='TCP':
swrt('at+cipstart="TCP","124.205.119.38",1562',6)
if connmod=='UDP':
swrt('at+cipstart="UDP","124.205.119.38",1562',6)
def snd(sndmod):
if sndmod=='uctl':
for tran in transno:
for n in range(2):
swrt('at+cipsend',1)
wrtmsg(tran*'v',8)
if sndmod=='fixl':
for tran in transno:
for n in range(2):
swrt('at+cipsend='+str(tran),1)
wrtmsg(tran*'v',8)
def dis(dismod):
if dismod=='dirsht':
shutConn()
if dismod=='closht':
cloShut() #online mode function
def modTs():
for actmod in ('diract','apnact'):
act(actmod)
for connmod in ('TCP','UDP'):
print connmod
conn(connmod)
connt=0
for dismod in ('dirsht','closht'):
print dismod
olsnd()
dis(dismod)
print dismod
if connt<1:
conn(connmod)
connt+=1 def olsnd():
for rtm in range(2):
for tran in transno:
swrt(tran*'v',1)
nmipmd()
ato()
nmipmd()
def ato():
swrt('ato',2)
def nmipmd():
ser.write('+++')
time.sleep(3)
srd() #mux-mode functions indset=[0,1,3]
def muxTS(indset):
for actmod in ('diract','apnact'):
act(actmod)
for dismod in ('dirsht','closht'):
for x in range(2**len(indset)):
binstr=bin(int(str(x),10))[2:].rjust(len(indset),'')
typset=[]
for nc in binstr:
if nc=='':
typset.append('"TCP"')
else:
typset.append('"UDP"')
for ind in range(len(indset)):
constr='at+cipstart='+str(indset[ind])+','+typset[ind]+',"124.205.119.38",1562'
swrt(constr,7)
print 'wrt done' ##connect OK
for sndmod in ('uctl','fixl'):
for index in indset:
print indset,index
for tran in transno:
for n in range(2):
if sndmod=='uctl':
swrt('at+cipsend='+str(index),1)
wrtmsg(tran*'v',15)
else:
swrt('at+cipsend='+str(index)+','+str(tran),1)
wrtmsg(tran*'v',15)
dis(dismod) sndmod=''
def muxmod():
swrt('at+cipmux=1',2) def muxsnd(indset):
for index in indset:
print indset,index
for tran in transno:
for n in range(5):
if sndmod=='uctl':
swrt('at+cipsend='+str(index),1)
wrtmsg(tran*'v',15)
else:
swrt('at+cipsend='+str(index)+','+str(tran),1)
wrtmsg(tran*'v',15) #sigle-mode Ts
init()
shutConn()
cipmod()
sigmod()
sigsnd() #online-mode Ts
init()
shutConn()
cipmod()
sigmod()
olmod()
modTs() #mux-mode Ts
init()
shutConn()
cipmod()
sigmod()
muxmod()
[muxTS(indset) for indset in indexsets] init()
shutConn()
ser.close()

simple python code when @ simplnano的更多相关文章

  1. Exploring Python Code Objects

    Exploring Python Code Objects https://late.am/post/2012/03/26/exploring-python-code-objects.html Ins ...

  2. 机器学习算法实现(R&Python code)

    Machine Learning Algorithms Machine Learning Algorithms (Python and R) 明天考试,今天就来简单写写机器学习的算法 Types Su ...

  3. How to run Python code from Sublime

    How to run Python Code from Sublime,and How to run Python Code with input from sublime Using Sublime ...

  4. 46 Simple Python Exercises (前20道题)

    46 Simple Python Exercises This is version 0.45 of a collection of simple Python exercises construct ...

  5. Python code 提取UML

    Python是一门支持面向对象编程的语言,在大型软件项目中,我们往往会使用面向对象的特性去组织我们的代码,那有没有这样一种工具,可以帮助我们从已有代码中提取出UML图呢?答案是有的.以下,我们逐个介绍 ...

  6. PEP 8 – Style Guide for Python Code

    原文:PEP 8 – Style Guide for Python Code PEP:8 题目:Python代码风格指南 作者:Guido van Rossum, www.yszx11.cnBarry ...

  7. 46 Simple Python Exercises-Very simple exercises

    46 Simple Python Exercises-Very simple exercises 4.Write a function that takes a character (i.e. a s ...

  8. Change the environment variable for python code running

    python程序运行中改变环境变量: Trying to change the way the loader works for a running Python is very tricky; pr ...

  9. python code

    执行动态语句 执行字符串中的代码 http://www.cnblogs.com/fanweibin/p/5418817.html #!usr/bin/env python #coding:utf-8 ...

随机推荐

  1. PHP学习(五)----jQuery和JSON数据

    对于jQuery: jQuery 是一个 JavaScript 库. jQuery 极大地简化了 JavaScript 编程.

  2. 控制变量法-初中物理-Nobel Lecture, December 12, 1929-php执行SET GLOBAL connect_timeout=2效果

    $link = mysqli_connect("localhost", "wu", "wp", "wdb"); $sql ...

  3. java继承覆盖总结

      Java基础(1)  版权声明:本文为博主原创文章,未经博主允许不得转载. java的继承与覆盖基本是java笔试中常出的题,也比较绕,我这里对java的继承覆盖做一个总结1.构造函数:      ...

  4. 六 mybatis高级映射(一对一,一对多,多对多)

    1  订单商品数据模型 以订单商品数据为模型,来对mybaits高级关系映射进行学习.

  5. comms.nottingham.ac.uk/learningtechnology

    http://comms.nottingham.ac.uk/learningtechnology/

  6. 深入GetMessage和PeekMessage

    http://blog.csdn.net/fireseed/article/details/2176 http://www.cnblogs.com/sadier/articles/100948.htm ...

  7. USB2.0协议笔记

    1.概述     USB(Universal Serial Bus)具有传输速率快,可热插拔等显著特点,应用已经十分广泛,目前的相当多的设备已经支持最新的USB3.0协议.理论上USB1.1的传输速度 ...

  8. Tinyxml的简单应用

    参考文章: 1-> http://www.cnblogs.com/phinecos/archive/2008/03/11/1100912.html 2-> http://blog.csdn ...

  9. Spring整合CXF之发布WebService服务

    今天我们来讲下如何用Spring来整合CXF,来发布WebService服务: 给下官方文档地址:http://cxf.apache.org/docs/writing-a-service-with-s ...

  10. ArcGIS API for Silverlight 调用GP服务绘制等值面

    原文:ArcGIS API for Silverlight 调用GP服务绘制等值面 GP服务模型如下图: 示例效果图片如下: