Pyserial output does not change

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bryanleo
    New Member
    • Sep 2008
    • 3

    Pyserial output does not change

    We are trying to read data from a microcontroller and interface it through serial port. The output is then displayed in Python using Pyserial or the hyperterminal, the former is more important

    When you touch the input pins of the microcontroller the value changes real time in hyper terminal. But In the case of the pyserial module, even though you touch the input pins, the value does not change. We have actually noticed that python records and prints the input to the pins of the microcontroller before the code is executed in python. Hence, it does not record and print new inputs once the code was been executed.

    What could possibly be wrong with this?

    CODE:
    import serial,csv,sys


    print('Tactile Sensing Feedback for Medical Palpation in MIS\n Pyserial Testing Module\n')
    ch= raw_input("Ente r a to initialize pyserial module, b to quit:")

    if ch == 'a':
    print 'Initializing Pyserial module'
    ser = serial.Serial(0 ,baudrate=57600 ,timeout=0,xonx off=0)
    print ser.portstr
    ser.flushInput( )

    for x in range (0,100):
    s = ser.readline()
    print repr(s)
    ser.close()

    elif ch == 'b':
    print 'closing port......\n'
    quit()

    else:
    print 'Invalid option please input a or b only'
  • bryanleo
    New Member
    • Sep 2008
    • 3

    #2
    Example.

    When I touch the pins of the microcontroller it outputs a certain value. And then when I execute the program in python it outputs that data from the microcontroller . But when I release my hand or input a certain voltage to the pins of the microcontroller (while the code is running), the output (which is a continuous stream) does not change and still prints the data when the pins were touched.

    So no matter how you change the output of the MCU (which is the input to the serial line), output in python does not change and will just retain the value of the output of the MCU at the instant the script in python was executed. So its like the serial line isn't accepting new inputs.

    New Findings:

    I have actually used the flushInput() command and is incorporated in the code

    for x in range (0,100):
    s = ser.readline(16 )
    flushInput()
    print (s)
    ser.close()

    I have noticed that the value now changes when I touch the pins of the MCU. This leads me to the conclusion that the Input buffer does not dumped the initial data it recorded. Now by using flushInput(), the input buffer dumped the old data and accepts new ones but the data printed is disorganized. Why did the input buffer not update when there was new data????

    So I have a new problem, which is to organize the data. haha

    Comment

    • bryanleo
      New Member
      • Sep 2008
      • 3

      #3
      The flushInput() successfully solved the output changing part. Data seem not to change probably because...

      Here are my theories:
      1. The Input buffer was not accepting new ones. Since there was not interrupt to signal the CPU to read the contents of the Input buffer
      2. Pyserial was reading the data in the input buffer way too slowly. But its so weird how the data can be read this slow.


      Any thoughts on this?

      Thanks for the Help

      Comment

      Working...