资源简介
用于控制电机的python程序,主要用于控制减速电机,通过电机自带的电子工具箱,由串口工具连接电脑,以python程序控制电机的动作
代码片段和文件信息
# author:hejiamin
# coding:utf-8
import serial.tools.list_ports
import time
import re
def list_com():
res = ““
plist = list(serial.tools.list_ports.comports())
if len(plist) <= 0:
res = “find no com!“
print res
return res
else:
for plist_item in plist:
res = res + “com available:“ + str(plist_item.device)
print res
return res
class MyInverter:
port = “com5“
#1 closed 2 using 0 ready
serial_status = 1;
def __init__(self port):
self.port = port
def open_serial(self):
self.ser = serial.Serial(port=self.port baudrate=9600 bytesize=8 parity=‘O‘ stopbits=1 timeout=2)
time.sleep(2)
self.serial_status = 0
def __send_cmd(self cmd):
if self.serial_status != 0:
print “serial not ready!!!“
return False “serial not ready!!!“
self.serial_status = 2
self.ser.write(cmd)
time.sleep(1)
response = self.ser.read_all()
#print “response:“ + response
self.serial_status = 0
if len(response) == 0:
return False response
if response[0] == ‘:‘:
return True response
else:
return False response
def close_serial(self):
self.ser.close()
self.serial_status = 1
def send_set_fwd_cmd(self):
cmd = ‘:010620000010c9\r\n‘
status response = self.__send_cmd(cmd)
return status
def send_set_rev_cmd(self):
cmd = ‘:010620000020b9\r\n‘
status response = self.__send_cmd(cmd)
return status
def send_set_run_cmd(self):
cmd = ‘:010620000002d7\r\n‘
status response = self.__send_cmd(cmd)
return status
def send_set_stop_cmd(self):
cmd = ‘:010620000001d8\r\n‘
status response = self.__send_cmd(cmd)
return status
def __hex_string2hex_array(self hex_string):
if len(hex_string) % 2 != 0:
return False [“0x00“]
hex_arr = [0] * (len(hex_string) / 2)
string_arr = re.findall(r‘.{2}‘ hex_string)
for index value in enumerate(string_arr):
string_arr[index] = ‘0x‘ + value
tmp_int = int(string_arr[index] 16)
hex_arr[index] = tmp_int
#print hex_arr
return True hex_arr
def __get_lrc(self hex_arr):
lrc = 0
sum = 0
for hex_item in hex_arr:
sum = sum + hex_item
lrc = 256 - sum % 256
return lrc
def __raw_cmd_2_msg(self raw_str):
pre_lrc_str = “00“ + raw_str[1:]
_ pre_lrc_hex_arr = self.__hex_string2hex_array(pre_lrc_str)
lrc = self.__get_lrc(pre_lrc_hex_arr)
lrc_str = hex(lrc)[2:]
post_lrc_hex_str = raw_str + str(lrc_str)
#print post_lrc_hex_str
return post_lrc_hex_str
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-09-27 11:03 Code\
目录 0 2018-08-27 15:56 Code\.idea\
文件 466 2018-04-05 21:57 Code\.idea\Code.iml
文件 192 2018-04-05 21:57 Code\.idea\misc.xm
文件 260 2018-04-05 21:55 Code\.idea\modules.xm
文件 23423 2018-08-27 15:56 Code\.idea\workspace.xm
目录 0 2018-04-06 23:54 Code\build\
目录 0 2018-04-06 18:52 Code\build\Gui_inverter\
文件 1352 2018-04-06 18:52 Code\build\Gui_inverter\Gui_inverter.exe.manifest
文件 104687 2018-04-06 18:52 Code\build\Gui_inverter\out00-Analysis.toc
文件 88768 2018-04-06 18:52 Code\build\Gui_inverter\out00-EXE.toc
文件 5484220 2018-04-06 18:52 Code\build\Gui_inverter\out00-PKG.pkg
文件 87458 2018-04-06 18:52 Code\build\Gui_inverter\out00-PKG.toc
文件 773126 2018-04-06 18:52 Code\build\Gui_inverter\out00-PYZ.pyz
文件 17937 2018-04-06 18:52 Code\build\Gui_inverter\out00-PYZ.toc
文件 78571 2018-04-06 18:52 Code\build\Gui_inverter\out00-Tree.toc
文件 6596 2018-04-06 18:52 Code\build\Gui_inverter\out01-Tree.toc
文件 855 2018-04-06 18:52 Code\build\Gui_inverter\warnGui_inverter.txt
文件 177991 2018-04-06 18:52 Code\build\Gui_inverter\xref-Gui_inverter.html
目录 0 2018-04-06 23:54 Code\build\sc
文件 19020 2018-04-06 23:54 Code\build\sc
文件 3498 2018-04-06 23:54 Code\build\sc
文件 3130403 2018-04-06 23:54 Code\build\sc
文件 2182 2018-04-06 23:54 Code\build\sc
文件 719162 2018-04-06 23:54 Code\build\sc
文件 17566 2018-04-06 23:54 Code\build\sc
文件 1355 2018-04-06 23:54 Code\build\sc
文件 855 2018-04-06 23:54 Code\build\sc
文件 174474 2018-04-06 23:54 Code\build\sc
文件 3628 2018-07-20 10:54 Code\cmd2serial.py
文件 4796 2018-07-20 11:03 Code\cmd2serial.pyc
............此处省略2198个文件信息
评论
共有 条评论