0

I am trying to write a code that talks with the serial interface using a USB to serial converter. I am running my code on a Ubuntu 12.04 OS. I have attached the device and it looks like it is recognized by the system, as the output of the lsusb gives me:

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

and the dmesg command reports:

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

However when I try to run my code I do not get the expected answer from the other party. The code I'm running is here:

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()

I expect a reply like:

0x7E 0x04 0x00 CRC1 CRC2

but instead I get:

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

Is there something I am overlooking here? Thanks in advance!

toti08
  • 2,448
  • 5
  • 24
  • 36
  • Do you have something to debug your serial port? Like a protocol analyzer? Maybe a scope? – DSLima90 Apr 19 '17 at 11:00
  • Unfortunately not...I will try now to connect two of the converter outputs together and see if I can write on one side and read on the other one. – toti08 Apr 19 '17 at 11:29
  • Ok, tried this and it worked...so I guess it's something wrong with the other computer... – toti08 Apr 19 '17 at 12:09
  • Maybe some error in baud rate? What is the other device? When I am working with embeded devices I almost always have to modify the baud a little bit... – DSLima90 Apr 19 '17 at 12:59
  • The other device is an MVME 4100 board, with a PowerPC microcontroller. Code is written using VxWorks and the baud rate is set to 9600 on both sides... – toti08 Apr 19 '17 at 13:20
  • Yeah, your python code is just fine, probably some configuration (or bug) in the other device... – DSLima90 Apr 19 '17 at 13:29
  • I guess so, I'll keep investigating that! Thanks a lot for your help! – toti08 Apr 19 '17 at 13:56

1 Answers1

1

If you view that result in ASCII instead of hex, you get:

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

(The token in quotes is hex 0x10, one of the characters you're trying to send.) So, your serial reception is working fine - you just aren't getting the results you expected. It looks like the other system is at a textual prompt, and is not expecting the binary packet you're sending. There may need to be some command you have to send first, to put it into the right mode.

jasonharper
  • 9,450
  • 2
  • 18
  • 42
  • Thanks for the hint, this is helpful! I got the problem solved by re-programming the other board, it got probably corrupted. But I should take this into account for the future! – toti08 Jul 11 '17 at 13:28