Re: [python.tw] 不好意思,我想詢問關於PYTHON與RS232方面的問題

1,031 views
Skip to first unread message

pingooo

unread,
Apr 29, 2013, 5:05:19 AM4/29/13
to pythontw
很久沒寫 serial 了,不太記得細節,不過要寫去 device 的東西是否該是個字串而不是清單(list)呢?類似:
  device.write(''.join(rslist))



2013/4/29 <devi...@gmail.com>
我目前是連接IR-8302ex 紅外線集收控制器,IR-8302ex 紅外線集收控制器硬體學習的部分已經設定好了。

現在python上,我是這樣寫。

import serial
device = serial.Serial()
device.port = 3
device.baudrate = 9600
print device.portstr
device.open()
while(1):
        rslist = [0xE0, 0x0, 0x43, 0x0, 0x1, 0x0, 0x30, 0x30, 0x31, 0xEF, 0xFF, 0xFF, 0xFF, 0xE0, 0x0, 0x43, 0x0, 0xA0, 0xAA, 0x30, 0x30, 0x30, 0xEF]
        device.write(rslist)
device.close()


可以成功執行,但只有顯示 COM4 這個字樣,利用IR-8302ex 紅外線集收控制器控制的冷氣毫無動作,因此想問看看,我Python的部份是否有寫錯的地方。

--
您已訂閱「Google 網上論壇」的「python.tw」群組,因此我們特別傳送這封郵件通知您。
如要取消訂閱這個群組並停止接收來自這個群組的郵件,請傳送電子郵件到 pythontw+u...@googlegroups.com
如要在此群組張貼留言,請傳送電子郵件至 pyth...@googlegroups.com
請前往以下網址造訪這個群組:http://groups.google.com/group/pythontw?hl=zh-TW。
如需更多選項,請前往:https://groups.google.com/groups/opt_out。
 
 

Mosky Liu

unread,
Apr 29, 2013, 7:12:56 AM4/29/13
to pyth...@googlegroups.com
int list 不能直接被 join,搭配 pingooo 說的,就是:

''.join(map(chr, rslist))

pingooo

unread,
Apr 29, 2013, 1:54:11 PM4/29/13
to pythontw
mosky ++


2013/4/29 Mosky Liu <mosk...@gmail.com>

letoh

unread,
May 3, 2013, 12:27:41 AM5/3/13
to pyth...@googlegroups.com
除非真的很喜歡或很需要 string,例如 target 其實是一個 serial console,否則在很多情況下想操作 serial port 的資料,不建議轉成 str 操作。除了寫起來多繞了一圈,而且 str 可能多塞了無法處理的資料,想計算時還得在 chr/ord 之間轉來轉去很麻煩... (type 根本就已經不對了)

用 python 2.6 以上版本的話可以考慮一下 bytearray,如果有 python3 可以用的話,bytes literal 可能會方便一點。這些都是直接以 int 型態的操作,在處理 sensor 數值時會方便很多。如果不幸還在用 2.5 以前的版本 (應該沒有人比我更不幸了XD)  pyserial 好像有給一個相容的 bytearray 實作可以用


回到原問題。只顯示 COM4 的話沒辦法知道底下送資料的動作到底對不對,
「執行成功」的判斷依據是?印象中送不出去會噴 exception,list 不行的話
應該會先看到 exception 才對...
但保險一點還是可以掛儀器檢查一下,或是先簡單看一下 write 的傳回值

devi...@gmail.com

unread,
May 24, 2013, 9:06:10 AM5/24/13
to pyth...@googlegroups.com
謝謝各位幫忙,這部份已經能夠成功執行了。
Reply all
Reply to author
Forward
0 new messages