python方法調用
問題描述
部分代碼:
#打開串口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對象的引用傳入方法中了嗎?書大神解答!
問題解答
回答1:你別傳對象,用全局變量試試
回答2:print(dir(ser))打印ser的所有方法,找到inWaiting和in_waiting,沒有inwaiting,所以報錯。你確定在外面調用的是inwaiting嗎?
[’BAUDRATES’, ’BYTESIZES’, ’PARITIES’, ’STOPBITS’, ’_GetCommModemStatus’, ’_SAVED_SETTINGS’, ’__abstractmethods__’, ’__class__’, ’__del__’, ’__delattr__’, ’__dict__’, ’__dir__’, ’__doc__’, ’__enter__’, ’__eq__’, ’__exit__’, ’__format__’, ’__ge__’, ’__getattribute__’, ’__gt__’, ’__hash__’, ’__init__’, ’__iter__’, ’__le__’, ’__lt__’, ’__module__’, ’__ne__’, ’__new__’, ’__next__’, ’__reduce__’, ’__reduce_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’, ’_checkWritable’, ’_close’, ’_dsrdtr’, ’_dtr_state’, ’_inter_byte_timeout’, ’_orgTimeouts’, ’_overlapped_read’,’_overlapped_write’, ’_parity’, ’_port’, ’_port_handle’, ’_reconfigure_port’, ’_rs485_mode’, ’_rts_state’, ’_rtscts’, ’_stopbits’, ’_timeout’, ’_update_break_state’, ’_update_dtr_state’, ’_update_rts_state’, ’_write_timeout’, ’_xonxoff’, ’applySettingsDict’, ’apply_settings’, ’baudrate’, ’break_condition’, ’bytesize’, ’cancel_read’, ’cancel_write’, ’cd’, ’close’, ’closed’, ’cts’, ’dsr’, ’dsrdtr’,’dtr’, ’fileno’, ’flush’, ’flushInput’, ’flushOutput’, ’getCD’, ’getCTS’, ’getDSR’, ’getRI’, ’getSettingsDict’, ’get_settings’, ’inWaiting’, ’in_waiting’, ’interCharTimeout’, ’inter_byte_timeout’, ’iread_until’, ’isOpen’, ’is_open’, ’isatty’, ’name’, ’open’, ’out_waiting’, ’parity’, ’port’, ’portstr’, ’read’, ’read_all’, ’read_until’, ’readable’, ’readall’, ’readinto’, ’readline’, ’readlines’, ’reset_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’]
相關文章:
1. android - NavigationView 的側滑菜單中如何保存新增項(通過程序添加)2. python - Pycharm一句代碼寫完可以自動補全空格么?3. 提示語法錯誤語法錯誤: unexpected ’abstract’ (T_ABSTRACT)4. python - xpath提取網頁路徑沒問題,但是缺失內容?5. mysql服務無法啟動1067錯誤,誰知道正確的解決方法?6. tp5 不同控制器中的變量調用問題7. 除了 python2 和 python3,ipython notebook 還可以用哪些內核?8. php7.3.4中怎么開啟pdo驅動9. 這段代碼既不提示錯誤也看不到結果,請老師明示錯在哪里,謝謝!10. 老師 我是一個沒有學過php語言的準畢業生 我希望您能幫我一下
