python2.7串口操作接受发送16进制指令

# -*- coding: utf-8 -*-
# @Time    : 2018/1/18 21:34
# @Author  : houyujiang
# @Email   : [email protected]
# @File    : SendRead.py
# @Software: PyCharm
import serial
import os#用于清屏,os.system('cls')
def ui():
    print '-------------------------------获取参数----------------------------------------'
    print '1.获取射频参数 2.获取模块地址 3.目标地址 4.获取分组码 5.获取语音地址 6.音频参数 7.中继等级'
    print '----------10.设置目标地址------------11.设置分组码-----------------------'
    print '---------------------------语音对讲模块----------------------------------------'
    print '-----------8.打开PTT-----------9.关闭PTT------------------'


    input_num = raw_input('请输入数字: ')
    return input_num

#串口工厂
def serial_Factory(cmd,num):
    serials = serial.Serial('com3',115200)
    serials.write(cmd)
    argv = serials.read(num)
    result = ''
    hlen = len(argv)
    for i in xrange(hlen):
        hvol = ord(argv[i])
        hhex = '%02x' % hvol
        result += hhex + ''
    return result
    #return cmd+'ok'

#获取射频参数
def get_she_ping():
    cmd = '\x68\x01\x00'
    num = 15
    back = serial_Factory(cmd,num)
    print back

# 获取模块地址
def get_mo_kuai():
    cmd = '\x68\x01\x02'
    num = 6
    back = serial_Factory(cmd,num)
    print back

# 获取目标地址
def get_mu_biao():
    cmd = '\x68\x01\x04'
    num = 6
    back = serial_Factory(cmd,num)
    print back

#获取分组码
def get_fen_zu():
    cmd = '\x68\x01\x06'
    num = 5
    back = serial_Factory(cmd,num)
    print back


#获取语音地址
def get_yu_yin():
    cmd = '\x68\x01\x08'
    num = 6
    back = serial_Factory(cmd,num)
    print back

#获取音频参数
def get_yin_pin():
    cmd = '\x68\x01\x0A'
    num = 5
    back = serial_Factory(cmd,num)
    print back

#获取中继等级
def get_zhong_ji():
    cmd = '\x68\x01\x0C'
    num = 4
    back = serial_Factory(cmd, num)
    print back
#打开PTT
def open_ptt():
    cmd = '\x68\x02\x09\x01'
    num = 3
    back = serial_Factory(cmd, num)
    print back
    if back == '680109':
        print 'ptt打开'

#关闭PTT
def close_ptt():
    cmd = '\x68\x02\x09\x00'
    num = 3
    back = serial_Factory(cmd, num)
    print back
    if back == '680109':
        print '关闭'
#2018.01.24-设置目标地址
def set_mubiao_dizhi():
    cmd = '\x68\x04\x05\xFF\xFF\xFF'#默认0xFFFFFF,id:300=00 01 4a
    num = 3
    back = serial_Factory(cmd,num)
    print back
    if back == '680105':
        print '设置目标地址成功'
    else:
        print '设置目标地址失败'
#设置分组码
def set_FenZuMa():
    cmd = '\x68\x03\x07\x00\x01'#分组码0001
    num = 3
    back = serial_Factory(cmd, num)
    print back
    if back == '680107':
        print '设置分组码成功'
    else:
        print '设置分组码失败'
# 切换器
dicts = {'1': get_she_ping, '2': get_mo_kuai,'3':get_mu_biao,
         '4':get_fen_zu,'5':get_yu_yin,'6':get_yin_pin,'7':get_zhong_ji,
         '8':open_ptt,'9':close_ptt,'10':set_mubiao_dizhi,'11':set_FenZuMa}

if __name__ == '__main__':
    while True:
        os.system('cls')
        num =ui()
        dicts[num]()

你可能感兴趣的:(python2.7串口操作接受发送16进制指令)