20230206_new_materil_emb/上位机/XCLPTInternalTesting/Form1.cs
2023-08-03 09:23:26 +08:00

431 lines
16 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace XCLPTInternalTesting
{
public partial class Form1 : Form
{
private string main_ip = "192.168.127.254"; //IP
//端口号
string[] btl = new string[] { "4001", "4002", "4003", "4004" };
//单线程
private System.Threading.Timer _flash_page;
private Client _client = new Client(); //主板客户端
private int station_id = 01;//子站地址
private Data data = new Data();//系统状态数据
public Form1()
{
InitializeComponent();
textBox1.Text = main_ip;
for (int i = 0; i < btl.Length; i++)
{
comboBox1.Items.Add(btl[i]);
}
//刷新界面的线程
_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
{
label10.Text = data.OneLine.ToString("0.00") + " V";
label11.Text = data.TwoLine.ToString("0.00") + " V";
label12.Text = data.ThreeLine.ToString("0.00") + " V";
label13.Text = data.ForeLine.ToString("0.00") + " V";
label14.Text = data.FiveLine.ToString("0.00") + " V";
label17.Text = data.Tem.ToString("0.00") + " ℃";
label18.Text = data.Hum.ToString("0.00") + " %";
}));
}
/// <summary>
/// 连接
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(comboBox1.Text))
{
MessageBox.Show("请选择端口号后重新连接!");
}
else
{
if (button1.Text == "连接")
{
//连接Socket
_client.InitSocket(textBox1.Text, Convert.ToInt32(comboBox1.Text));
bool isConnected = _client.Connect();
if (isConnected)
{
_client.pushSockets = ReceiveMess;//注册推送器
button1.Text = "断开";
textBox1.Enabled = false;
comboBox1.Enabled = false;
}
}
else if (button1.Text == "断开")
{
_client.Stop();
button1.Text = "连接";
textBox1.Enabled = true;
comboBox1.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));
buffer = null;
}
catch (Exception)
{
return;
}
}
}
/// <summary>
/// 数据解析
/// </summary>
/// <param name="byteList"></param>
private void ParsingData(List<byte> byteList)
{
//功能码
byte _kind = 0;
//显示报文
string message = "";
//获取当前系统时间
DateTime time_DataBase = DateTime.Now;
//电压数据 02 04 0A 2F 21 5D B0 5D 91 5E 17 2E F0 4A 43 (2F 21--12065/1000=12.065V)
//温湿度数据: 01 03 04 02 58 00 CC 7A 0D02 58 -- 湿度 00 CC -- 温度 7A 0D---CRC校验
//继电器开关状态返回:01 06 00 00 00 01 48 0A (Y1启动)
// 01 06 00 01 00 01 19 CA (Y2启动)
string rec_16 = null;
for (int i = 0; i < byteList.Count; i++)
{
rec_16 += byteList[i].ToString("X2"); //16进制显示
}
//状态数据显示
if (rec_16.StartsWith("0106"))
{
_kind = byteList[3];
switch (_kind)
{
//Y1启动
case 00:
message += "第1路已打开";
break;
//Y2启动
case 01:
message += "第2路已打开";
break;
//Y3启动
case 02:
message += "第3路已打开";
break;
//Y4启动
case 03:
message += "第4路已打开";
break;
//Y5启动
case 04:
message += "第5路已打开";
break;
default:
Console.WriteLine("未能识别类型!");
break;
}
wirte_textbox(message);
}
//电压数据
else if (rec_16.StartsWith("02040A"))
{
data.OneLine = ((float)tools.bytetoint(byteList, 3, 2)) / 1000F;
data.TwoLine = ((float)tools.bytetoint(byteList, 5, 2)) / 1000F;
data.ThreeLine = ((float)tools.bytetoint(byteList, 7, 2)) / 1000F;
data.ForeLine = ((float)tools.bytetoint(byteList, 9, 2)) / 1000F;
data.FiveLine = ((float)tools.bytetoint(byteList, 11, 2)) / 1000F;
}
//温湿度数据
else if (rec_16.StartsWith("010304"))
{
data.Hum = ((float)tools.bytetoint(byteList, 3, 2)) / 10F;
data.Tem = ((float)tools.bytetoint(byteList, 5, 2)) / 10F;
}
}
public void wirte_textbox(string rec_16)
{
//获取当前系统时间
DateTime time_DataBase = DateTime.Now;
Invoke((EventHandler)(delegate
{
textBox2.AppendText(time_DataBase + ":" + rec_16 + "\r\n" + "\r\n");
}));
}
/// <summary>
/// 开启继电器Y1 --24V摄像
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
Control(00);
}
/// <summary>
/// 开启继电器Y2 --24V传感器1
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
Control(01);
}
/// <summary>
/// 开启继电器Y3 ---24V传感器2
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
Control(02);
}
/// <summary>
/// 开启继电器Y4 --24V预留口
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button5_Click(object sender, EventArgs e)
{
Control(03);
}
/// <summary>
/// 开启继电器Y5 --24v多参数
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button6_Click(object sender, EventArgs e)
{
Control(04);
}
/// <summary>
/// 关闭所有传感器开关
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button9_Click(object sender, EventArgs e)
{
byte[] byteaq = new byte[] { 01, 16, 00, 00, 00, 05, 16, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00 };
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);
}
/// <summary>
/// 查询5路电压值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button7_Click(object sender, EventArgs e)
{
byte[] byteaq = new byte[] { 02, 04, 00, 00, 00, 05 };
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);
}
/// <summary>
/// 查询温湿度
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button8_Click(object sender, EventArgs e)
{
byte[] byteaq = new byte[] { 01, 03, 00, 00, 00, 02 };
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);
}
/// <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)
{
byte[] byteaq = new byte[] { (byte)station_id, 06, 00, (byte)kind, 00, 01 };
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);
}
#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, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC,
0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32,
0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D,
0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38,
0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF,
0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1,
0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4,
0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB,
0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA,
0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0,
0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97,
0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E,
0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 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
private void label5_Click(object sender, EventArgs e)
{
}
}
}