can_messenger 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +42 -5
- data/lib/can_messenger/messenger.rb +33 -2
- data/lib/can_messenger/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b21a6826af91cc61f6ea94c5cae6d2ba38cc32749d8e4acce38c7c897ff78501
|
4
|
+
data.tar.gz: 585849cf70fbc1f24e6d2e32aa6d0334a6d8addc90ef5b9c0433740621447208
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a356542e08412d91b8607f679b8414dcc10d23119cf1b529631eeb5f0c058ec4525b8df2e8bda120acfc74fb4020f6feef8cce9c54d9d6fe305da1e9229cc6b8
|
7
|
+
data.tar.gz: 8d9572376e9d5d3f9c2c7edacb0b3f2e4287ae11cc48122b06d6337b3e9858621558def72efa74fd9b06382b6e1475b3716696eb96ec20ecaf154d43719b39c6
|
data/README.md
CHANGED
@@ -1,5 +1,12 @@
|
|
1
1
|
# CanMessenger
|
2
2
|
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/can_messenger.svg?icon=si%3Arubygems&icon_color=%23e77682)](https://badge.fury.io/rb/can_messenger)
|
4
|
+
[![Build Status](https://github.com/fk1018/can_messenger/actions/workflows/ruby.yml/badge.svg)](https://github.com/fk1018/can_messenger/actions)
|
5
|
+
[![Test Coverage](https://codecov.io/gh/fk1018/can_messenger/branch/main/graph/badge.svg)](https://codecov.io/gh/fk1018/can_messenger)
|
6
|
+
![Status](https://img.shields.io/badge/status-stable-green)
|
7
|
+
[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](https://opensource.org/licenses/MIT)
|
8
|
+
![Gem Total Downloads](https://img.shields.io/gem/dt/can_messenger)
|
9
|
+
|
3
10
|
`can_messenger` is a Ruby gem that provides an interface for communicating over the CAN bus, allowing users to send and receive CAN messages. This gem is designed for developers who need an easy way to interact with CAN-enabled devices.
|
4
11
|
|
5
12
|
## Installation
|
@@ -44,9 +51,9 @@ messenger = CanMessenger::Messenger.new('can0')
|
|
44
51
|
|
45
52
|
To send a message:
|
46
53
|
|
47
|
-
```ruby
|
48
|
-
messenger.send_can_message(0x123, [0xDE, 0xAD, 0xBE, 0xEF])
|
49
|
-
```
|
54
|
+
```ruby
|
55
|
+
messenger.send_can_message(0x123, [0xDE, 0xAD, 0xBE, 0xEF])
|
56
|
+
```
|
50
57
|
|
51
58
|
### Receiving CAN Messages
|
52
59
|
|
@@ -54,8 +61,38 @@ To listen for incoming messages, set up a listener:
|
|
54
61
|
|
55
62
|
```ruby
|
56
63
|
messenger.start_listening do |message|
|
57
|
-
puts "Received: ID=#{message[:id]}, Data=#{message[:data]}"
|
64
|
+
puts "Received: ID=#{message[:id]}, Data=#{message[:data]}"
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
68
|
+
#### Listening with Filters
|
69
|
+
|
70
|
+
The `start_listening` method supports filtering incoming messages based on CAN ID:
|
71
|
+
|
72
|
+
Single CAN ID:
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
messenger.start_listening(filter: 0x123) do |message|
|
76
|
+
puts "Received filtered message: #{message}"
|
77
|
+
end
|
78
|
+
```
|
79
|
+
|
80
|
+
Range of CAN IDs:
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
messenger.start_listening(filter: 0x100..0x200) do |message|
|
84
|
+
puts "Received filtered message: #{message}"
|
58
85
|
end
|
86
|
+
|
87
|
+
```
|
88
|
+
|
89
|
+
Array of CAN IDs:
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
messenger.start_listening(filter: [0x123, 0x456, 0x789]) do |message|
|
93
|
+
puts "Received filtered message: #{message}"
|
94
|
+
end
|
95
|
+
|
59
96
|
```
|
60
97
|
|
61
98
|
### Stopping the Listener
|
@@ -92,4 +129,4 @@ The gem is available as open-source under the terms of the MIT License.
|
|
92
129
|
|
93
130
|
## Author
|
94
131
|
|
95
|
-
Developed by
|
132
|
+
Developed by fk1018.
|
@@ -43,16 +43,23 @@ module CanMessenger
|
|
43
43
|
|
44
44
|
# Continuously listens for CAN messages on the specified interface.
|
45
45
|
#
|
46
|
-
#
|
46
|
+
# This method listens for incoming CAN messages and applies an optional filter.
|
47
|
+
# The filter can be a specific CAN ID or a range of IDs. Only messages that match
|
48
|
+
# the filter are yielded to the provided block.
|
49
|
+
#
|
50
|
+
# @param [Integer, Range, nil] filter Optional filter for CAN IDs. Pass a single ID (e.g., 0x123)
|
51
|
+
# or a range (e.g., 0x100..0x200). If no filter is provided, all messages are processed.
|
47
52
|
# @yield [Hash] Each received message in the format { id: Integer, data: Array<Integer> }.
|
48
53
|
# @return [void]
|
49
|
-
def start_listening
|
54
|
+
def start_listening(filter: nil)
|
50
55
|
@logger.info("Started listening on #{@can_interface}")
|
51
56
|
socket = create_socket
|
52
57
|
while @listening
|
53
58
|
message = receive_message(socket)
|
54
59
|
break unless message # Exit loop if no message
|
55
60
|
|
61
|
+
next if filter && !matches_filter?(message[:id], filter) # Apply filter if specified
|
62
|
+
|
56
63
|
yield(message)
|
57
64
|
end
|
58
65
|
ensure
|
@@ -115,5 +122,29 @@ module CanMessenger
|
|
115
122
|
@logger.error("Error parsing CAN frame: #{e}")
|
116
123
|
nil
|
117
124
|
end
|
125
|
+
|
126
|
+
# Checks if a message ID matches the given filter.
|
127
|
+
#
|
128
|
+
# Supported filters:
|
129
|
+
# - A single ID (e.g., 0x123)
|
130
|
+
# - A range of IDs (e.g., 0x100..0x200)
|
131
|
+
# - An array of IDs (e.g., [0x123, 0x200, 0x300])
|
132
|
+
#
|
133
|
+
# @param [Integer] message_id The ID of the incoming CAN message.
|
134
|
+
# @param [Integer, Range, Array<Integer>, nil] filter The filter to apply.
|
135
|
+
# If nil, the method always returns true.
|
136
|
+
# @return [Boolean] True if the message ID matches the filter, false otherwise.
|
137
|
+
def matches_filter?(message_id, filter)
|
138
|
+
case filter
|
139
|
+
when Integer
|
140
|
+
message_id == filter
|
141
|
+
when Range
|
142
|
+
filter.cover?(message_id)
|
143
|
+
when Array
|
144
|
+
filter.include?(message_id)
|
145
|
+
else
|
146
|
+
true # No filter or unrecognized filter
|
147
|
+
end
|
148
|
+
end
|
118
149
|
end
|
119
150
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: can_messenger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- fk1018
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-12-06 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: CanMessenger provides an interface to send and receive messages over
|
14
14
|
the CAN bus, useful for applications requiring CAN communication in Ruby.
|