python script to interact with embedded board
Question: I want to write a Python script to interact with an Embedded board through
COM4.

I used Teraterm and could able to execute what I wanted. Basically, I just
want to get some information from the board.  
Ex: If I send Ver, the board replies back with Version number If I send Serv,
the board replies back with the list of its services

I don't know how to write a Python script to achieve the same.

Following is my code. _The problem is that ser.readline() is reading what I am
sending_. The output is: b'ver' and b'serv'.

Please suggest me what changes should I do in my script to make it work.

Thanks.

    
    
        import serial
        import time
        import sys
    
        ser = serial.Serial(baudrate=115200, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=5.0)
        ser.port = "COM4"
        ser.open()
        if ser.isOpen():
          print("Open: ",ser.portstr)
        print('--------')       
    
    
        ser.write(bytes('ver',encoding='ascii'))
        time.sleep(1)
        print(ser.readline())
    
        time.sleep(1)
    
        ser.write(bytes('serv',encoding='ascii'))
        time.sleep(1)
        print(ser.readline())
    
        ser.close()
    

To make it simple, I just used the following code. In this case, I am getting
a blank output as b' ' continuously.

    
    
    import serial
    import time
    import sys
    
    ser = serial.Serial(baudrate=115200, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=5.0)
    ser.port = "COM4"
    ser.open()
    if ser.isOpen():
      print("Open: ",ser.portstr)
    print('--------')       
    
    ser.write(bytes('ver',encoding='ascii'))
    while 1:
            print(ser.readline())
    
    ser.close()
    


Answer: Your code looks fine. When you send something over pySerial, what you get back
is what you sent, along with anything the board responds with. I think your
problem relates the the following lines:

    
    
    print(ser.readline())
    

If you look through the [readline()
documentation](http://pyserial.sourceforge.net/pyserial_api.html#serial.FileLike.readline)
you will notice that the readline() only reads up to the the eol character.

What your board returns might look something like this:

    
    
    serv           #your input
    \n             #newline character
    output_text    #board output
    \n             #newline character
    

but ser.readline() stops reading at the newline character because it only
reads one line.

You should be able to fix this by calling ser.readline() several times (at
least twice) until all of the output is read or use one of the [other reading
methods](http://pyserial.sourceforge.net/pyserial_api.html#serial.Serial.read)

