440 lines
14 KiB
C#
440 lines
14 KiB
C#
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;
|
|
using System.IO.Ports;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Serialization;
|
|
//using System.Text.Json;
|
|
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
|
|
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ToolBar;
|
|
using System.Threading;
|
|
using System.Net.Http;
|
|
//using System.Text.Json.Serialization;
|
|
|
|
namespace WindowsFormsApp1
|
|
{
|
|
public partial class frmMain : Form
|
|
{
|
|
bool _continue;
|
|
public frmMain()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
private void frmMain_Load(object sender, EventArgs e)
|
|
{
|
|
string[] ports = SerialPort.GetPortNames();
|
|
|
|
cbPorts.Items.Clear();
|
|
|
|
foreach (string port in ports)
|
|
{
|
|
cbPorts.Items.Add(port);
|
|
}
|
|
|
|
cbPorts.SelectedIndex = 0;
|
|
cbBaudRate.SelectedIndex = 11;
|
|
|
|
serialPort1 = new SerialPort
|
|
{
|
|
PortName = cbPorts.Text,
|
|
BaudRate = int.Parse(cbBaudRate.Text),
|
|
Parity = Parity.None,
|
|
StopBits = StopBits.One,
|
|
DataBits = 8,
|
|
Handshake = Handshake.RequestToSend,
|
|
DtrEnable = true,
|
|
RtsEnable = true,
|
|
NewLine = System.Environment.NewLine
|
|
};
|
|
}
|
|
|
|
private class jsonData
|
|
{
|
|
public float tempC { get; set; }
|
|
public float tempF { get; set; }
|
|
public float tempH { get; set; }
|
|
}
|
|
|
|
private void setStatus(int status, string msg = "") {
|
|
switch (status) {
|
|
case 0:
|
|
if (lblStatus.InvokeRequired)
|
|
{
|
|
lblStatus.Invoke(new Action(() =>
|
|
{
|
|
lblStatus.Text = "Connecting...";
|
|
lblStatus.ForeColor = Color.SteelBlue;
|
|
}));
|
|
}
|
|
else
|
|
{
|
|
lblStatus.Text = "Connecting...";
|
|
lblStatus.ForeColor = Color.SteelBlue;
|
|
}
|
|
break;
|
|
case 1:
|
|
if (lblStatus.InvokeRequired)
|
|
{
|
|
lblStatus.Invoke(new Action(() =>
|
|
{
|
|
lblStatus.Text = "Connected";
|
|
lblStatus.ForeColor = Color.Green;
|
|
}));
|
|
}
|
|
else
|
|
{
|
|
lblStatus.Text = "Connected";
|
|
lblStatus.ForeColor = Color.Green;
|
|
}
|
|
break;
|
|
case 2:
|
|
if (lblStatus.InvokeRequired)
|
|
{
|
|
lblStatus.Invoke(new Action(() =>
|
|
{
|
|
lblStatus.Text = "Error: " + msg;
|
|
lblStatus.ForeColor = Color.Red;
|
|
}));
|
|
}
|
|
else
|
|
{
|
|
lblStatus.Text = "Error: " + msg;
|
|
lblStatus.ForeColor = Color.Red;
|
|
}
|
|
break;
|
|
case 3:
|
|
if (lblStatus.InvokeRequired)
|
|
{
|
|
lblStatus.Invoke(new Action(() =>
|
|
{
|
|
lblStatus.Text = "Disconnected";
|
|
lblStatus.ForeColor = Color.Black;
|
|
}));
|
|
}
|
|
else
|
|
{
|
|
lblStatus.Text = "Disconnected";
|
|
lblStatus.ForeColor = Color.Black;
|
|
}
|
|
break;
|
|
case 4:
|
|
if (lblStatus.InvokeRequired)
|
|
{
|
|
lblStatus.Invoke(new Action(() =>
|
|
{
|
|
lblStatus.Text = msg;
|
|
lblStatus.ForeColor = Color.Red;
|
|
}));
|
|
}
|
|
else
|
|
{
|
|
lblStatus.Text = msg;
|
|
lblStatus.ForeColor = Color.Red;
|
|
}
|
|
break;
|
|
case 5:
|
|
if (lblStatus.InvokeRequired)
|
|
{
|
|
lblStatus.Invoke(new Action(() =>
|
|
{
|
|
lblStatus.Text = msg;
|
|
lblStatus.ForeColor = Color.Green;
|
|
}));
|
|
}
|
|
else
|
|
{
|
|
lblStatus.Text = msg;
|
|
lblStatus.ForeColor = Color.Red;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void btnConnect_Click(object sender, EventArgs e)
|
|
{
|
|
if (btnConnect.Text.Equals("Connect"))
|
|
{
|
|
serialPort1.PortName = cbPorts.Text;
|
|
serialPort1.BaudRate = int.Parse(cbBaudRate.Text);
|
|
|
|
if (serialPort1 != null)
|
|
{
|
|
setStatus(0);
|
|
if (!serialPort1.IsOpen)
|
|
{
|
|
try
|
|
{
|
|
serialPort1.Open();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
btnConnect.Text = "Connect";
|
|
setStatus(2, ex.Message);
|
|
_continue = false;
|
|
serialPort1.Close();
|
|
return;
|
|
}
|
|
setStatus(1);
|
|
btnConnect.Text = "Disconnect";
|
|
//tReader = new Thread(DataReader);
|
|
_continue = true;
|
|
//tReader.Start();
|
|
|
|
//DataReader();
|
|
serialPort1.DataReceived += serialPort1_DataReceived;
|
|
|
|
//await DataReaderAsync();
|
|
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
_continue = false;
|
|
//tReader.Abort();
|
|
//Thread.Sleep(1000);
|
|
serialPort1.Close();
|
|
setStatus(3);
|
|
btnConnect.Text = "Connect";
|
|
}
|
|
}
|
|
|
|
private void DataReader() {
|
|
while (_continue)
|
|
{
|
|
if (serialPort1.IsOpen) {
|
|
string res;
|
|
res = serialPort1.ReadLine();
|
|
if (res != null)
|
|
{
|
|
//ChartAddY(res);
|
|
//if (setReading(res))
|
|
//sendToServer();
|
|
}
|
|
else
|
|
{
|
|
}
|
|
}
|
|
Thread.Sleep(1000);
|
|
}
|
|
}
|
|
|
|
private async Task DataReaderAsync()
|
|
{
|
|
await Task.Run(() =>
|
|
{
|
|
while (_continue)
|
|
{
|
|
if (serialPort1.IsOpen)
|
|
{
|
|
string res;
|
|
res = serialPort1.ReadLine();
|
|
if (res != null)
|
|
{
|
|
//ChartAddY(res);
|
|
if (setReading(res))
|
|
sendToServer();
|
|
}
|
|
else
|
|
{
|
|
}
|
|
Console.WriteLine("Reading....( " + res + " )");
|
|
}
|
|
//Thread.Sleep(1000);
|
|
}
|
|
});
|
|
}
|
|
|
|
private Boolean setReading(string data) {
|
|
Boolean res= false;
|
|
jsonData jd;
|
|
|
|
try {
|
|
//jd = JsonSerializer.Deserialize<jsonData>(data);
|
|
jd = JsonConvert.DeserializeObject<jsonData>(data);
|
|
}
|
|
catch (Exception ex) { return false; }
|
|
|
|
if (jd != null) {
|
|
res = true;
|
|
if (lblTempC.InvokeRequired)
|
|
{
|
|
lblTempC.Invoke(new Action(() => { lblTempC.Text = jd.tempC.ToString(); }));
|
|
|
|
if (jd.tempC > int.Parse(txtMaxC.Text))
|
|
lblTempC.Invoke(new Action(() => {
|
|
lblTempC.ForeColor = Color.Red;
|
|
lblTempF.ForeColor = Color.Red;
|
|
}));
|
|
|
|
if (jd.tempC < int.Parse(txtMinC.Text))
|
|
lblTempC.Invoke(new Action(() => {
|
|
lblTempC.ForeColor = Color.DeepSkyBlue;
|
|
lblTempF.ForeColor = Color.DeepSkyBlue;
|
|
}));
|
|
|
|
if (jd.tempC >= int.Parse(txtMinC.Text) && jd.tempC <= int.Parse(txtMaxC.Text))
|
|
lblTempC.Invoke(new Action(() => {
|
|
lblTempC.ForeColor = Color.Green;
|
|
lblTempF.ForeColor = Color.Green;
|
|
}));
|
|
|
|
}
|
|
else
|
|
{
|
|
lblTempC.Text = jd.tempC.ToString();
|
|
if (jd.tempC > int.Parse(txtMaxC.Text)) {
|
|
lblTempC.ForeColor = Color.Red;
|
|
lblTempF.ForeColor = Color.Red;
|
|
}
|
|
|
|
if (jd.tempC < int.Parse(txtMinC.Text))
|
|
{
|
|
lblTempC.ForeColor = Color.DeepSkyBlue;
|
|
lblTempF.ForeColor = Color.DeepSkyBlue;
|
|
}
|
|
|
|
if (jd.tempC >= int.Parse(txtMinC.Text) && jd.tempC <= int.Parse(txtMaxC.Text))
|
|
{
|
|
lblTempC.ForeColor = Color.Green;
|
|
lblTempF.ForeColor = Color.Green;
|
|
}
|
|
}
|
|
|
|
if (lblTempF.InvokeRequired)
|
|
{
|
|
lblTempF.Invoke(new Action(() => { lblTempF.Text = jd.tempF.ToString(); }));
|
|
}
|
|
else
|
|
{
|
|
lblTempF.Text = jd.tempF.ToString();
|
|
}
|
|
|
|
if (lblHumidity.InvokeRequired) {
|
|
lblHumidity.Invoke(new Action(() => { lblHumidity.Text = jd.tempH.ToString(); }));
|
|
}
|
|
else
|
|
{
|
|
lblHumidity.Text = jd.tempH.ToString();
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
private void txtMinC_TextChanged(object sender, EventArgs e)
|
|
{
|
|
double n = 0.00;
|
|
bool isNum = double.TryParse(txtMinC.Text, out n);
|
|
if (isNum == true) {
|
|
txtMinF.Text = ((n * 1.8) + 32).ToString("#,##0.00");
|
|
}
|
|
}
|
|
|
|
private void txtMaxC_TextChanged(object sender, EventArgs e)
|
|
{
|
|
double n = 0.00;
|
|
bool isNum = double.TryParse(txtMaxC.Text, out n);
|
|
if (isNum == true)
|
|
{
|
|
txtMaxF.Text = ((n * 1.8) + 32).ToString("#,##0.00");
|
|
}
|
|
}
|
|
|
|
private class tempData {
|
|
public int id { get; set; }
|
|
public DateTime templog { get; set; }
|
|
public decimal humidity { get; set; }
|
|
public decimal tempC { get; set; }
|
|
public decimal tempF { get; set; }
|
|
public int dev_ID { get; set; }
|
|
|
|
}
|
|
|
|
private async void sendToServer() {
|
|
//HttpClient client = new HttpClient();
|
|
DateTime dt_today = DateTime.Now;
|
|
|
|
//if (dt_today.Second == 0) {
|
|
using (var client = new HttpClient())
|
|
{
|
|
tempData data = new tempData();
|
|
data.id = 0;
|
|
data.templog = dt_today;
|
|
data.humidity = decimal.Parse(lblHumidity.Text);
|
|
data.tempC = decimal.Parse(lblTempC.Text);
|
|
data.tempF = decimal.Parse(lblTempF.Text);
|
|
data.dev_ID = 1;
|
|
|
|
//var json = JsonSerializer.Serialize<tempData>(data);
|
|
var json = JsonConvert.SerializeObject(data);
|
|
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
|
var response = await client.PostAsync(txtURL.Text, content);
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
Console.WriteLine("Successfully saved.");
|
|
else
|
|
Console.WriteLine("d2 ---> " + response.StatusCode.ToString() + " <---");
|
|
//setStatus(4, response.StatusCode.ToString());
|
|
}
|
|
//}
|
|
}
|
|
|
|
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
|
|
{
|
|
if (serialPort1.IsOpen)
|
|
{
|
|
string res;
|
|
try
|
|
{
|
|
res = serialPort1.ReadLine();
|
|
}
|
|
catch (Exception ex) {
|
|
setStatus(4, "DataReceived: Read Error.");
|
|
return;
|
|
}
|
|
|
|
if (res != null)
|
|
{
|
|
setStatus(5, "DataReceived: Setting Received values.");
|
|
setReading(res);
|
|
//if (setReading(res) && chkSend.Checked)
|
|
// sendToServer();
|
|
}
|
|
else
|
|
{
|
|
setStatus(5, "DataReceived: Empty.");
|
|
}
|
|
Console.WriteLine("Reading....( " + res + " )");
|
|
}
|
|
Thread.Sleep(500);
|
|
}
|
|
|
|
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
|
|
{
|
|
_continue = false;
|
|
serialPort1.Close();
|
|
}
|
|
|
|
private void timer1_Tick(object sender, EventArgs e)
|
|
{
|
|
DateTime dt_today = DateTime.Now;
|
|
|
|
if (dt_today.Second == 0 && chkSend.Checked)
|
|
{
|
|
sendToServer();
|
|
}
|
|
}
|
|
}
|
|
}
|