sim800c 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 310b9aeb43924f653f98434db70f657317edccc821a69f63d989eaa261201b82
4
- data.tar.gz: 414d85f974e7bb3bee2366af72239ea2aa7a8be51cf38825dc236b36cea18a22
3
+ metadata.gz: e4c8cf1359ddeddbca07e6bf8b185cd345721b821b82721e40c2020b507e0341
4
+ data.tar.gz: e0898ae2d7a2b33ab214865619fec17b7ee740865470a4ae30b32a872ab4120c
5
5
  SHA512:
6
- metadata.gz: 738ab0705bb356d100142ce0bf6dea9945872442ae49e66746241aeabc951020bcf0ea5eeb71c6d7ce8704c436cf79f5ed98705a29ed7b5bb9cd0091d4077385
7
- data.tar.gz: '08aa82316c67748b43d976cc17925d4f1d2f62a9a1541f1f2d1b2a6ca321063c287888cb33253428dfae7bb48804a3f4b0e5d17cf659f13be7dc779a57444767'
6
+ metadata.gz: 5bf8a9f2e9c1a9f9d73bdb8725f4b821dc9177f869d4232c908bab8d7b72806d3a0ba8bc39fd5920e64bb7f7d65276e62032404b1c363ca136040deb93b24b5b
7
+ data.tar.gz: 63296ac6063dd34c6af33f6daea2fd1a5142dbfc91da13c7cd22438c161336ae774f57e8c0417f75611d3b9aab1309762487a6aa266327cfbba8f7cbd5b99f69
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,38 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.1
3
+
4
+ Style/StringLiterals:
5
+ EnforcedStyle: single_quotes
6
+
7
+ Style/StringLiteralsInInterpolation:
8
+ EnforcedStyle: double_quotes
9
+
10
+ Layout/LineLength:
11
+ Max: 120
12
+
13
+ Metrics/AbcSize:
14
+ Max: 21
15
+
16
+ Metrics/BlockLength:
17
+ CountComments: false
18
+ CountAsOne: [array, hash, heredoc, method_call]
19
+ Exclude:
20
+ - spec/**/*
21
+
22
+ Metrics/MethodLength:
23
+ Max: 20
24
+ CountComments: false
25
+ CountAsOne: [array, hash, heredoc, method_call]
26
+
27
+ Style/Documentation:
28
+ Enabled: false
29
+
30
+ Layout/LineLength:
31
+ CountComments: false
32
+ Exclude:
33
+ - spec/**/*
34
+
35
+ Metrics/MethodLength:
36
+ Max: 20
37
+ CountComments: false
38
+ CountAsOne: [array, hash, heredoc, method_call]
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-06-07
4
+
5
+ - Initial release
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Nujian Den Mark Meralpis
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,148 @@
1
+ # SIM800C
2
+
3
+ **Sim800c** is a Ruby gem that provides a clean interface to the SIM800C GSM/GPRS module using AT commands over a serial connection. Easily send SMS, manage inbox/outbox, query signal strength, check battery info, and more.
4
+
5
+ ![SIM800C](sim800c.png "SIM800C USB GSM MODULE")
6
+
7
+ ---
8
+
9
+ ## Features
10
+
11
+ - 📤 Send SMS (immediate or stored)
12
+ - 📩 Read, list, and delete messages
13
+ - 📶 Monitor signal quality (RSSI)
14
+ - 🔋 Check battery status and voltage
15
+ - 💾 Get SIM storage usage info
16
+
17
+ ---
18
+
19
+ ## Installation
20
+
21
+ ```ruby
22
+ gem 'sim800c'
23
+ ```
24
+
25
+ then run:
26
+
27
+ ```
28
+ bundle install
29
+ ```
30
+
31
+ Or install it directly:
32
+
33
+ ```
34
+ gem install sim800c
35
+ ```
36
+
37
+ ## Overview
38
+
39
+ ```ruby
40
+ require 'sim800c'
41
+
42
+ port_path = Sim800c.find_port # /dev/ttyUSB0
43
+ client = Sim800c::Client.new(port_path, baud: 9600)
44
+
45
+ # Send SMS
46
+ client.send_message("+639123456789", "Hello from SIM800C!")
47
+
48
+ # Store and then send SMS
49
+ client.save_and_send_message("+639123456789", "Stored first, sent later")
50
+
51
+ # List of all messages
52
+ puts client.list_messages.all
53
+
54
+ # Check signal quality
55
+ puts client.signal_quality.info
56
+
57
+ # Get battery info
58
+ puts client.device.battery_info
59
+
60
+ # Delete message by index
61
+ client.delete_message.by_index(1)
62
+
63
+ # ...and more
64
+ ```
65
+
66
+ ## Usage
67
+
68
+ ### Message Listing
69
+ ```ruby
70
+ messages = client.list_messages
71
+
72
+ messages.all # => All messages
73
+ messages.unread # => Only unread messages
74
+ messages.read # => Read messages
75
+ messages.unsent # => Unsent messages
76
+ messages.sent # => Sent messages
77
+ ```
78
+
79
+ ### Message Deletion
80
+
81
+ ```ruby
82
+ deleter = client.delete_message
83
+
84
+ deleter.by_index(1) # => delete specific message by index
85
+ deleter.all # => delete all messages
86
+ deleter.all_read # => delete all read messages
87
+ deleter.all_read_and_sent # => delete all read and sent messages
88
+ deleter.all_read_sent_and_unsent # => same with .all
89
+
90
+ ```
91
+
92
+ ### Signal Quality
93
+
94
+ ```ruby
95
+ signal = client.signal_quality
96
+
97
+ signal.info # => { rssi: 25, bars: 5, strength: 'Excellent' }
98
+ signal.rssi # => 25
99
+ ```
100
+
101
+ ### Device / Battery Info
102
+
103
+ ```ruby
104
+ device = client.device
105
+
106
+ device.battery_info
107
+ # => {
108
+ # charge_status: 0,
109
+ # battery_level: 100,
110
+ # voltage_mv: 4367,
111
+ # charge_status_label: "Not charging"
112
+ # }
113
+ ```
114
+
115
+ ### Storage Info
116
+
117
+ ```ruby
118
+ client.storage_info
119
+ # => [{
120
+ # storage_type: "read_write",
121
+ # memory: "SM",
122
+ # used: 2,
123
+ # total: 30
124
+ # }]
125
+ ```
126
+
127
+ ### TODOs:
128
+ - Add HTTP/TCP support
129
+ - Enable incoming SMS handling (AT+CNMI)
130
+ - Command retries with timeout/backoff
131
+
132
+ ## Development
133
+
134
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
135
+
136
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
137
+
138
+ ## Contributing
139
+
140
+ Bug reports and pull requests are welcome on GitHub at https://github.com/denmarkmeralpis/sim800c. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/denmarkmeralpis/sim800c/blob/main/CODE_OF_CONDUCT.md).
141
+
142
+ ## License
143
+
144
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
145
+
146
+ ## Code of Conduct
147
+
148
+ Everyone interacting in the Sim800c project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/denmarkmeralpis/sim800c/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'helpers'
4
+ require_relative 'commands/send_message'
5
+ require_relative 'commands/list_messages'
6
+ require_relative 'commands/signal_quality'
7
+ require_relative 'commands/delete_message'
8
+ require_relative 'commands/storage_info'
9
+ require_relative 'commands/device'
10
+
11
+ module Sim800c
12
+ class Client
13
+ include Helpers
14
+
15
+ def initialize(port_path, baud: 9600)
16
+ require 'rubyserial'
17
+ @serial = Serial.new(port_path, baud)
18
+ end
19
+
20
+ # Sends message immediately (does not store sent messages)
21
+ def send_message(phone_number, message)
22
+ Commands::SendMessage.new(@serial).send_immediate(phone_number, message)
23
+ end
24
+
25
+ # Store and send (AT+CMGW = store, AT+CMSS = send)
26
+ def save_and_send_message(phone_number, message)
27
+ Commands::SendMessage.new(@serial).save_and_send(phone_number, message)
28
+ end
29
+
30
+ # Methods: .all, .unread, .read, .unsent, .sent (Hash)
31
+ def list_messages
32
+ Commands::ListMessages.new(@serial)
33
+ end
34
+
35
+ # Methods: .info (Hash), .rssi (Integer)
36
+ def signal_quality
37
+ Commands::SignalQuality.new(@serial)
38
+ end
39
+
40
+ # Methods: .by_index, .all_read, .all_read_and_sent, .all_read_sent_and_unsent
41
+ def delete_message
42
+ Commands::DeleteMessage.new(@serial)
43
+ end
44
+
45
+ # Checks storage info
46
+ def storage_info
47
+ Commands::StorageInfo.new(@serial).call
48
+ end
49
+
50
+ # Methods: .battery_info
51
+ def device
52
+ Commands::Device.new(@serial)
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sim800c
4
+ module Commands
5
+ class DeleteMessage
6
+ include Helpers
7
+
8
+ def initialize(serial)
9
+ @serial = serial
10
+ end
11
+
12
+ def by_index(index)
13
+ perform_delete!("AT+CMGD=#{index}")
14
+ end
15
+
16
+ def all
17
+ perform_delete!('AT+CMGD=1,4')
18
+ end
19
+
20
+ def all_read
21
+ perform_delete!('AT+CMGD=1,1')
22
+ end
23
+
24
+ def all_read_and_sent
25
+ perform_delete!('AT+CMGD=1,2')
26
+ end
27
+
28
+ def all_read_sent_and_unsent
29
+ all
30
+ end
31
+
32
+ private
33
+
34
+ def perform_delete!(cmd)
35
+ at_cmd(cmd)
36
+ response = read_cmg_response
37
+ return_or_raise_error!(response)
38
+ end
39
+
40
+ def read_cmg_response
41
+ response = String.new
42
+
43
+ loop do
44
+ chunk = @serial.read(1024)
45
+ response << chunk
46
+ break if chunk.include?('OK') || chunk.include?('ERROR')
47
+
48
+ sleep(0.3)
49
+ end
50
+
51
+ response
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sim800c
4
+ module Commands
5
+ class Device
6
+ include Helpers
7
+
8
+ def initialize(serial)
9
+ @serial = serial
10
+ end
11
+
12
+ def battery_info
13
+ at_cmd('AT+CBC')
14
+ parse_cbc_response(@serial.read(1024))
15
+ end
16
+
17
+ private
18
+
19
+ def parse_cbc_response(response)
20
+ match = response.match(/\+CBC:\s*(\d+),(\d+),(\d+)/)
21
+
22
+ if match
23
+ bcs, bcl, voltage = match.captures.map(&:to_i)
24
+
25
+ {
26
+ charge_status: bcs, # 0 = not charging, 1 = charging
27
+ battery_level: bcl,
28
+ voltage_mv: voltage
29
+ }
30
+
31
+ else
32
+ 'No valid +CBC line found.'
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sim800c
4
+ module Commands
5
+ class ListMessages
6
+ include Helpers
7
+
8
+ def initialize(serial)
9
+ @serial = serial
10
+ end
11
+
12
+ def all
13
+ perform_message_query('AT+CMGL="ALL"')
14
+ end
15
+
16
+ def unread
17
+ perform_message_query('AT+CMGL="REC UNREAD"')
18
+ end
19
+
20
+ def read
21
+ perform_message_query('AT+CMGL="REC READ"')
22
+ end
23
+
24
+ def unsent
25
+ perform_message_query('AT+CMGL="STO UNSENT"')
26
+ end
27
+
28
+ def sent
29
+ perform_message_query('AT+CMGL="STO SENT"')
30
+ end
31
+
32
+ private
33
+
34
+ def perform_message_query(command)
35
+ at_cmd('AT')
36
+ @serial.read(100)
37
+
38
+ at_cmd('AT+CMGF=1')
39
+ @serial.read(100)
40
+
41
+ at_cmd(command)
42
+ response = read_response(wait_time: 1)
43
+ parse_cmgl_response(response)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sim800c
4
+ module Commands
5
+ class SendMessage
6
+ include Helpers
7
+
8
+ def initialize(serial)
9
+ @serial = serial
10
+ end
11
+
12
+ # Sends the message immediately using AT+CMGS
13
+ def send_immediate(phone_number, message)
14
+ at_cmd('AT+CMGF=1')
15
+ @serial.read(100)
16
+
17
+ at_cmd("AT+CMGS=\"#{phone_number}\"")
18
+ @serial.read(100)
19
+
20
+ at_cmd("#{message}#{26.chr}")
21
+ response = read_response(wait_time: 1)
22
+ response.lines.last.chomp.eql?('OK')
23
+ end
24
+
25
+ # Stores and sends the message using AT+CMGW and AT+CMSS
26
+ def save_and_send(phone_number, message)
27
+ at_cmd('AT+CMGF=1')
28
+ @serial.read(100)
29
+
30
+ at_cmd("AT+CMGW=\"#{phone_number}\"")
31
+ @serial.read(100)
32
+
33
+ at_cmd("#{message}#{26.chr}")
34
+ response = read_response(wait_time: 1)
35
+ index = response[/\+CMGW:\s*(\d+)/, 1]
36
+
37
+ at_cmd("AT+CMSS=#{index}") if index
38
+ response = read_response(wait_time: 1)
39
+ response.lines.last.chomp.eql?('OK')
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sim800c
4
+ module Commands
5
+ class SignalQuality
6
+ include Helpers
7
+
8
+ def initialize(serial)
9
+ @serial = serial
10
+ end
11
+
12
+ def info
13
+ rssi_level = rssi
14
+ return { rssi: nil, bars: 0, strength: 'Unknown' } unless rssi_level
15
+
16
+ {
17
+ rssi: rssi_level,
18
+ bars: bars(rssi_level),
19
+ strength: strength(rssi_level)
20
+ }
21
+ end
22
+
23
+ def rssi
24
+ at_cmd('AT+CSQ')
25
+ response = @serial.read(100)
26
+ return unless response =~ /\+CSQ:\s*(\d+),/
27
+
28
+ ::Regexp.last_match(1).to_i
29
+ end
30
+
31
+ private
32
+
33
+ def strength(val)
34
+ case val
35
+ when 0..9 then 'Weak'
36
+ when 10..14 then 'Moderate'
37
+ when 15..19 then 'Good'
38
+ when 20..24 then 'Strong'
39
+ when 25..31 then 'Excellent'
40
+ else 'Unknown'
41
+ end
42
+ end
43
+
44
+ def bars(val)
45
+ case val
46
+ when 0..9 then 1
47
+ when 10..14 then 2
48
+ when 15..19 then 3
49
+ when 20..24 then 4
50
+ when 25..31 then 5
51
+ else 0
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sim800c
4
+ module Commands
5
+ class StorageInfo
6
+ include Helpers
7
+
8
+ def initialize(serial)
9
+ @serial = serial
10
+ end
11
+
12
+ def call
13
+ at_cmd('AT+CPMS?')
14
+ response = @serial.read(1024)
15
+ parse_cpms_response(response)
16
+ end
17
+
18
+ private
19
+
20
+ def parse_cpms_response(response)
21
+ matches = response.scan(/"(\w+)",(\d+),(\d+)/)
22
+ matches.each_with_index.map do |(mem, used, total), i|
23
+ {
24
+ storage_type: %w[read_write write_only receive_only][i],
25
+ memory: mem,
26
+ used: used.to_i,
27
+ total: total.to_i
28
+ }
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sim800c
4
+ module Helpers
5
+ def at_cmd(cmd, wait_time: 0.2)
6
+ @serial.write("#{cmd}\r")
7
+ sleep(wait_time)
8
+ end
9
+
10
+ def read_response(wait_time: 0.2, bytes: 1024)
11
+ response = String.new
12
+
13
+ loop do
14
+ chunk = @serial.read(bytes)
15
+ response << chunk
16
+ break if chunk.include?("\r\nOK\r\n") || chunk.include?("\r\nERROR\r\n")
17
+
18
+ sleep(wait_time)
19
+ end
20
+
21
+ response
22
+ end
23
+
24
+ def parse_cmgl_response(response) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
25
+ messages = []
26
+ lines = response.split("\r\n").reject(&:empty?)
27
+
28
+ i = 0
29
+ while i < lines.length
30
+ if lines[i].start_with?('+CMGL:')
31
+ header = lines[i]
32
+ message_lines = []
33
+
34
+ # Collect message body lines until next +CMGL or end
35
+ i += 1
36
+ while i < lines.length && !lines[i].start_with?('+CMGL:')
37
+ message_lines << lines[i]
38
+ i += 1
39
+ end
40
+
41
+ # Parse the header
42
+ if header =~ /^\+CMGL:\s*(\d+),"(.*?)","(.*?)",".*?","(.*?)"$/
43
+ index = ::Regexp.last_match(1).to_i
44
+ status = ::Regexp.last_match(2)
45
+ sender = ::Regexp.last_match(3)
46
+ datetime = ::Regexp.last_match(4)
47
+
48
+ date, time = datetime.split(',', 2)
49
+ message = message_lines.join("\n").strip
50
+
51
+ messages << {
52
+ index: index,
53
+ status: status,
54
+ sender: sender,
55
+ date: date,
56
+ time: time,
57
+ message: message,
58
+ unread: (status == 'REC UNREAD')
59
+ }
60
+ end
61
+ else
62
+ i += 1
63
+ end
64
+ end
65
+
66
+ messages
67
+ end
68
+
69
+ def return_or_raise_error!(response)
70
+ return true if response.include?("\r\nOK\r\n")
71
+
72
+ if response.include?("\r\nERROR: 302")
73
+ raise OperationNotAllowed, 'Operation not allowed'
74
+ elsif response.include?("\r\nERROR: 512")
75
+ raise SimNotFound, 'SIM failure or not inserted'
76
+ elsif response.include?("\r\nERROR: 321")
77
+ raise InvalidIndex, 'Index not found'
78
+ else
79
+ raise Error, response
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sim800c
4
+ class PortScanner
5
+ def self.find_port
6
+ each_candidate do |port|
7
+ serial = Serial.new(port, 9_600)
8
+ serial.write("AT\r")
9
+ sleep 0.3
10
+ response = serial.read(64).to_s
11
+ next unless response.include?('OK')
12
+
13
+ serial.write("ATI\r")
14
+ sleep 0.3
15
+ info = serial.read(128).to_s
16
+ serial.close
17
+
18
+ return port if info =~ /SIM[-_ ]?800C/i
19
+ rescue StandardError
20
+ false
21
+ ensure
22
+ serial&.close
23
+ end
24
+ end
25
+
26
+ def self.each_candidate(&block)
27
+ ports =
28
+ case RUBY_PLATFORM
29
+ when /linux/
30
+ Dir.glob('/dev/ttyUSB*') + Dir.glob('/dev/ttyACM*')
31
+ when /darwin/
32
+ Dir.glob('/dev/tty.usbmodem*') + Dir.glob('/dev/tty.usbserial*')
33
+ when /mswin|mingw/
34
+ (1..20).map { |i| "COM#{i}" }
35
+ else
36
+ []
37
+ end
38
+
39
+ ports.each(&block).first
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sim800c
4
+ VERSION = '0.1.1'
5
+ end
data/lib/sim800c.rb ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'sim800c/version'
4
+ require_relative 'sim800c/port_scanner'
5
+ require_relative 'sim800c/client'
6
+ require_relative 'sim800c/helpers'
7
+
8
+ module Sim800c
9
+ class Error < StandardError; end
10
+ class InvalidIndex < Error; end
11
+ class OperationNotAllowed < Error; end
12
+ class SimNotFound < Error; end
13
+ end
data/sig/sim800c.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Sim800c
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
data/sim800c.png ADDED
Binary file
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sim800c
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nujian Den Mark Meralpis
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2025-06-12 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: rubyserial
@@ -34,7 +33,27 @@ email:
34
33
  executables: []
35
34
  extensions: []
36
35
  extra_rdoc_files: []
37
- files: []
36
+ files:
37
+ - ".rspec"
38
+ - ".rubocop.yml"
39
+ - CHANGELOG.md
40
+ - CODE_OF_CONDUCT.md
41
+ - LICENSE.txt
42
+ - README.md
43
+ - Rakefile
44
+ - lib/sim800c.rb
45
+ - lib/sim800c/client.rb
46
+ - lib/sim800c/commands/delete_message.rb
47
+ - lib/sim800c/commands/device.rb
48
+ - lib/sim800c/commands/list_messages.rb
49
+ - lib/sim800c/commands/send_message.rb
50
+ - lib/sim800c/commands/signal_quality.rb
51
+ - lib/sim800c/commands/storage_info.rb
52
+ - lib/sim800c/helpers.rb
53
+ - lib/sim800c/port_scanner.rb
54
+ - lib/sim800c/version.rb
55
+ - sig/sim800c.rbs
56
+ - sim800c.png
38
57
  homepage: https://github.com/denmarkmeralpis/sim800c.git
39
58
  licenses:
40
59
  - MIT
@@ -43,7 +62,6 @@ metadata:
43
62
  homepage_uri: https://github.com/denmarkmeralpis/sim800c.git
44
63
  source_code_uri: https://github.com/denmarkmeralpis/sim800c.git
45
64
  changelog_uri: https://github.com/denmarkmeralpis/sim800c.git
46
- post_install_message:
47
65
  rdoc_options: []
48
66
  require_paths:
49
67
  - lib
@@ -58,8 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
58
76
  - !ruby/object:Gem::Version
59
77
  version: '0'
60
78
  requirements: []
61
- rubygems_version: 3.3.27
62
- signing_key:
79
+ rubygems_version: 3.7.1
63
80
  specification_version: 4
64
81
  summary: Easy serial communication with the SIM800C module for sending and receiving
65
82
  SMS messages using Ruby.