qq_25957921 2018-09-26 08:28 采纳率: 0%
浏览 829

python代码如何让机电控制精确到毫秒?

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

  • 写回答

3条回答 默认 最新

  • threenewbee 2018-09-26 08:36
    关注

    非实时操作系统,非实时编程语言,没有办法有那么高的精度,你为难python了

    评论

报告相同问题?

悬赏问题

  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名