FAQ
hello, i'm new in python, i used to program in C,
i have a small problem, i tryed to do some serial port things
manipulation
with python.
i have something like:

import sys,termios

fd = sys.stdin.fileno()
term_conf=termios.tcgetattr(fd);

now i want to modify the actuall values in term_conf zone to zero
i don't see how to do it,
in C we can do : bzero(&term_conf,sizeof(struct termios));
i want to know if it exist a similar function in python, thanks

Search Discussions

  • Diez B. Roggisch at Aug 30, 2009 at 8:58 pm

    Mug schrieb:
    hello, i'm new in python, i used to program in C,
    i have a small problem, i tryed to do some serial port things
    manipulation
    with python.
    i have something like:

    import sys,termios

    fd = sys.stdin.fileno()
    term_conf=termios.tcgetattr(fd);

    now i want to modify the actuall values in term_conf zone to zero
    i don't see how to do it,
    in C we can do : bzero(&term_conf,sizeof(struct termios));
    i want to know if it exist a similar function in python, thanks
    In python you don't modify memory like that.

    For the above function, you pass a value as the one you got to
    tcgetattr, with values modified as you desire them.

    Diez
  • Mug at Aug 30, 2009 at 9:59 pm

    On Aug 30, 8:58?pm, "Diez B. Roggisch" wrote:
    Mug schrieb:
    hello, i'm new in python, i used to program in C,
    i have a small problem, i tryed to do some serial port things
    manipulation
    with python.
    i have something like:
    import sys,termios
    fd = sys.stdin.fileno()
    term_conf=termios.tcgetattr(fd);
    now i want to modify the actuall values in term_conf ?zone to zero
    i don't see how to do it,
    in C we can do : bzero(&term_conf,sizeof(struct termios));
    i want to know if it exist a similar function in python, thanks
    In python you don't modify memory like that.

    For the above function, you pass a value as the one you got to
    tcgetattr, with values modified as you desire them.
    i tryed
    print term_conf
    and i saw that the term_conf is actually represent with an array in
    python:

    zsh/3 4201 [1] % python test.py
    [27906, 5, 1215, 35387, 15, 15, ['\x03', '\x1c', '\x7f', '\x15',
    '\x04', '\x00', '\x01', '\xff', '\x11', '\x13', '\x1a', '\xff',
    '\x12', '\x0f', '\x17', '\x16', '\xff', '\x00', '\x00', '\x00',
    '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
    '\x00', '\x00', '\x00', '\x00']]

    it's a array of 7 elements, with the seventh element it self a array,
    so in order to initialize them to zero, there's no other way than
    modify them
    one by one in a loop? is there a fonction like "memset" or "bzero" in
    python?
    Diez
  • R at Aug 31, 2009 at 12:51 am
    On Aug 30, 4:59?pm, Mug wrote:
    (snip)
    is there a fonction like "memset" or "bzero" in python?
    Not that i know of ;). Try this link for a coverage of the Python
    built-in functions. You cannot write quality Python code without
    them.
    http://docs.python.org/3.1/library/functions.html

    What you refer to as an array is called a list round here, see this
    informative read
    http://docs.python.org/3.1/library/stdtypes.html#sequence-types-str-bytes-bytearray-list-tuple-range

    And be sure to check out list comprehentions too, just icing on the
    cake ;)

    python 3000 docs
    http://docs.python.org/3.1/
  • Diez B. Roggisch at Aug 31, 2009 at 8:08 am

    Mug schrieb:
    On Aug 30, 8:58 pm, "Diez B. Roggisch" wrote:
    Mug schrieb:
    hello, i'm new in python, i used to program in C,
    i have a small problem, i tryed to do some serial port things
    manipulation
    with python.
    i have something like:
    import sys,termios
    fd = sys.stdin.fileno()
    term_conf=termios.tcgetattr(fd);
    now i want to modify the actuall values in term_conf zone to zero
    i don't see how to do it,
    in C we can do : bzero(&term_conf,sizeof(struct termios));
    i want to know if it exist a similar function in python, thanks
    In python you don't modify memory like that.

    For the above function, you pass a value as the one you got to
    tcgetattr, with values modified as you desire them.
    i tryed
    print term_conf
    and i saw that the term_conf is actually represent with an array in
    python:

    zsh/3 4201 [1] % python test.py
    [27906, 5, 1215, 35387, 15, 15, ['\x03', '\x1c', '\x7f', '\x15',
    '\x04', '\x00', '\x01', '\xff', '\x11', '\x13', '\x1a', '\xff',
    '\x12', '\x0f', '\x17', '\x16', '\xff', '\x00', '\x00', '\x00',
    '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
    '\x00', '\x00', '\x00', '\x00']]

    it's a array of 7 elements, with the seventh element it self a array,
    so in order to initialize them to zero, there's no other way than
    modify them
    one by one in a loop? is there a fonction like "memset" or "bzero" in
    python?
    Again: no, there is no such function. But for the above, you'd easily write:

    foo = [0] * 7 # only do this with immutables
    foo[-1] = ['\x00'] * 16

    alternatively, you modify the result you got from tcgetattr in the
    places you want.



    Diez
  • Grant Edwards at Aug 31, 2009 at 12:25 am

    On 2009-08-30, Mug wrote:

    hello, i'm new in python, i used to program in C, i have a
    small problem, i tryed to do some serial port things
    You're probably better off just using pyserial:

    http://pyserial.sourceforge.net/

    If you really want to muck about with termios stuff, you can
    look at the posix serial module in pyserial to see how it's
    done.
    import sys,termios

    fd = sys.stdin.fileno()
    term_conf=termios.tcgetattr(fd);

    now i want to modify the actuall values in term_conf zone to
    zero
    Have you looked at the documentation for termios.tcgettattr()?

    It returns a list of integers. If you want set them to zero,
    then just set them to zero.

    If you want a configuration that's all zeros, why bother
    calling tcgetattr() at all? Just set the configuration to
    zeros:

    term_conf = [0,0,0,0,0,0]

    --
    Grant
  • Grant Edwards at Aug 31, 2009 at 12:37 am

    On 2009-08-31, Grant Edwards wrote:

    If you want a configuration that's all zeros, why bother
    calling tcgetattr() at all? Just set the configuration to
    zeros:

    term_conf = [0,0,0,0,0,0]
    Oops, I forgot about cc. That should be:

    term_conf = [0,0,0,0,0,0,['\x00']*32]

    That's why it's easier to just use pyserial ;)

    --
    Grant

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedAug 30, '09 at 8:33p
activeAug 31, '09 at 8:08a
posts7
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase