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.IO;
using System.IO.Ports;
using System.Data.OleDb;
using System.Data;
using System.Threading;
using System.IO.Ports;
using System.Windows.Forms;
namespace Lora_Temperature
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Set_bt_Click(object sender, EventArgs e)
{
}
int Connect_flag = 0;
private Thread t;
private Boolean receiving;
private SerialPort comport;
private Int32 totalLength = 0;
delegate void Display(Byte[] buffer);
private void Connect_bt_Click(object sender, EventArgs e)
{
Connect_flag++;
if (Connect_flag == 1)
{
try
{
serialPort1.PortName = (string)comboBox1.SelectedItem;
serialPort1.Open();
if (serialPort1.IsOpen)
{
//serialPort1.ReadTimeout = 2000;
//serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
Connect_bt.Text = "Disconnect";
receiving = true;
t = new Thread(DoReceive);
t.IsBackground = true;
t.Start();
}
}
catch (Exception)
{
MessageBox.Show("請選擇序列埠");
Connect_flag = 0;
}
}
else
{
serialPort1.Close();
Connect_bt.Text = "Connect";
receiving = false;
Connect_flag = 0;
}
}
private void DoReceive()
{
Byte[] buffer = new Byte[1024];
while (receiving)
{
try
{
if (serialPort1.BytesToRead > 11 && serialPort1.IsOpen)
{
Int32 length = serialPort1.Read(buffer, 0, buffer.Length);
Array.Resize(ref buffer, length);
Display d = new Display(DisplayText);
this.Invoke(d, new Object[] { buffer });
Array.Resize(ref buffer, 1024);
}
Thread.Sleep(16);
}
catch (Exception)
{
}
}
}
String getdata = "";
private void DisplayText(Byte[] buffer)
{
byte []datarecive = buffer;
string station = "";
string sensor_num = "";
string value = "";
int ddd;
getdata = String.Format("{0}{1}", BitConverter.ToString(buffer), Environment.NewLine);
getdata = getdata.Replace("-", " ");
station = buffer[0].ToString()+ buffer[1].ToString();
sensor_num = (Convert.ToChar((int)buffer[2])).ToString() + (Convert.ToChar((int)buffer[3])).ToString();
value = (Convert.ToChar((int)buffer[5])).ToString()+(Convert.ToChar((int)buffer[6])).ToString()
+ (Convert.ToChar((int)buffer[7])).ToString()+(Convert.ToChar((int)buffer[8])).ToString()
+(Convert.ToChar((int)buffer[9])).ToString();
/*for (int i = 0; i < 12 ;i++)
{
Console.Write(i+":");
Console.WriteLine(buffer[i]);
}*/
showdata(station, sensor_num, value);
}
private void showdata(string station, string sensor_num, string data)
{
switch (station)
{
case "01":
statino1(sensor_num, data);
break;
case "02":
statino2(sensor_num, data);
break;
case "03":
statino3(sensor_num, data);
break;
case "04":
statino4(sensor_num, data);
break;
case "05":
statino5(sensor_num, data);
break;
default:
break;
}
}
private void statino1(string sensor_num, string value)
{
switch (sensor_num)
{
case "01":
S11.Text = value;
break;
case "02":
S12.Text = value;
break;
case "03":
S13.Text = value;
break;
case "04":
S14.Text = value;
break;
case "05":
S15.Text = value;
break;
case "06":
S16.Text = value;
break;
case "07":
S17.Text = value;
break;
case "08":
S18.Text = value;
break;
case "09":
S19.Text = value;
break;
case "10":
S110.Text = value;
break;
default:
break;
}
}
private void statino2(string sensor_num, string value)
{
switch (sensor_num)
{
case "01":
S21.Text = value;
break;
case "02":
S22.Text = value;
break;
case "03":
S23.Text = value;
break;
case "04":
S24.Text = value;
break;
case "05":
S25.Text = value;
break;
case "06":
S26.Text = value;
break;
case "07":
S27.Text = value;
break;
case "08":
S28.Text = value;
break;
case "09":
S29.Text = value;
break;
case "10":
S210.Text = value;
break;
default:
break;
}
}
private void statino3(string sensor_num, string value)
{
switch (sensor_num)
{
case "01":
S31.Text = value;
break;
case "02":
S32.Text = value;
break;
case "03":
S33.Text = value;
break;
case "04":
S34.Text = value;
break;
case "05":
S35.Text = value;
break;
case "06":
S36.Text = value;
break;
case "07":
S37.Text = value;
break;
case "08":
S38.Text = value;
break;
case "09":
S39.Text = value;
break;
case "10":
S310.Text = value;
break;
default:
break;
}
}
private void statino4(string sensor_num, string value)
{
switch (sensor_num)
{
case "01":
S41.Text = value;
break;
case "02":
S42.Text = value;
break;
case "03":
S43.Text = value;
break;
case "04":
S44.Text = value;
break;
case "05":
S45.Text = value;
break;
case "06":
S46.Text = value;
break;
case "07":
S47.Text = value;
break;
case "08":
S48.Text = value;
break;
case "09":
S49.Text = value;
break;
case "10":
S410.Text = value;
break;
default:
break;
}
}
private void statino5(string sensor_num,string value)
{
switch (sensor_num)
{
case "01":
S51.Text = value;
break;
case "02":
S52.Text = value;
break;
case "03":
S53.Text = value;
break;
case "04":
S54.Text = value;
break;
case "05":
S55.Text = value;
break;
case "06":
S56.Text = value;
break;
case "07":
S57.Text = value;
break;
case "08":
S58.Text = value;
break;
case "09":
S59.Text = value;
break;
case "10":
S510.Text = value;
break;
default:
break;
}
}
private void serch_bt_Click(object sender, EventArgs e)
{
string[] serialPorts = SerialPort.GetPortNames();
comboBox1.Items.Clear();
foreach (string serialPort in serialPorts)
{
comboBox1.Items.Add(serialPort);
if (comboBox1.Items.Count > 0)
{
comboBox1.SelectedIndex = 0;
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
}
}
}
沒有留言:
發佈留言