import os
from cmd2serial import *
cmd_arr = ['CON', 'CLS', 'FWD', 'RVT', 'RUN', 'STP', 'DLY', 'FRQ']
class InverterScript():
def init(self):
self.inverter = MyInverter("com8")
def read_script(self):
cwd = os.getcwd()
file = open(cwd + r'\SCRIPT.ini', 'r')
all_text = file.readlines()
file.close()
for index, line in enumerate(all_text):
all_text[index] = line.strip('\n')
print all_text
self.script_line_arr = all_text
def in_cmd_arr(self, cmd):
for item in cmd_arr:
if item == cmd: return True
return False
def pre_judge(self):
for line in self.script_line_arr:
if line[0:2] == r'//': continue
str_arr = line.split(' ')
if self.in_cmd_arr(str_arr[0]) == False:
print "illegal script"
return False
return True
def interprter(self):
for line in self.script_line_arr:
if line[0:2] == r'//': continue
str_arr = line.split(' ')
if str_arr[0] == "CON":
self.inverter.port = str_arr[1]
self.inverter.open_serial()
print "connect com " + str_arr[1]
elif str_arr[0] == "CLS":
self.inverter.close_serial()
print "close com"
elif str_arr[0] == "FWD":
self.inverter.send_set_fwd_cmd()
print "set forward"
elif str_arr[0] == "RVT":
self.inverter.send_set_rev_cmd()
print "set revert"
elif str_arr[0] == "RUN":
self.inverter.send_set_run_cmd()
print "set run"
elif str_arr[0] == "STP":
self.inverter.send_set_stop_cmd()
print "set stop"
elif str_arr[0] == "DLY":
print "delay for " + str(str_arr[1]) + " seconds"
time.sleep(int(str_arr[1]))
elif str_arr[0] == "FRQ":
self.inverter.send_set_freq_cmd(int(str_arr[1]))
print "set freqency to " + str(str_arr[1]) + " Hz"
my_is = InverterScript()
my_is.read_script()
if my_is.pre_judge():
my_is.interprter()
//ini文件
CON COM8
// loop1
FWD
FRQ 16
RUN
DLY 6
STP
DLY 2
// revert
RVT
FRQ 16
RUN
DLY 6.01
STP
DLY 2
// finish
// loop2
FWD
FRQ 16
RUN
DLY 6
STP
DLY 2
// revert
RVT
FRQ 16
RUN
DLY 6.02
STP
DLY 2
// loop3
FWD
FRQ 16
RUN
DLY 6
STP
DLY 2
// revert
RVT
FRQ 16
RUN
DLY 6.03
STP
DLY 2
// loop4
FWD
FRQ 16
RUN
DLY 6
STP
DLY 2
// revert
RVT
FRQ 16
RUN
DLY 6.04
STP
DLY 2
// loop5
FWD
FRQ 16
RUN
DLY 6
STP
DLY 2
// revert
RVT
FRQ 16
RUN
DLY 6.05
STP
DLY 2
// loop6
FWD
FRQ 16
RUN
DLY 6
STP
DLY 2
// revert
RVT
FRQ 16
RUN
DLY 6.05
STP
DLY 2
// loop7
FWD
FRQ 16
RUN
DLY 6
STP
DLY 2
// revert
RVT
FRQ 16
RUN
DLY 6.05
STP
DLY 2
// loop8
FWD
FRQ 16
RUN
DLY 6
STP
DLY 2
// revert
RVT
FRQ 16
RUN
DLY 6.05
STP
DLY 2
// loop9
FWD
FRQ 16
RUN
DLY 6
STP
DLY 2
// revert
RVT
FRQ 16
RUN
DLY 6.05
STP
DLY 2
// loop10
FWD
FRQ 16
RUN
DLY 6
STP
DLY 2
// revert
RVT
FRQ 16
RUN
DLY 6.05
STP
DLY 2
// finish
// finish
CLS
非实时操作系统,非实时编程语言,没有办法有那么高的精度,你为难python了
elif str_arr[0] == "DLY":
print "delay for " + str(str_arr[1]) + " seconds"
time.sleep(int(str_arr[1]))
改为:
elif str_arr[0] == "DLY":
print "delay for " + str(str_arr[1]) + " ms"
time.sleep(float(str_arr[1])/1000)
elif str_arr[0] == "DLY":
print "delay for " + str(str_arr[1]) + " seconds"
time.sleep(int(str_arr[1]))
改为
elif str_arr[0] == "DLY":
print "delay for " + str(str_arr[1]) + " seconds"
time.sleep(float(str_arr[1]))
然后在脚本同级目录下新建一个bat文件:
里面代码:
python script_inverter.py py2exe
双击改bat后,你的dist目录中exe就会被重新编译一遍了.代码修改将在exe中生效.