def login(ip, port, username, password, device_info, error_code):
"""
LLONG CLIENT_Login(
char *pchDVRIP, WORD wDVRPort,
char *pchUserName, char *pchPassword,
LPNET_DEVICEINFO lpDeviceInfo, int *error = 0);
:param ip:
:param port:
:param username:
:param password:
:param device_info:
:param error_code:
:return: LLONG
"""
ip_buffer = c_buffer(ip)
# ip_buffer.encode('utf8')
# user_id = c_longlong(0)
user_id = SDK._dll.CLIENT_Login(byref(ip_buffer), port, username, password, byref(device_info), byref(error_code))
return user_id # c_longlong(user_id).value
用戶(hù)ID作為句柄,傳入其他SDK函數(shù)中,報(bào)錯(cuò),句柄無(wú)效。查看出現(xiàn)負(fù)值。因此懷疑是類(lèi)型不匹配
網(wǎng)上查了下,并看了下文檔,python中調(diào)用C的sdk,默認(rèn)返回的是int型,按照l(shuí)ogin C版本的函數(shù)定義,返回的是LLONG型
By default functions are assumed to return the C int
type. Other return types can be specified by setting the restype
attribute of the function object.
Here is a more advanced example, it uses the strchr
function, which expects a string pointer and a char, and returns a pointer to a string:
>>> strchr = libc.strchr >>> strchr("abcdef", ord("d")) 8059983 >>> strchr.restype = c_char_p # c_char_p is a pointer to a string >>> strchr("abcdef", ord("d")) 'def' >>> print strchr("abcdef", ord("x")) None >>>
設(shè)置sdk函數(shù)的返回值為c_longlong,問(wèn)題解決
SDK._dll.CLIENT_Login.restype = c_longlong
網(wǎng)上找了一圈,一般兩種可能性:內(nèi)存越界或者讀寫(xiě)非法; 還有一種就是函數(shù)調(diào)用棧太深。
代碼本身就添加了Condition讀寫(xiě)鎖得,buf也是在寫(xiě)的時(shí)候分配的,多番調(diào)試,應(yīng)該不是這個(gè)地方因?yàn)榈膯?wèn)題。打印日志看,也與讀寫(xiě)操作無(wú)關(guān)。
寫(xiě)
index = userdata # c_uint(userdata).value _buf_cond.acquire() # time.sleep(0.2) # 復(fù)制圖片到內(nèi)存 # _pic_buf.buf = pBuf c_char 和 c_byte轉(zhuǎn)換 try: temp = [pBuf[i] for i in xrange(0, RevLen)] _buf_list[index].buf = struct.pack('%db' % RevLen, *temp) # 序列號(hào) _buf_list[index].sn = c_ulong(CmdSerial).value _buf_list[index].id = index _buf_list[index].size = c_uint(RevLen).value _buf_list[index].ext = 'jpeg' # encode_dict.get(EncodeType, 'jpeg') except Exception, e: logger.error('截圖緩存發(fā)生異常:%s' % str(e)) finally: _buf_cond.notify() _buf_cond.release()
讀
_buf_cond.acquire()
_buf_cond.wait(timeout=15.0) # 等待200ms再訪(fǎng)問(wèn)數(shù)據(jù) # time.sleep(0.2) if _buf_list[self.index].sn == snap.CmdSerial and _buf_list[self.index].id == self.index: self.save_picture(_buf_list[self.index].buf, _buf_list[self.index].ext) self.info('針對(duì)通道%d截圖成功,IP:%s,Port:%s' % (channel, self.ip, self.port)) pass _buf_cond.release()
由于引入這個(gè)sdk之后,使用了回調(diào)函數(shù)。因此將回調(diào)函數(shù)定義層次減少。
傳入函數(shù)給基類(lèi),在基類(lèi)中CFUNCTYPE實(shí)例化函數(shù)
基類(lèi)中定義
self.callback = CFUNCTYPE(c_void_p, c_longlong, POINTER(c_byte), c_uint, c_uint, c_ulong, c_ulonglong)
def set_callback(self, save_after_recv_pic, index): self.dll.CLIENT_SetSnapRevCallBack(self._callback(save_after_recv_pic), index)
子類(lèi)中定義,_save_after_recv_pic也在子類(lèi)中定義為staticmethod
def _set_callback(self): try: if 0 <= self.index < _buf_size:
self.set_callback(self._save_after_recv_pic, self.index) # 函數(shù)調(diào)用層次太深,經(jīng)常報(bào)segmentation fault return True else: self.error('設(shè)置截圖保存回調(diào)函數(shù)的userdata參數(shù)錯(cuò)誤:%d' % self.index) return False except Exception, e: self.error('設(shè)置截圖保存回調(diào)函數(shù)失敗,%s' % str(e)) return False
子類(lèi)中直接實(shí)例化回調(diào)函數(shù)
self.capture_callback = self.callback(self._save_after_recv_pic)
子類(lèi)中直接注冊(cè)回調(diào)函數(shù)
def _set_callback(self): try: if 0 <= self.index < _buf_size: self.dll.CLIENT_SetSnapRevCallBack(self.capture_callback, self.index) # self.set_callback(self._save_after_recv_pic, self.index) # 函數(shù)調(diào)用層次太深,經(jīng)常報(bào)segmentation fault return True else: self.error('設(shè)置截圖保存回調(diào)函數(shù)的userdata參數(shù)錯(cuò)誤:%d' % self.index) return False except Exception, e: self.error('設(shè)置截圖保存回調(diào)函數(shù)失敗,%s' % str(e)) return False
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com