Repository navigation
Expand file tree
/
Copy pathCommunicationBlock.py
More file actions
221 lines (194 loc) · 7.09 KB
/
Copy pathCommunicationBlock.py
File metadata and controls
221 lines (194 loc) · 7.09 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# -*- coding: utf-8 -*-
'''
通信功能区
Created on 2014-2-18
@author: GroundMelon
'''
from binascii import b2a_hex
import wx
import util
import communication.MessageProcess as MsgPrcs
import PickleFileIO
from GroundStationBase import FrameGroundStationBase
class CommBlock(FrameGroundStationBase,object):
'''
通信功能区
包含一些与GUI相关的,与通信相关的函数
'''
def send_data_by_GUI(self):
'''
在GUI端触发了数据发送功能的执行函数,从Text Area里获得发送数据
'''
gui_data = self.m_textCtrl_comm_send.GetValue().encode('ascii','ignore')
self.history.add(gui_data)
# text area中的内容是hex/ascii格式分别处理
if self.m_choice_send_style.GetStringSelection() == 'HEX':
# 按空格分离,str->int->char,把hexstring转为byte-string
rtn = self.send_data_by_frame(''.join([chr(int(x,16)) for x in gui_data.split()]))
elif self.m_choice_send_style.GetStringSelection() == 'ASCII':
# 直接发送
rtn = self.send_data_by_frame(gui_data)
else:
assert False, u'Send type is not ASCII nor HEX!'
return
if self.m_checkBox_sent_clear.IsChecked():
# 发送后清空
self.m_textCtrl_comm_send.SetValue('')
self.sbar.update(u'本次发送%d字节'%rtn)
def send_data_by_frame(self, data):
'''
直接由frame发送数据,比如控制指令,不需读取text area 的值
@param data: 待发送数据,string
@return: 发送的字节数
'''
return self.comm.send_string(data)
def get_backdata(self):
'''
从通信模块获取传回的数据
@return: 数据有效,返回值参考L{GroundStation.communication.MessageProcess.unpack_type},数据无效为(None,None)
'''
buf = self.comm.get_rcvbuf()
if len(buf)>=23:
tmp=buf[-23::]
return MsgPrcs.unpack_type(''.join(buf[-23::]))
else:
return (None, None)
def update_rcv_area(self, refresh = False):
'''
更新数据接收区函数
@param refresh: 是否需要刷新
'''
if refresh:
data = self.comm.get_rcvbuf()
func = 'ChangeValue'
else:
data = self.comm.get_new_buf()
func = 'AppendText'
if data:
if self.m_choice_recv_style.GetStringSelection() == 'HEX':
s = ' '.join([b2a_hex(x) for x in data]).upper()+' '
elif self.m_choice_recv_style.GetStringSelection() == 'ASCII':
s = ''.join([x if x<'\xF0' else ' ' for x in data])
else:
assert False, u'Send type is not ASCII nor HEX!'
s = ''
else:
s = ''
eval("self.m_textCtrl_comm_receive.%s"%func)(s) # 使用ChangeValue/AppendText
def process_backdata(self, msgtype, buf):
'''
处理回传数据
@param msgtype: 消息类型
@param buf: 消息数据
'''
for (k,v) in MsgPrcs.PKGTYPE_PID.iteritems():# PID消息
if v == msgtype:
data = MsgPrcs.unpack_5f(buf)
self.update_rcv_pid(k, data[:4])
return
if msgtype == MsgPrcs.PKGTYPE_ATTI:
data = MsgPrcs.unpack_5f(buf)
self.UAVinfo.update_info(data)
elif msgtype == MsgPrcs.PKGTYPE_LOC:
data = MsgPrcs.unpack_5f(buf)
self.UAVinfo.update(None, None, None, None, None, data[0], data[1])
self.update_GE(self.UAVinfo.get())
elif msgtype == MsgPrcs.PKGTYPE_REF:
data = MsgPrcs.unpack_5f(buf)
self.UAVinfo.update_ref(data)
elif msgtype == MsgPrcs.PKGTYPE_CTRL:
data = MsgPrcs.unpack_ctrl(buf)
self.UAVinfo.update_status(data)
elif msgtype == MsgPrcs.PKGTYPE_SPD:
data = MsgPrcs.unpack_5f(buf)
self.UAVinfo.update_spd(data)
elif msgtype == MsgPrcs.PKGTYPE_U0:
data = MsgPrcs.unpack_5f(buf)
self.UAVinfo.update_u0(data)
elif msgtype == MsgPrcs.PKGTYPE_U1:
data = MsgPrcs.unpack_5f(buf)
self.UAVinfo.update_u1(data)
def enable_comm_relative_components(self, switch):
'''
通信相关控件使能失能
@param switch: True or False
'''
for each in self.m_panel_comm.GetChildren():
if each != self.m_textCtrl_comm_receive:
each.Enable(switch)
for each in self.m_panel_para_adj.GetChildren():
each.Enable(switch)
self.m_button_update_uavinfo.Enable(switch)
for each in self.m_panel_uavctrl.GetChildren():
each.Enable(switch)
def load_default_comm_options(self):
'''
读取默认通信配置
'''
filepath = r'communication\xbee.gss'
try:
pfio = PickleFileIO.PickleFileIO(filepath)
comm_options = pfio.load()
self.sbar.update(u'默认通信设置"%s"已经应用。'%filepath)
except (EOFError,IOError),e:
self.m_statusBar.SetStatusText(u'未发现默认设置')
comm_options = {}
return comm_options
class InputHistory(object):
'''
输入区域历史记录管理器
'''
def __init__(self,window):
'''
初始化函数
@param window:窗口
'''
self.window = window
self.clear()
def clear(self):
'''
清除记录
'''
self.history=[]
self.cursor = -1
def add(self,s):
'''
添加记录
'''
self.history.append(s)
self.cursor = len(self.history)
def getByRel(self,rel):
'''
按相对位置获取记录
@param rel: 相对位置
@return: 记录内容,string
'''
ret = ""
try:
if self.cursor+rel == -1:
ret = self.history[0]
raise IndexError
if self.cursor+rel == len(self.history):
ret = self.history[self.cursor]
raise IndexError
ret = self.history[self.cursor+rel]
self.cursor = self.cursor+rel
self.window.SetStatusText("历史记录[%d]:%s"%(self.cursor+1,ret),0)
except IndexError:
self.window.SetStatusText("没有更多历史记录",0)
return ret
def getByAbs(self,cur):
'''
按绝对位置获取记录
@param cur: 绝对位置
@return: 记录内容,string
'''
try:
if cur == -1:
raise IndexError
ret = self.history[cur]
self.window.SetStatusText("历史记录:%s"%ret,0)
except IndexError:
self.window.setStatusText("没有更多历史记录",0)
ret = ""
return ret