Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8502059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:22:52+00:00 2026-06-11T01:22:52+00:00

I’m new to Ruby. I’m trying to make an app that reads from a

  • 0

I’m new to Ruby.
I’m trying to make an app that reads from a serial-port and puts values into a sqlite3 database. When a client connects via TCP socket he should recieve values from the db. Values written by the client should be sent via serial-port.

I have two questions regarding my app.

This would open one connection to the db on the main thread(?) and one for each client..
Is there a better way to use sqlite3?
I think i figured this out. sqlite3 is not thread safe by defaul,t so this seems like the way to do it..

How do i write to the serialport in the recieve_data method? Is it okay to make serial a global variable?

#!/usr/bin/env ruby
#
# server_1

require 'rubygems'
require 'eventmachine'
require 'sqlite3'
require 'em-serialport'
require 'json'

module SocketClient
    def self.list
        @list ||= []
    end

    def post_init
        SocketClient.list << self
        @db = SQLite3::Database.new( "data.db" )

        values = []
        @db.execute("SELECT * FROM values") do |row|
            values << {row[0] => row[1]} #id => value
        end
        self.send_data "#{values.to_json}\n"

        p "Client connected"

    end

    def unbind
        SocketClient.list.delete self
        @db.close
    end

    def receive_data data
        p data
        #How do i send via serialport from here??? serial.send_data data
    end
end

db = SQLite3::Database.new( "data.db" )

EM.run{
    EM.start_server '0.0.0.0', 8081, SocketClient
    serial = EM.open_serial '/dev/tty.usbserial-xxxxxxxx', 9600, 8, 1, 0

    serial.on_data do |data|
        #Parse data into an array called values
        db.execute("UPDATE values SET value = ? WHERE id = ?", values["value"], values["id"])
        SocketClient.list.each{ |c| c.send_data "#{values.to_json}\n" }
    end
}

db.close
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T01:22:54+00:00Added an answer on June 11, 2026 at 1:22 am

    Setup the constructor for your Socket client so that it will receive the shared serial connection.

    module SocketClient
        def initialize serial
          @serial = serial
        end
    
        def receive_data data
            p data
            @serial.send_data data
        end
    

    Then pass it when you call EM.start_server

    EM.run{
        serial = EM.open_serial '/dev/tty.usbserial-xxxxxxxx', 9600, 8, 1, 0
        EM.start_server '0.0.0.0', 8081, SocketClient, serial
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I'm trying to create an if statement in PHP that prevents a single post
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.