1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
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.IO.Ports;
using System.Text.RegularExpressions;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
SerialPort port;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "Connecter")
{
port = new SerialPort();
port.BaudRate = 9600;
port.DataBits = 8;
port.StopBits = StopBits.One;
port.Parity = Parity.None;
port.PortName = comboPort.SelectedItem.ToString();
port.ReadTimeout = 200;
port.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
port.Open();
button1.Text = "Deconnecter";
}
else
{
port.Close();
button1.Text = "Connecter";
}
}
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
String texte = port.ReadExisting();
SetText(texte);
}
delegate void SetTextCallback(string text);
private void SetText(string text)
{
if (textBox4.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
textBox4.Invoke(d, new object[] {text});
}
else
{
textBox4.Text += text;
string[] newData = textBox4.Text.Split(new char[] { });
textBox1.Text = newData[0] + " %";
textBox2.Text = newData[1] + " °C";
textBox3.Text = newData[2] + " °C";
} |
Partager