シリアル経由で接続されたArduinoからのデータ出力を読み取り、シリアルモニターで理解しやすいようにフォーマットされたテキスト文字列から関連データを取り出し、2つの部分を表示する簡単なプログラムをC#で作成しようとしていますカウンターとして機能する 2 つの読み取り専用テキスト ボックスに情報を入力し (情報が更新されるたびに上書きします)、これらの数値を使用して SQL テーブルを更新します。
例:
Arduino の出力:
'Day Users: 234231'
'All Users: 433241'
C# は textbox1 に完全なメッセージを表示します
C# は textbox2 に 234231 を表示します
C# は textbox3 に 433241 を表示します
C# UPDATES SQL テーブルのようなもの:
SqlCommand myCommand = new SqlCommand("UPDATE USER_NUMBERS
SET NUMBERZ= 234231
WHERE UPTIME = DAYHK");
しかし、明らかに、「234231」は Arduino からの入力によって指定される変数であり、textbox2 の内容と SQL テーブルの更新に必要な値の両方を通知します。
私はC#に非常に慣れていませんが、これまでのところ、Arduinoからシリアルデータを読み取り、出力全体をtextbox1に入れることができるプログラムがありますが、それ以上はありません。
値に割り当てたい受信データの一部をテキストボックス 2 (またはテキストボックス 3) のテキスト出力に指定し、それを使用してそれと SQL テーブルに保持されている値の更新の両方を行うにはどうすればよいですか?
これまでの私のコードは次のとおりです。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Serial_Reader
{
public partial class Form1 : Form
{
string RxString;
public Form1()
{
InitializeComponent();
}
private void buttonStart_Click(object sender, EventArgs e)
{
serialPort1.PortName = "COM3";
serialPort1.BaudRate = 9600;
serialPort1.Open();
if (serialPort1.IsOpen)
{
buttonStart.Enabled = false;
buttonStop.Enabled = true;
buttonSQL.Enabled = true;
textBox1.ReadOnly = false;
}
}
private void buttonStop_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
buttonStart.Enabled = true;
buttonStop.Enabled = false;
buttonSQL.Enabled = false;
textBox1.ReadOnly = true;
}
}
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 serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
RxString = serialPort1.ReadExisting();
this.Invoke(new EventHandler(DisplayText));
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (serialPort1.IsOpen) serialPort1.Close();
}
private void DisplayText(object sender, EventArgs e)
{
textBox1.AppendText(RxString);
}
private void buttonSQL_Click(object sender, EventArgs e)
{
}
}
}
「私の」熟練したコメントからわかるように、プログラミングは不適切ですが、私はここの素晴らしいチュートリアルのコードを使用しています。私のプログラムに余分にあるのは、SQL通信を開始するボタンだけで、シリアル通信が停止すると停止します。および 2 つの追加のテキスト ボックス。
編集:
入力の形式は次のとおりです。
Someone got up from seat number 9
All-time users on seat number 9 is: 5
Today's total users: 17
All-time total users: 17
シリアルモニターをチェックしているプログラマー以外の人が出力を簡単に理解できるようにします。1 行目は常に「Someone got up from」または「Someone sat down on」のいずれかで、2 行目では数字のみが変わり、3 行目と 4 行目でも数字が変わりますが、3 行目と 4 行目の数字は変わります (システムが 1 日未満しか実行されていないため、このケースも同じです) は、解析する必要があるものです。
編集 2 (新しいコード):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Serial_Reader
{
public partial class Form1 : Form
{
string RxString;
public Form1()
{
InitializeComponent();
}
private void buttonStart_Click(object sender, EventArgs e)
{
serialPort1.PortName = "COM3";
serialPort1.BaudRate = 9600;
serialPort1.Open();
if (serialPort1.IsOpen)
{
buttonStart.Enabled = false;
buttonStop.Enabled = true;
buttonSQL.Enabled = true;
textBox1.ReadOnly = true;
}
}
private void buttonStop_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
buttonStart.Enabled = true;
buttonStop.Enabled = false;
buttonSQL.Enabled = false;
textBox1.ReadOnly = true;
}
}
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 serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
RxString = serialPort1.ReadExisting();
this.Invoke(new EventHandler(DisplayText));
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (serialPort1.IsOpen) serialPort1.Close();
}
private void DisplayText(object sender, EventArgs e)
{
textBox1.AppendText(RxString);
}
private void buttonSQL_Click(object sender, EventArgs e)
{
// SqlConnection myConnection = new SqlConnection("user id=username;" +
// "password=password;server=serverurl;" +
// "Trusted_Connection=yes;" +
// "database=database; " +
// "connection timeout=30");
do
{
string dayUsers, allUsers;
string s1 = "Today's total users: ";
string s2 = "All-time total users: ";
if (RxString.Contains(s1))
{
dayUsers = RxString.Replace(s1, "");
textDU.Text = dayUsers;
// string queryDay = "UPDATE USER_NUMBERS SET NUMBERZ=" + dayUsers + "WHERE UPTIME=DAYHK";
}
if (RxString.Contains(s2))
{
allUsers = RxString.Replace(s2, "");
textAU.Text = allUsers;
// string queryAll = "UPDATE USER_NUMBERS SET NUMBERZ=" + allUsers + "WHERE UPTIME=ALLHK";
}
}
while (serialPort1.IsOpen);
}
private void textDU_TextChanged(object sender, EventArgs e)
{
}
private void textAU_TextChanged(object sender, EventArgs e)
{
}
}
}
編集 3 (着信データ):
if (abs (then [i] - now[i]) > THRESH)
{
Serial.print ("Someone ");
if (now [i] > then [i])
{
Serial.print ("sat on");
allusers++;
dayusers++;
digitalWrite(counterPin, HIGH); // hit counter
delay(75);
digitalWrite(counterPin, LOW);
val[i]++;
}
else
{
Serial.print ("got up from");
}
Serial.print (" seat number ");
Serial.println (i);
Serial.print ("All-time users on seat number ");
Serial.print (i);
Serial.print (" is: ");
Serial.println (val[i]);
Serial.print ("Today's total users: ");
Serial.println (dayusers);
Serial.print ("All-time total users: ");
Serial.println (allusers);
}