[英]Parsing integers from a string of floats
我設法通過RS232從溫度傳感器獲取值。 將期望值添加到數據庫以進行進一步操作。 這是代碼。
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));
}
}
}
請注意:
tempvalue = float.Parse(RxString);
dbconnect();
textBox1.AppendText(RxString);
是我想要操縱的價值。 當前輸出類似於56.1234557.4323253.34221 。 感興趣的值是每個'。'之后左邊的兩位數。我想得到這個值有56 57 53等我試過這個似乎不是最好的選擇。 由於它來自串行端口,因此讀取值的方式使我松散了一些位。 有沒有辦法可以在XX.DDDDD中分隔X和D並使用XX來填充數據庫? 我試過RxString.Split('.')
。 也許我做得不對。 有人幫忙。 謝謝
string[] values = RxString.Split(new Char[] {','});
foreach(String value in values)
{
string[] parts = value.Split(new char[] {'.'});
if (parts.Length == 2)
{
int v;
if (int.TryParse(parts[0], out v))
{
// do something with v
}
}
}
如果我找到你
您可以使用Regex來提取字符串的有趣部分
MatchCollection matches =
Regex.Matches("56.1234557.4323253.34221", @"\d\d(?=\.)");
foreach (Match match in matches) {
Console.WriteLine(match.Value);
}
我使用正則表達式模式find(?=suffix)
匹配后綴之前的位置。 \\d\\d
匹配兩位數字和\\.
匹配小數點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.