20230323_YTDX_upperpc/YTDX202303TCP/Form1.cs

493 lines
18 KiB
C#
Raw Permalink Normal View History

2023-07-24 08:07:03 +00:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Threading;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ToolBar;
namespace YTDX202303TCP
{
public partial class Form1 : Form
{
private string main_ip = "192.168.0.65"; //IP
private int main_port = 502; //端口
private Client _client = new Client(); //主板客户端
//单线程
private System.Threading.Timer _flash_page;
private Data data = new Data();
static bool isSending = false;//数据是否定时发送
private int station_id = 100;
public Form1()
{
InitializeComponent();
textBox1.Text = main_ip;
textBox2.Text = main_port.ToString();
textBox3.Text = station_id.ToString();
//刷新界面的线程
_flash_page = new System.Threading.Timer(new System.Threading.TimerCallback(WritetoPage), null, 2000, 1000);
}
/// <summary>
/// 数据写入界面
/// </summary>
/// <param name="sender"></param>
private void WritetoPage(object sender)
{
Invoke((EventHandler)(delegate
{
label7.Text = data.AIN0.ToString("0.00") + " V";
label15.Text = data.AIN1.ToString("0.00") + " A";
label16.Text = data.AIN2.ToString("0.00") + " V";
label17.Text = data.AIN3.ToString("0.00") + " V";
label18.Text = data.AIN4.ToString("0.00") + " ℃";
label19.Text = data.AIN5.ToString("0.00") + " %";
label20.Text = data.AIN6.ToString("0.00") + " V";
label21.Text = data.AIN7.ToString("0.00") + " V";
label34.Text = data.DO1;
label35.Text = data.DO2;
label36.Text = data.DO3;
label37.Text = data.DO4;
}));
}
/// <summary>
/// 连接
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "连接")
{
//连接Socket
_client.InitSocket(textBox1.Text, Convert.ToInt32(textBox2.Text));
bool isConnected = _client.Connect();
if (isConnected)
{
_client.pushSockets = ReceiveMess;//注册推送器
button1.Text = "断开";
textBox1.Enabled = false;
textBox2.Enabled = false;
}
}
else if (button1.Text == "断开")
{
if (button11.Text == "停止")
{
MessageBox.Show("请先关闭自动读取功能!");
}
else
{
_client.Stop();
button1.Text = "连接";
textBox1.Enabled = true;
textBox2.Enabled = true;
}
}
}
/// <summary>
/// 客户端数据接收
/// </summary>
/// <param name="sks"></param>
private void ReceiveMess(Sockets sks)
{
if (sks.ex != null)
{
if (sks.ClientDispose == true)
{
//由于未知原因引发异常.导致客户端下线. 比如网络故障.或服务器断开连接.
//wirte_textbox(string.Format("客户端下线.!异常消息:{0}\r\n", sks.ex));
}
else
{
//SetClientState(string.Format("异常消息:{0}\r\n", sks.ex));
}
//timerConnect.Enabled = true;
}
else if (sks.Offset == 0)
{
//客户端主动下线
//wirte_textbox("客户端下线!");
}
else
{
byte[] buffer = new byte[sks.Offset];
Array.Copy(sks.RecBuffer, buffer, sks.Offset);
string str = Encoding.UTF8.GetString(buffer);
try
{
ParsingData(new List<byte>(buffer));
}
catch (Exception)
{
return;
}
}
}
/// <summary>
/// 数据解析
/// </summary>
/// <param name="byteList"></param>
private void ParsingData(List<byte> byteList)
{
string rec_16 = null;
for (int i = 0; i < byteList.Count; i++)
{
rec_16 += byteList[i].ToString("X2"); //16进制显示
}
//string _message = tools.HexStringToString(rec_16, Encoding.Default).Replace("\r\n", "");
//校验当前List是否为空或者长度
if (byteList == null || byteList.Count == 0)
return;
//获取当前系统时间
DateTime time_DataBase = DateTime.Now;
//帧头
byte[] _header = new byte[] { (byte)station_id };
//功能码
byte _fun_code = 0;
//操作功能
byte _operation = 0;
//传感器类型
byte _kind = 0;
//校验帧头
if (byteList[0] != _header[0])
{
return;
}
//读取状态显示
if (rec_16.StartsWith(station_id.ToString("x2") + "0330"))
{
data.AIN0 = (float)tools.bytetofloat(byteList, 3) * 118F; //电压
data.AIN1 = (float)tools.bytetofloat(byteList, 7) / 0.75F; //电流
data.AIN2 = (float)tools.bytetofloat(byteList, 11);
data.AIN3 = (float)tools.bytetofloat(byteList, 15);
data.AIN4 = (float)tools.bytetofloat(byteList, 19) * 100F; //温度
data.AIN5 = (float)tools.bytetofloat(byteList, 23) * 100F / 3F; //湿度
data.AIN6 = (float)tools.bytetofloat(byteList, 27);
data.AIN7 = (float)tools.bytetofloat(byteList, 31);
if (byteList[44] == 1)
{
data.DO1 = "开";
}
else
{
data.DO1 = "关";
}
if (byteList[46] == 1)
{
data.DO2 = "开";
}
else
{
data.DO2 = "关";
}
if (byteList[48] == 1)
{
data.DO3 = "开";
}
else
{
data.DO3 = "关";
}
if (byteList[50] == 1)
{
data.DO4 = "开";
}
else
{
data.DO4 = "关";
}
}
}
/// <summary>
/// 继电器1开
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
Control(20, 01);
}
/// <summary>
/// 继电器1关
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
Control(20, 00);
}
/// <summary>
/// 继电器2开
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button5_Click(object sender, EventArgs e)
{
Control(21, 01);
//Thread.Sleep(500);
//button3_Click(null, null);
}
/// <summary>
/// 继电器2关
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button6_Click(object sender, EventArgs e)
{
Control(21, 00);
}
/// <summary>
/// 继电器3开
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button7_Click(object sender, EventArgs e)
{
Control(22, 01);
}
/// <summary>
/// 继电器3关
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button8_Click(object sender, EventArgs e)
{
Control(22, 00);
}
/// <summary>
/// 继电器4开
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button9_Click(object sender, EventArgs e)
{
Control(23, 01);
}
/// <summary>
/// 继电器4关
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button10_Click(object sender, EventArgs e)
{
Control(23, 00);
}
/// <summary>
/// 读取
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
//byte[] byte_read = new byte[] { (byte)station_id, 03, 00, 00, 00, 24, 76, 53 };
byte[] byte_read = new byte[] { (byte)station_id, 03, 00, 00, 00, 24 };
byte[] nr_crc = new byte[2];
nr_crc = Crc16(byte_read, byte_read.Length);
List<byte> data = byte_read.ToList();
data.AddRange(nr_crc);
string list = tools.byteToHexStr(data.ToArray());
byte[] bytea = tools.ConvertHexStringToBytes(list);
_client.SendData(bytea);
}
//创建读取线程
private System.Threading.Timer timer_Read;
/// <summary>
/// 定时读取
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button11_Click(object sender, EventArgs e)
{
if (button11.Text == "自动读取")
{
int time = 1;//间隔
isSending = true;
//byte[] byte_read = new byte[] { (byte)station_id, 03, 00, 00, 00, 24 };
//byte[] nr_crc = new byte[2];
//nr_crc = Crc16(byte_read, byte_read.Length);
//List<byte> data = byte_read.ToList();
//data.AddRange(nr_crc);
//string list = tools.byteToHexStr(data.ToArray());
//byte[] bytea = tools.ConvertHexStringToBytes(list);
//Time_Send(time, bytea);
timer_Read = new System.Threading.Timer(new System.Threading.TimerCallback(Time_Send), null, 0, 1000);
button11.Text = "停止";
}
else if (button11.Text == "停止")
{
timer_Read.Dispose();
isSending = false;
button11.Text = "自动读取";
}
}
/// <summary>
/// 定时发送数据
/// </summary>
/// <param name="time"></param>
/// <param name="message"></param>
//private async void Time_Send(int time, byte[] message)
//{
// while (isSending)
// {
// _client.SendData(message);
// //间隔
// await Task.Delay(1000 * time);
// }
//}
private void Time_Send(object sender)
{
byte[] byte_read = new byte[] { (byte)station_id, 03, 00, 00, 00, 24 };
byte[] nr_crc = new byte[2];
nr_crc = Crc16(byte_read, byte_read.Length);
List<byte> data = byte_read.ToList();
data.AddRange(nr_crc);
string list = tools.byteToHexStr(data.ToArray());
byte[] bytea = tools.ConvertHexStringToBytes(list);
_client.SendData(bytea);
}
/// <summary>
/// 远程控制
/// </summary>
/// <param name="kind"></param>
/// <param name="Sensor_Id"></param>
/// <param name="crc_1"></param>
/// <param name="cec_2"></param>
private void Control(int kind, int Sensor_Id)
{
byte[] byteaq = new byte[] { (byte)station_id, 16, 00, (byte)kind, 00, 01, 02, 00, (byte)Sensor_Id };
byte[] nr_crc = new byte[2];
nr_crc = Crc16(byteaq, byteaq.Length);
List<byte> data = byteaq.ToList();
data.AddRange(nr_crc);
string list = tools.byteToHexStr(data.ToArray());
byte[] bytea = tools.ConvertHexStringToBytes(list);
_client.SendData(bytea);
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox3.Text))
return;
station_id = Convert.ToInt32(this.textBox3.Text);
}
#region CRC校验
private static readonly byte[] aucCRCHi = {
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40
};
private static readonly byte[] aucCRCLo = {
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7,
0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E,
0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9,
0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 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0x98, 0x88, 0x48, 0x49, 0x89,
0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83,
0x41, 0x81, 0x80, 0x40
};
private static byte[] Crc16(byte[] pucFrame, int usLen)
{
int i = 0;
byte crcHi = 0xFF;
byte crcLo = 0xFF;
UInt16 iIndex = 0x0000;
while (usLen-- > 0)
{
iIndex = (UInt16)(crcLo ^ pucFrame[i++]);
crcLo = (byte)(crcHi ^ aucCRCHi[iIndex]);
crcHi = aucCRCLo[iIndex];
}
return new byte[] { crcLo, crcHi };
}
#endregion
}
}