Thread: Pyserial help

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0

    Pyserial help


    Ok so I have code that will print out the value of the slider to idle every 100 ms but how can i send this value to the serial port.
    My current code looks like this:

    import serial
    import time
    import Tkinter

    ser = serial.Serial('/dev/tty.usbmodem1411', 9600)
    time.sleep(2)

    root = Tkinter.Tk()

    w = Tkinter.Scale(root, from_=0, to=100)
    w.pack()

    def show_slider(root,w):
    millisecond = 1
    print(w.get())
    root.after(100*millisecond,show_slider,root,w)

    show_slider(root,w)

    root.mainloop()
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,856
    Rep Power
    481
    For python code please use [code] tags instead of [quote] tags.

    I expect you'd open a file and write to it. On DOS the file might be called COM1, SER1, or even LPT1:

    In unix I expect the file would be accessible somewhere in /dev . Haven't done it. (In 1983 I wrote a BASICA program that communicated with a serial device. Come to think of it, I also worked on a FORTRAN program that communicated with an external device with that microcomputer operating system that preceded DOS.)
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0
    [QUOTE=b49P23TIvg]For python code please use [code] tags instead of
    tags.

    I expect you'd open a file and write to it. On DOS the file might be called COM1, SER1, or even LPT1:

    In unix I expect the file would be accessible somewhere in /dev . Haven't done it. (In 1983 I wrote a BASICA program that communicated with a serial device. Come to think of it, I also worked on a FORTRAN program that communicated with an external device with that microcomputer operating system that preceded DOS.)
    Haha sorry. I have the path to the microcontroller setup in the code and i can write to the serial port (ser.write("hello")) but i need the value of the scale to go to the serial port. Not the IDLE.

    and i cant just put ser.write(w.get()) like i thought i would be able to, i get an error.
    I'm so confused haha. Thanks
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,856
    Rep Power
    481
    Oh! Sorry, you do have
    ser = serial.Serial('/dev/tty.usbmodem1411', 9600)

    OK, so the problem may be data type. w.get() returns an integer.

    ser.write(str(w.get()))

    and of course that still might not work because Serial may need bytes.

    Anyway, the problem may be data type. When debugging python the data type is often more useful to know than a human representation.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Oh! Sorry, you do have
    ser = serial.Serial('/dev/tty.usbmodem1411', 9600)

    OK, so the problem may be data type. w.get() returns an integer.

    ser.write(str(w.get()))

    and of course that still might not work because Serial may need bytes.

    Anyway, the problem may be data type. When debugging python the data type is often more useful to know than a human representation.
    Yes! it worked thankyou

IMN logo majestic logo threadwatch logo seochat tools logo