Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #32115 > unrolled thread

How to set 250000 baud rate in pyserial ?

Started bykurabas@gmail.com
First post2012-10-25 04:09 -0700
Last post2012-10-26 15:01 -0700
Articles 7 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  How to set 250000 baud rate in pyserial ? kurabas@gmail.com - 2012-10-25 04:09 -0700
    Re: How to set 250000 baud rate in pyserial ? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-10-25 14:11 -0400
      Re: How to set 250000 baud rate in pyserial ? Grant Edwards <invalid@invalid.invalid> - 2012-10-25 21:14 +0000
      Re: How to set 250000 baud rate in pyserial ? kurabas@gmail.com - 2012-10-26 15:01 -0700
        Re: How to set 250000 baud rate in pyserial ? Michael Torrie <torriem@gmail.com> - 2012-10-26 16:08 -0600
        Re: How to set 250000 baud rate in pyserial ? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-10-27 00:43 -0400
      Re: How to set 250000 baud rate in pyserial ? kurabas@gmail.com - 2012-10-26 15:01 -0700

#32115 — How to set 250000 baud rate in pyserial ?

Fromkurabas@gmail.com
Date2012-10-25 04:09 -0700
SubjectHow to set 250000 baud rate in pyserial ?
Message-ID<d5871091-63c8-4582-9485-138195cf6d36@googlegroups.com>
I use Arduino 1280 and Arduino 2560 under Fedora 15. 
1280 creates ttyUSB0 port  and can be set at 2500000 successfully.
2560 creates ttyACM0 port and can be only set at speeds from list (no 250000) in pyserial. How to set 250000 to ttyACM0 port?? Need I patch kernel or python?

[toc] | [next] | [standalone]


#32152

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2012-10-25 14:11 -0400
Message-ID<mailman.2858.1351188682.27098.python-list@python.org>
In reply to#32115
On Thu, 25 Oct 2012 04:09:56 -0700 (PDT), kurabas@gmail.com declaimed
the following in gmane.comp.python.general:

> I use Arduino 1280 and Arduino 2560 under Fedora 15. 
> 1280 creates ttyUSB0 port  and can be set at 2500000 successfully.
> 2560 creates ttyACM0 port and can be only set at speeds from list (no 250000) in pyserial. How to set 250000 to ttyACM0 port?? Need I patch kernel or python?

	You don't say what error you are receiving but looking at the source
(serialposix.py) implies that it accepts nearly anything on Linux, and
relies on the OS returning a success/failure if the value is allowed or
not. 

	xxxBSD, SunOS, HPUX, IRIX, and CYGWIN systems don't allow "special"
baudrates at all.

	.NET, JVM, and Windows don't seem to have explicit call outs for bad
rates -- just a generic port configured OK test.
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#32167

FromGrant Edwards <invalid@invalid.invalid>
Date2012-10-25 21:14 +0000
Message-ID<k6ca3f$n17$1@reader1.panix.com>
In reply to#32152
On 2012-10-25, Dennis Lee Bieber <wlfraed@ix.netcom.com> wrote:
> On Thu, 25 Oct 2012 04:09:56 -0700 (PDT), kurabas@gmail.com wrote:
>
>> I use Arduino 1280 and Arduino 2560 under Fedora 15.  1280 creates
>> ttyUSB0 port and can be set at 2500000 successfully. 2560 creates
>> ttyACM0 port and can be only set at speeds from list (no 250000) in
>> pyserial. How to set 250000 to ttyACM0 port?? Need I patch kernel or
>> python?
>
> You don't say what error you are receiving but looking at the source
> (serialposix.py) implies that it accepts nearly anything on Linux, and
> relies on the OS returning a success/failure if the value is allowed or
> not. 

1) Are you sure it matters?  I've never played with an Arduino board,
   but other stuff I've used that implements a "virtual serial port"
   using a ttyUSB or ttyACM device (e.g. oscilloscope, various Atmel
   eval boards, JTAG interfaces, etc.) didn't have actual UARTs in
   them with real baud rate generators.  You got the same high-speed
   transfers no matter what baud rate you told the tty driver.

2) If you want a non-standard baud rate, there is a termios2 API on
   Linux that allows that (assuming the low-level driver and hardwdare
   support it).  The last time I looked, it wasn't supported by
   pyserial, but you can ask pyserial for the underlying file
   descriptor and do the ioctl manually.  The slightly ugly bit is
   that you'll have to use struct (or maybe ctypes) to handle the
   termios2 "C" structure.

   The behavior of baud rate requests that can't be met exactly is
   probably not very consistent.  IIRC, the recommended approach is
   for the low level driver to pick the closest supported baud, and
   then report the actual baud rate back when you subsequently read
   the termios2 structure. However, I do know of some devices that
   will always report the requested baud rate even if the physical
   baud rate that was selected wasn't exactly the same as the request
   rate.

Here's how you set an arbitrary baud rate in C:
   
-------------------------arbitrary-baud.c--------------------------
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <linux/termios.h>

int ioctl(int d, int request, ...);

int main(int argc, char *argv[])
{
  struct termios2 t;
  int fd,baud;

  if (argc != 3)
    {
      fprintf(stderr,"usage: %s <device> <baud>\n", argv[0]);
      return 1;
    }

  fd = open(argv[1], O_RDWR | O_NOCTTY | O_NDELAY);

  if (fd == -1)
      {
        fprintf(stderr, "error opening %s: %s", argv[1], strerror(errno));
        return 2;
      }

  baud = atoi(argv[2]);

  if (ioctl(fd, TCGETS2, &t))
    {
      perror("TCGETS2");
      return 3;
    }

  t.c_cflag &= ~CBAUD;
  t.c_cflag |= BOTHER;
  t.c_ispeed = baud;
  t.c_ospeed = baud;

  if (ioctl(fd, TCSETS2, &t))
    {
      perror("TCSETS2");
      return 4;
    }

  if (ioctl(fd, TCGETS2, &t))
    {
      perror("TCGETS2");
      return 5;
    }

  printf("actual speed reported %d\n", t.c_ospeed);
  return 0;
}

--------------------------------------------------------------------

-- 
Grant Edwards               grant.b.edwards
                                  at       
                              gmail.com    

[toc] | [prev] | [next] | [standalone]


#32237

Fromkurabas@gmail.com
Date2012-10-26 15:01 -0700
Message-ID<10b2bbd7-c2b5-409e-be83-bd11948e16a8@googlegroups.com>
In reply to#32152
Error is like cannot set special baud rate.
But as I said pyserial set this speed without problem  for ttyUSB0
So it seems pyserial uses diefferent code depending of port type.
I tried to simlink ln -s ttyACM0 ttyUSB0 but it does not work


On Thursday, October 25, 2012 9:11:23 PM UTC+3, Dennis Lee Bieber wrote:
> On Thu, 25 Oct 2012 04:09:56 -0700 (PDT), kurabas@gmail.com declaimed
> 
> the following in gmane.comp.python.general:
> 
> 
> 
> > I use Arduino 1280 and Arduino 2560 under Fedora 15. 
> 
> > 1280 creates ttyUSB0 port  and can be set at 2500000 successfully.
> 
> > 2560 creates ttyACM0 port and can be only set at speeds from list (no 250000) in pyserial. How to set 250000 to ttyACM0 port?? Need I patch kernel or python?
> 
> 
> 
> 	You don't say what error you are receiving but looking at the source
> 
> (serialposix.py) implies that it accepts nearly anything on Linux, and
> 
> relies on the OS returning a success/failure if the value is allowed or
> 
> not. 
> 
> 
> 
> 	xxxBSD, SunOS, HPUX, IRIX, and CYGWIN systems don't allow "special"
> 
> baudrates at all.
> 
> 
> 
> 	.NET, JVM, and Windows don't seem to have explicit call outs for bad
> 
> rates -- just a generic port configured OK test.
> 
> -- 
> 
> 	Wulfraed                 Dennis Lee Bieber         AF6VN
> 
>         wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#32240

FromMichael Torrie <torriem@gmail.com>
Date2012-10-26 16:08 -0600
Message-ID<mailman.2910.1351289302.27098.python-list@python.org>
In reply to#32237
On 10/26/2012 04:01 PM, kurabas@gmail.com wrote:
> Error is like cannot set special baud rate. But as I said pyserial
> set this speed without problem  for ttyUSB0 So it seems pyserial uses
> diefferent code depending of port type. I tried to simlink ln -s
> ttyACM0 ttyUSB0 but it does not work

No the difference in how baud rate is set is most likely in the driver.
 pyserial just uses standard kernel apis and ioctls to control the device.

[toc] | [prev] | [next] | [standalone]


#32265

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2012-10-27 00:43 -0400
Message-ID<mailman.2930.1351312998.27098.python-list@python.org>
In reply to#32237
On Fri, 26 Oct 2012 15:01:43 -0700 (PDT), kurabas@gmail.com declaimed
the following in gmane.comp.python.general:

> Error is like cannot set special baud rate.
> But as I said pyserial set this speed without problem  for ttyUSB0
> So it seems pyserial uses diefferent code depending of port type.

	Did you look at the source file?

	It doesn't know about "port type" -- it only differs by the OS in
use. Given the OS, the same system calls are invoked regardless of what
the device port "name" is.

-=-=-=-=- serialposix.py (extract)
if   plat[:5] == 'linux':    # Linux (confirmed)

    def device(port):
        return '/dev/ttyS%d' % port

    ASYNC_SPD_MASK = 0x1030
    ASYNC_SPD_CUST = 0x0030

    def set_special_baudrate(port, baudrate):
        import array
        buf = array.array('i', [0] * 32)

        # get serial_struct
        FCNTL.ioctl(port.fd, TERMIOS.TIOCGSERIAL, buf)

        # set custom divisor
        buf[6] = buf[7] / baudrate

        # update flags
        buf[4] &= ~ASYNC_SPD_MASK
        buf[4] |= ASYNC_SPD_CUST

        # set serial_struct
        try:
            res = FCNTL.ioctl(port.fd, TERMIOS.TIOCSSERIAL, buf)
        except IOError:
            raise ValueError('Failed to set custom baud rate: %r' %
baudrate)
-=-=-=-=-=-

	Practically all the other OS options (Mac, Sun, HP, Cygwin, etc.)
produce

    def set_special_baudrate(port, baudrate):
        raise ValueError("sorry don't know how to handle non standard
baud rate on this platform")


	Windows has its own module...
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#32238

Fromkurabas@gmail.com
Date2012-10-26 15:01 -0700
Message-ID<mailman.2908.1351288913.27098.python-list@python.org>
In reply to#32152
Error is like cannot set special baud rate.
But as I said pyserial set this speed without problem  for ttyUSB0
So it seems pyserial uses diefferent code depending of port type.
I tried to simlink ln -s ttyACM0 ttyUSB0 but it does not work


On Thursday, October 25, 2012 9:11:23 PM UTC+3, Dennis Lee Bieber wrote:
> On Thu, 25 Oct 2012 04:09:56 -0700 (PDT), kurabas@gmail.com declaimed
> 
> the following in gmane.comp.python.general:
> 
> 
> 
> > I use Arduino 1280 and Arduino 2560 under Fedora 15. 
> 
> > 1280 creates ttyUSB0 port  and can be set at 2500000 successfully.
> 
> > 2560 creates ttyACM0 port and can be only set at speeds from list (no 250000) in pyserial. How to set 250000 to ttyACM0 port?? Need I patch kernel or python?
> 
> 
> 
> 	You don't say what error you are receiving but looking at the source
> 
> (serialposix.py) implies that it accepts nearly anything on Linux, and
> 
> relies on the OS returning a success/failure if the value is allowed or
> 
> not. 
> 
> 
> 
> 	xxxBSD, SunOS, HPUX, IRIX, and CYGWIN systems don't allow "special"
> 
> baudrates at all.
> 
> 
> 
> 	.NET, JVM, and Windows don't seem to have explicit call outs for bad
> 
> rates -- just a generic port configured OK test.
> 
> -- 
> 
> 	Wulfraed                 Dennis Lee Bieber         AF6VN
> 
>         wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web