Ruby serialport gem that is responsible for parity error checking?

gems

serialport (1.0.4)
Authors: Guillaume Pierronnet, Alan Stern, Daniel E. Shipton, Tobin
Richard, Hector Parra, Ryan C. Payne
Homepage: http://github.com/hparra/ruby-serialport/
Library for using RS-232 serial ports.

I use this gem and my device specifications are as follows.

  • 9600
  • 7bits
  • 1 stop bit
  • EVEN parity

When I receive the data as shown below, the decompressed data still has a parity bit.

sp = SerialPort.new("/dev/serial-device", 9600, 7, 1, SerialPort::EVEN) data = sp.gets data.chars.each do |char| puts char.unpack("B*") end 

ex. if sp gets a , the unpacked data is 11100001 instead of 01100001 , because it has the value EVEN.


To convert a byte back to what it should be, I like it

 data = sp.gets #gets 11100001 for 'a' (even parity) data.bytes.to_a.each do |byte| puts (byte & 127).chr end 

now, for me, this is a low level way. I expected that the serial descriptor should have performed this parity check, but as far as I read his document, it says nothing about parity.

Am I missing a method that has already been implemented in stone, or is my work on it important because it is my responsibility to check the parity and find the error?

+6
source share
1 answer

SerialPort :: ODD, SerialPort :: MARK, SerialPort :: SPACE (MARK and SPACE are not supported in Posix)

Raise the argError argument about a bad argument.

SerialPort :: new and SerialPort :: open without returning a block instance of SerialPort. SerialPort :: open with skipping blocks SerialPort to the block and closes it when you exit the block (for example, File :: open).

** Instance Methods **

  • modem_params () β†’ aHash
  • modem_params = (aHash) -> aHash
  • get_modem_params () β†’ aHash
  • set_modem_params (aHash) -> aHash
  • set_modem_params (baudrate [, databits [, stopbits [, parity]]])

Get and set the modem parameters. The hash keys are "baud", "bit_data", "stop_bits" and "parity" (see above).

Parameters absent from the hash or not set to nil remain unchanged. The default parameter values ​​for the set_modem_params method are: databits = 8, stopbits = 1, parity = (databits == 8? SerialPort :: NONE: SerialPort :: EVEN).

  • baud () β†’ anInteger
  • baud = (anInteger) β†’ anInteger
  • data_bits () β†’ 4, 5, 6, 7 or 8
  • data_bits = (anInteger) β†’ anInteger
  • stop_bits () β†’ 1 or 2
  • stop_bits = (anInteger) β†’ anInteger
  • parity () β†’ anInteger: SerialPort :: NONE, SerialPort :: EVEN, SerialPort :: ODD, SerialPort :: MARK or SerialPort :: SPACE
  • parity = (anInteger) β†’ anInteger

Get and set the corresponding modem parameter.

  • flow_control () β†’ anInteger
  • flow_control = (anInteger) β†’ anInteger

Get and install flow control: SerialPort :: NONE, SerialPort :: HARD, SerialPort :: SOFT or (SerialPort :: HARD | SerialPort :: SOFT).

Note. SerialPort :: HARD mode is not supported on all platforms. SerialPort :: HARD uses the RTS / CTS connection; DSR / DTR is not supported.

  • read_timeout () β†’ anInteger
  • read_timeout = (anInteger) β†’ anInteger
  • write_timeout () β†’ anInteger
  • write_timeout = (anInteger) β†’ anInteger

Get and set the timeout values ​​(in milliseconds) for reading and writing. A negative timeout will return all available data without waiting, a zero read timeout will not return, at least one byte is available, and a positive read timeout is returned when the requested number of bytes is available or the interval between the arrival of two bytes exceeds timeout value.

Note. Reading timeouts do not mix well with multi-threaded ones.

Note. In Posix, write timeouts are not implemented.

  • break (time) β†’ nil

Send a break for a given time.

time β†’ anInteger: tenths of a second to break. Note. In Posix, this value is very approximate.

  • signals () β†’ aHash

Returns a hash with the state of each bit of the status string. The keys are "rts", "dtr", "cts", "dsr", "dcd" and "ri".

Note. On Windows, rts and dtr are not included.

  • RTS ()
  • DTR ()
  • CTS ()
  • DSR ()
  • DCD ()
  • ri () β†’ 0 or 1

  • rts = (0 or 1)

  • dtr = (0 or 1) β†’ 0 or 1

Get and set the corresponding line status bit.

Note. On Windows, rts () and dtr () are not implemented

+3
source

Source: https://habr.com/ru/post/909377/


All Articles