python方法調用
学习ing
学习ing 2017-06-12 09:23:06
0
2
1088

部分程式碼:

#打开串口
ser=serial.Serial('COM3', 9600)
#开启一个线程
th=threading.Thread(target=thread_read, args=(ser, msg_parsed))
th.start()

def thread_read(ser, callback=None):
     buf=b' '
     while running.is_set():
            buf=read_data(ser, buf,callback=None)

def read_data(ser, buf, callback=None):
     if callback is None:
            callback=print
     buf +=  ser.read(ser.inwaiting())

為啥在read_data()這個方法裡調用inwaiting(),報錯,告訴我has no attribute 'inwaiting()',在方法外面寫ser.inwaiting()正常,我不是已經把serial物件的引用傳入方法中了嗎?書大神解答!

学习ing
学习ing

全部回覆(2)
Peter_Zhu

你別傳對象,用全域變數試試

大家讲道理

print(dir(ser))印ser的所有方法,找到inWaitingin_waiting,沒有inwaiting,所以報錯。
你確定在外面調用的是inwaiting嗎?

['BAUDRATES', 'BYTESIZES', 'PARITIES', 'STOPBITS', '_GetCommModemStatus', '_SAVED_SETTINGS', '__abst
ractmethods__', '__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__'
, '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__'
, '__iter__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__next__', '__reduce__', '__re
duce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_abc_cache', '_
abc_negative_cache', '_abc_negative_cache_version', '_abc_registry', '_baudrate', '_break_state', '_
bytesize', '_cancel_overlapped_io', '_checkClosed', '_checkReadable', '_checkSeekable', '_checkWrita
ble', '_close', '_dsrdtr', '_dtr_state', '_inter_byte_timeout', '_orgTimeouts', '_overlapped_read',
'_overlapped_write', '_parity', '_port', '_port_handle', '_reconfigure_port', '_rs485_mode', '_rts_s
tate', '_rtscts', '_stopbits', '_timeout', '_update_break_state', '_update_dtr_state', '_update_rts_
state', '_write_timeout', '_xonxoff', 'applySettingsDict', 'apply_settings', 'baudrate', 'break_cond
ition', 'bytesize', 'cancel_read', 'cancel_write', 'cd', 'close', 'closed', 'cts', 'dsr', 'dsrdtr',
'dtr', 'fileno', 'flush', 'flushInput', 'flushOutput', 'getCD', 'getCTS', 'getDSR', 'getRI', 'getSet
tingsDict', 'get_settings', 'inWaiting', 'in_waiting', 'interCharTimeout', 'inter_byte_timeout', 'ir
ead_until', 'isOpen', 'is_open', 'isatty', 'name', 'open', 'out_waiting', 'parity', 'port', 'portstr
', 'read', 'read_all', 'read_until', 'readable', 'readall', 'readinto', 'readline', 'readlines', 're
set_input_buffer', 'reset_output_buffer', 'ri', 'rs485_mode', 'rts', 'rtscts', 'seek', 'seekable', '
sendBreak', 'send_break', 'setDTR', 'setPort', 'setRTS', 'set_buffer_size', 'set_output_flow_control
', 'stopbits', 'tell', 'timeout', 'truncate', 'writable', 'write', 'writeTimeout', 'write_timeout',
'writelines', 'xonxoff']
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!