Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by afoken (Chancellor)
on Apr 09, 2021 at 14:14 UTC ( [id://11131055]=note: print w/replies, xml ) Need Help??


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

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". ;-)
  • Comment on Re^2: How to read serial port with WIN32:SerialPort?

Replies are listed 'Best First'.
Re^3: How to read serial port with WIN32:SerialPort?
by jmlynesjr (Deacon) on Apr 12, 2021 at 01:38 UTC

    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". ;-)

        The MAX3140 datasheet description of 9bit mode:

        The MAX3140 supports a common multidrop communication technique refe +rred to as 9-bit mode. In this mode, the parity bit is set + to indicate a message that contains a header with a desti +nation address. Set the MAX3140’s parity mask to generate int +errupts for this condition. Operating a network in this mode + reduces the processing overhead of all nodes by enabling the + slave controllers to ignore most message traffic. This reliev +es the remote processor to handle more useful tasks.In 9-bit + mode, the MAX3140 is set up with eight bits plus parity. T +he parity bit in all normal messages is clear, but is set +in an address-type message. The MAX3140’s parity-interrupt mask g +enerates an interrupton high parity when enabled. When the mas +ter sends an address message with the parity bit set, all MAX3140 n +odes issue an interrupt. All nodes then retrieve the received b +yte to compare to their assigned address. Once addressed, the nod +e continues to process each received byte. If the node was not addr +essed, it ignores all message traffic until a new address is +sent out by the master.The parity/9th-bit interrupt is controlled onl +y by the data in the receive register and is not affected by data in +the FIFO, so the most effective use of the parity/9th-bitinte +rrupt is with FIFO disabled. With the FIFO disabled, received non-ad +dress words are ignored and not even read from the UART.

        The parity bit that this is referring to is a bit in an on-chip transmit control register. This register has to be modified to cause the chip generated parity bit to be set to indicate an address byte. And then be modified again to turn off address-mode.

        My guess is the Device::SerialPort module doesn't use it's parity argument for this purpose.

        James

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-19 14:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found