-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXbeeConnect.py
More file actions
87 lines (74 loc) · 2.7 KB
/
Copy pathXbeeConnect.py
File metadata and controls
87 lines (74 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#! /usr/bin/python
# -*- coding: utf-8 -*-
from xbee import XBee
import XbeeCommands
import serial
import time
from PyQt4 import QtCore
import logging
"""
установление соединения с модулем Xbee
"""
commands = []
commands_short = []
for i in XbeeCommands.ALL_CLASSES:
for command in [command for command in dir(i) if not command.startswith("__")]:
c = i.__dict__.get(command)
commands.append(c)
commands_short.append(c.command)
commands_dict = dict(zip(commands_short, commands))
class XbeeConnect(QtCore.QThread):
def __init__(self, form):
QtCore.QThread.__init__(self)
self.com = ''
self.speed = ''
self.ser = None
self.prefs = []
self.logger = None
def sendDataToForm(self, data):
self.emit(QtCore.SIGNAL('SendData(QString)'), data)
# Для теста будем распечатывать то же самое в консоли
print self, data
def run(self):
# self.connectToModule()
# Запускаем специальную тестовую функцию
self.connectToModule()
def connectToModule(self):
self.sendDataToForm("Initialize Xbee...")
print "Initialize Xbee..."
time.sleep(1)
try:
self.ser = serial.Serial(self.com, self.speed)
except Exception as ex:
self.sendDataToForm(ex.message)
return
else:
self.sendDataToForm("Connection to port {} was successful".format(self.com))
xbee = XBee(self.ser)
self.sendDataToForm("Send DH command to module...")
print "Send DH command to module..."
time.sleep(1)
xbee.send('at', frame_id='A', command='DH')
response = xbee.wait_read_frame()
self.sendDataToForm("Wait for response from module...")
print "Wait for response from module..."
self.sleep(1)
self.sendResponse(response['parameter'].encode("hex"))
print response['parameter'].encode("hex")
def sendCommand(self, command, frame_id):
xbee = XBee(self.ser)
xbee.send('at', frame_id=frame_id, command=str(command))
response = xbee.wait_read_frame()
self.sendResponse(response['parameter'].encode("hex"))
def sendResponse(self, response):
self.emit(QtCore.SIGNAL('SendResponse(QString)'), response)
class LVL():
"""
Сопоставление уровней логирования с числами
"""
CRITICAL = 50
ERROR = 40
WARNING = 30
INFO = 20
DEBUG = 10
NOTSET = 0