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 7859247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:50:34+00:00 2026-06-02T21:50:34+00:00

I have managed to fetch values via RS232 from a temperature sensor. The expected

  • 0

I have managed to fetch values via RS232 from a temperature sensor. The expected values are to be added to the database for further manipulation. Here is the code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.IO.Ports;

namespace SimpleSerial
{

    public partial class Form1 : Form
    {
        // Add this variable 
        MySqlConnection conn = null;
        float tempvalue;
        string RxString;

        public Form1()
        {
            InitializeComponent();
        }
        public void dbconnect()
        {
            string cs = @"server=xxx.x.x.xx;userid=uuuuuu;password=xxxxx;database=project";
            try
            {
                conn = new MySqlConnection(cs);
                conn.Open();
                MySqlCommand cmd = new MySqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = "INSERT INTO temp(value) VALUES(@value) ";
                cmd.Prepare();
                cmd.Parameters.AddWithValue("@value", tempvalue);
                cmd.ExecuteNonQuery();
                //Console.WriteLine("MySQL version : {0}", conn.ServerVersion);

                //MessageBox.Show("DB Connected");
            }
            catch (MySqlException ex)
            {
                Console.WriteLine("Error: {0}", ex.ToString());
                MessageBox.Show("Error: {0}", ex.ToString());
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }

        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            serialPort1.PortName = "COM2";
            serialPort1.BaudRate = 9600;
            serialPort1.Open();
            if (serialPort1.IsOpen)
            {
                buttonStart.Enabled = false;
                buttonStop.Enabled = true;
                textBox1.ReadOnly = false;
            }

        }

        private void buttonStop_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                serialPort1.Close();
                buttonStart.Enabled = true;
                buttonStop.Enabled = false;
                textBox1.ReadOnly = true;
            }

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (serialPort1.IsOpen) serialPort1.Close();
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            // If the port is closed, don't try to send a character.
            if (!serialPort1.IsOpen) return;

            // If the port is Open, declare a char[] array with one element.
            char[] buff = new char[1];

            // Load element 0 with the key character.
            buff[0] = e.KeyChar;

            // Send the one character buffer.
            serialPort1.Write(buff, 0, 1);

            // Set the KeyPress event as handled so the character won't
            // display locally. If you want it to display, omit the next line.
            e.Handled = true;
        }

        private void DisplayText(object sender, EventArgs e)
        {
            tempvalue = float.Parse(RxString);    
            dbconnect();
            textBox1.AppendText(RxString);
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            RxString = serialPort1.ReadExisting();
            this.Invoke(new EventHandler(DisplayText));

        }
    }
}

Kindly note:

    tempvalue = float.Parse(RxString);    
    dbconnect();
    textBox1.AppendText(RxString);

is the value I would like to manipulate. Current output is like 56.1234557.4323253.34221 . The values of interest are two digits on the left after the every ‘.’.I would like to get this values has 56 57 53 etc. I have tried round it seems not to be the best option here. Since it is from a serial port, the way the values are read make me loose some bits.
Is there a way I can separate the Xs and Ds in XX.DDDDD and use the XX to populate the database? I have tried RxString.Split('.'). Maybe I did not do it right. Someone kindly help. Thanks

  • 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-02T21:50:47+00:00Added an answer on June 2, 2026 at 9:50 pm

    You can use Regex to extract the interesting parts of the string

    MatchCollection matches =
        Regex.Matches("56.1234557.4323253.34221", @"\d\d(?=\.)");
    foreach (Match match in matches) {
        Console.WriteLine(match.Value);
    }
    

    I used the regex pattern find(?=suffix) which matches a position preceding a suffix. \d\d matches two digits and \. matches the decimal point.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table view that displays managed objects (Tasks). I'd like to fetch
I have managed to set up Cassandra + Thrift and the Python wrapper for
I have managed to get a cron job to run a rake task by
I have managed to create a custom action in C# using MakeSfxCA which is
I have managed to bring the following output with this query SELECT DISTINCT IF(purchaseproduct_customerid=0,contactperson,vendorname)
I have managed to put this condition and rule below together to redirect a
I have managed to run hadoop examples grep, wordcount... however I am finding some
I have managed objects that have image properties. Since storing large blobs in CoreData
I have managed to reload the list item in a list when the activity
I have managed to crunch down a several line code to this For Each

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.