6

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 am using this gem, and my device's specifications are as follows.

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

When I receive data like below, the unpacked data is still with 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 receives a, the unpacked data is 11100001 instead of 01100001, because it's EVEN parity.


To convert the byte back the what it should be, I do like this

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

now, to me, this is a way low-level. I was expecting the serialport gem was to do this parity check, but as far as I read its document, it doesn't say anything about parity check.

Am I missing a method that is already implemented in the gem, or is my work around above is nessesary since it's my responsibity to check the parity and find error?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
kukrt
  • 2,117
  • 3
  • 21
  • 32
  • I know this doesn't help much but normally the parity check is handled by the serial port driver itself. Ruby should be going through the serial driver. – Shane Wealti Feb 28 '12 at 20:25
  • so I guess the driver is passing data as is (this means that I am responsible to check parity). Thank you for your comment. – kukrt Feb 29 '12 at 13:20
  • I think the 0x80 bit is not the parity bit but the serial port driver setting the 8th bit to 1 out of convention. Is it 1 all the time? – Michael Slade Dec 09 '12 at 23:04

1 Answers1

3

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

Raise an argError on bad argument.

SerialPort::new and SerialPort::open without a block return an instance of SerialPort. SerialPort::open with a block passes a SerialPort to the block and closes it when the block exits (like 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. Hash keys are "baud", "data_bits", "stop_bits", and "parity" (see above).

Parameters not present in the hash or set to nil remain unchanged. 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 set the 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 RTS/CTS handshaking; DSR/DTR is not supported.

  • read_timeout() -> anInteger
  • read_timeout=(anInteger) -> anInteger
  • write_timeout() -> anInteger
  • write_timeout=(anInteger) -> anInteger

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

Note: Read timeouts don't mix well with multi-threading.

Note: Under Posix, write timeouts are not implemented.

  • break(time) -> nil

Send a break for the given time.

time -> anInteger: tenths-of-a-second for the break. Note: Under Posix, this value is very approximate.

  • signals() -> aHash

Return a hash with the state of each line status bit. Keys are "rts", "dtr", "cts", "dsr", "dcd", and "ri".

Note: Under Windows, the rts and dtr values 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: Under Windows, rts() and dtr() are not implemented