2017-04-19 119 views
0

我正在嘗試編寫一個代碼,使用串口轉換器與串行接口進行通信。 我在Ubuntu 12.04操作系統上運行我的代碼。我重視的設備,它看起來就像是被系統識別,爲的lsusb的輸出給了我:Python:與USB到串行轉換器的串行通信失敗

Bus 002 Device 003: ID 9710:7840 MosChip Semiconductor MCS7820/MCS7840 2/4 port serial adapter 

dmesg的命令報告:

Tue Apr 18 15:22:07 2017] usb 2-2: new high-speed USB device number 3 using ehci_hcd 
[Tue Apr 18 15:22:07 2017] usbcore: registered new interface driver usbserial 
[Tue Apr 18 15:22:07 2017] USB Serial support registered for generic 
[Tue Apr 18 15:22:07 2017] usbcore: registered new interface driver usbserial_generic 
[Tue Apr 18 15:22:07 2017] usbserial: USB Serial Driver core 
[Tue Apr 18 15:22:07 2017] USB Serial support registered for Moschip 7840/7820 USB Serial Driver* 
[Tue Apr 18 15:22:07 2017] mos7840: 1.3.2:Moschip 7840/7820 USB Serial Driver 
[Tue Apr 18 15:22:07 2017] mos7840 2-2:1.0: Moschip 7840/7820 USB Serial Driver converter detected 
[Tue Apr 18 15:22:07 2017] usb 2-2: Moschip 7840/7820 USB Serial Driver converter now attached to ttyUSB0 
[Tue Apr 18 15:22:07 2017] usb 2-2: Moschip 7840/7820 USB Serial Driver converter now attached to ttyUSB1 
[Tue Apr 18 15:22:07 2017] usb 2-2: Moschip 7840/7820 USB Serial Driver converter now attached to ttyUSB2 
[Tue Apr 18 15:22:07 2017] usb 2-2: Moschip 7840/7820 USB Serial Driver converter now attached to ttyUSB3 
[Tue Apr 18 15:22:07 2017] usbcore: registered new interface driver mos7840 

然而,當我嘗試運行我的代碼我沒有得到對方的預期答案。我跑的代碼是在這裏:

class RTSSerial(object): 

    def __init__(self,serialID, baud): 
     self.serialID = serialID 
     self.baud = baud 
     try: 
      self.serDevice = serial.Serial(self.serialID, 
              baudrate = self.baud, 
              parity = serial.PARITY_NONE, 
              stopbits = serial.STOPBITS_ONE, 
              bytesize = serial.EIGHTBITS) 
      self.serDevice.flushInput() 
      self.serDevice.flushOutput() 
     except Exception as e: 
      print 'Got the following exception: ', e 

    def read(self):   
     try: 
      inWaiting = self.serDevice.inWaiting() 
     except:    
      if os.path.exists(self.serialID): 
       self.serDevice = serial.Serial(self.serialID,self.baud) 
      inWaiting = 0 

     try: 
      rec = '' 
      if (inWaiting > 0): 
       rec = self.serDevice.read(inWaiting) 
       #Convert string message into list of integers   
      message = [ord(x) for x in rec]   
     except Exception as e: 
      message = [] 
      print 'Got the following exception: ', e 

     return message 

    #Function to write to serial 
    #message should be a list. Then the function converts it into a string 
    def write(self, message):  
     try: 
      #Convert message into string 
      messageToSend = "".join(chr(x) for x in message)   
      errCode = self.serDevice.write(messageToSend) 
     except Exception as e: 
      errCode = 0 
      print 'Got the following exception: ', e 

     return errCode 

ser = RTSSerial('/dev/ttyUSB0', 9600)  
mess = [0x7e, 0xa, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x0, 0x4d, 0x8a] 
ser.write(mess) 
time.sleep(1) 
reply = ser.read() 

我希望像一個答覆:

0x7E 0x04 0x00 CRC1 CRC2 

而是我得到:

0x7e 0xd 0xa 0x43 0x20 0x69 0x6e 0x74 0x65 0x72 0x70 0x3a 0x20 0x73 0x79 0x6e 0x74 0x61 0x78 0x20 0x65 0x72 0x72 0x6f 0x72 0x2e 0xd 0xa 0x2d 0x3e 0x20 0x3f 0x4d 0xd 0xa 0x43 0x20 0x69 0x6e 0x74 0x65 0x72 0x70 0x3a 0x20 0x74 0x6f 0x6b 0x65 0x6e 0x20 0x27 0x10 0x27 0x20 0x6e 0x6f 0x74 0x20 0x72 0x65 0x63 0x6f 0x67 0x6e 0x69 0x7a 0x65 0x64 0x2e 0xd 0xa 0x2d 0x3e 0x20 

有我在這裏可以俯瞰什麼?提前致謝!

+0

你有什麼要調試你的串口?像協議分析儀一樣?也許是一個範圍? – DSLima90

+0

不幸的是,我現在會嘗試將兩個轉換器輸出端連接在一起,看看我是否可以在一面上書寫並在另一面上閱讀。 – toti08

+0

好吧,試過這個,它工作...所以我想這是其他電腦有問題... – toti08

回答

1

如果您查看結果在ASCII而不是十六進制,你會得到:

~ 
C interp: syntax error. 
-> ?M 
C interp: token '' not recognized. 
-> 

(引號中的令牌是十六進制0x10的,你想送的人物之一。)所以,你的串口接待工作正常 - 你只是沒有得到你預期的結果。它看起來像另一個系統在文本提示,並不期望您發送的二進制數據包。可能需要先發送一些命令,才能將其置於正確的模式。

+0

感謝您的提示,這很有幫助!我通過重新編程其他電路板解決了問題,它可能已損壞。但我應該考慮到未來! – toti08