Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: How to read serial port with WIN32:SerialPort?

by jmlynesjr (Deacon)
on Apr 09, 2021 at 02:54 UTC ( #11131041=note: print w/replies, xml ) Need Help??


in reply to How to read serial port with WIN32:SerialPort?

I've only used Device::SerialPort under Linux, but, your stated data format looks funny. 1 start bit is ok. 1 stop bit is ok. 8 data bits dictates no parity, not odd parity. Serial data bytes are 8 bits, not 11.

Sample code:

# Perl Polling Test # Title: Perl to chipKIT Serial Poll Test # Author: James M. Lynes, Jr. # Version: 1.0 # Creation Date: June 10, 2012 # Modification Date: June 10, 2012 use strict; use warnings; # Initialize the serial port - creates the serial port object $Arduino use Device::SerialPort::Arduino; my $Arduino = Device::SerialPort::Arduino->new( port => '/dev/ttyUSB0', baudrate => 9600, databits => 8, parity => 'none', ); while(1) { $Arduino->communicate('P'); # Send a poll print ($Arduino->receive(), "\n"); # Print poll respons +e sleep(1); # Delay until next p +oll }
package Device::SerialPort::Arduino; use strict; use warnings; use Time::HiRes; use Carp; use Device::SerialPort; use vars qw($VERSION); our $VERSION = '0.07'; sub new { my $class = shift; my $self = bless {}, $class; my %init = @_; # Sets many parameters for Device::SerialPort usage $self->{'port'} = $init{'port'}; $self->{'baudrate'} = $init{'baudrate'}; $self->{'databits'} = $init{'databits'}; $self->{'parity'} = $init{'parity'}; $self->{'stopbits'} = $init{'stopbits'}; $self->initialize(); return $self; } sub initialize { my $self = shift; $self->{'DSP'} = Device::SerialPort->new( $self->{'port'} ) or croak "Can't open " . $self->{'port'} . " - $!\n";

James

There's never enough time to do it right, but always enough time to do it over...

Replies are listed 'Best First'.
Re^2: How to read serial port with WIN32:SerialPort?
by afoken (Canon) on Apr 09, 2021 at 14:14 UTC
    your stated data format looks funny. 1 start bit is ok. 1 stop bit is ok. 8 data bits dictates no parity, not odd parity. Serial data bytes are 8 bits, not 11.

    That's not quite correct. With a PC-standard serial port (i.e. 8250, 16450, 16550, and generations of clones embedded in various chipsets), you can freely choose any combination of 5 to 8 data bits, 1 or 2 stop bits, and None, Even, or Odd parity. There is always only one start bit. Combined, the minimum number of bits in one serial frame is used in 5N1, i.e. one start bit, 5 data bits, No parity bit, 1 stop bit, for a total of only 7 bit. The maximum number is 8E2 or 8O2, i.e. one start bit, 8 data bits, 1 parity bit (even or odd), 2 stop bits, for a total of 12 bit. (Note: Nobody uses the total number of bits. Only the number of data and stop bits are specified.)

    5 data bits are nearly exclusively used for old teletypes, I've never seen anything using 6 data bits, 7 and 8 data bits are quite common for general communication. 7 data bits are often used with parity, 8 data bits are often used without parity, but as I wrote: data bits and parity bits can be used in any combination.

    When using other serial ports (e.g. on embedded systems), you can often also select mark or space in place of the parity bit (i.e. fixed 0 or fixed 1). Some UARTs also support 9 data bits, just to confuse you further. Also, 1.5 stop bits are possible, but rare.

    See also Serial port.

    Update: Mark and Space in place of the parity bit is supported by PC-standard serial ports, so you can choose from None, Even, Odd, Mark, Space for the parity bit. Some embedded controllers do not support mark and space for the parity bit.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      Alexander:

      Thanks for the response. It prompted me to do a lot of reading.

      Showing my age, parity was always the MSB of an 8-bit byte. There also appears to be an unfortunate reuse of the term "parity". In some implementations of RS-485 systems, the 9th "parity" bit is used to key the end nodes that the current byte is an address(see MAX 3140 datasheet). This allows the end node to quickly determine if the message is for it without having to decode the packet.

      This 9th bit is generated internally by the UART based on the value of the parity bit. So, which parity bit is the Perl modules dealing with? And do the modules allow this definition to be changed at will to drive the address function out onto the RS-485 bus?

      Interesting topic.

      James

      There's never enough time to do it right, but always enough time to do it over...

        This 9th bit is generated internally by the UART based on the value of the parity bit. So, which parity bit is the Perl modules dealing with?

        Perl does not do any parity, neither does any other language. The parity bit is generated and checked by the UART hardware. All that the OS and any language on top sees is a parity error if the check fails.

        And do the modules allow this definition to be changed at will to drive the address function out onto the RS-485 bus?

        That's an application of the mark and space "parity" bits. You could switch the serial format between "mark parity" and "space parity" to set or clear the parity bit from the application.

        Of course it is possible to generate and verify the parity bit in the application, that's why some embedded U(S)ARTs allow 9 bit formats (8 bit plus application-specific use of the parity bit). On a generic PC-style USART, the data bits are limited to 8 bit, so if you want to use an application-specific parity bit, you are limited to 4 to 7 data bits.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11131041]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2022-01-28 18:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (73 votes). Check out past polls.

    Notices?