老大,我试着把温湿度数组精简了 也不行~ 麻烦您帮我看看,问题究竟是出在哪了。太感谢您了!
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace Serial
{
public partial class mainFrom : Form
{
bool isOpen = false; //socket打开标志位
bool isSetProperty = false; //socket设置标志位
bool SolenoidvalveTemp = false;
IPAddress ips;
IPEndPoint ipe;
Socket socket = null; //声明一个Socket类
byte[] CMD_request = { 0xff, 0xaa, 0xaa, 0xaa, 0xff };
byte[] CMD_Solenoidvalve1_On = { 0xff, 0x00, 0x01, 0x01, 0xff }; //一路电磁阀打开
byte[] CMD_Solenoidvalve1_Off = { 0xff, 0x00, 0x01, 0x00, 0xff }; //一路电磁阀关闭
byte[] CMD_Recive = { };
public mainFrom()
{
InitializeComponent();
}
private void mainFrom_Load(object sender, EventArgs e)
{
tbxIPAdress.Text = "192.168.1.100";
tbxPort.Text = "2001";
}
void ReciveData()
{
try
{
byte[] data = new byte[15];
Socket d = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //创建一个Socket
d.Connect(ipe); //连接到服务器
d.Receive(data, data.Length, SocketFlags.None);
d.Close();
;
if (15 == data.Length) //改为{01 温度 湿度 02 温度 湿度 03 温度 湿度 04 温度 湿度 05 温度 湿度 }
{
lblTemp1.Text = data[1].ToString();
lblHumidity1.Text = data[2].ToString();
lblTemp2.Text = data[4].ToString();
lblHumidity2.Text = data[5].ToString();
lblTemp3.Text = data[7].ToString();
lblHumidity3.Text = data[8].ToString();
lblTemp4.Text = data[10].ToString();
lblHumidity4.Text = data[11].ToString();
lblTemp5.Text = data[13].ToString();
lblHumidity5.Text = data[14].ToString();
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
void SendData(byte[] data)
{
try
{
Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个Socket
c.Connect(ipe);//连接到服务器
byte[] bs = data;
c.Send(bs, bs.Length, 0);//发送测试信息
c.Close();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
private void btnStart_Click(object sender, EventArgs e)
{
if (isOpen == false)
{
if (!CheckSocketSetting())
{
MessageBox.Show("通讯地址未设置!", "错误提示");
return;
}
if (!isSetProperty) //通讯为设置
{
SetSocket();
isSetProperty = true;
}
try //打开socket
{
byte[] recive = new byte[1024];
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个Socket
isOpen = true;
btnStart.Text = "通讯中";
//socket打开后则相关的串口设置按钮便不可以再用
tbxIPAdress.Enabled = false;
tbxPort.Enabled = false;
}
catch (Exception f)
{
//socket打开失败后,相应标志位取消
socket.Close();
btnStart.Text = "开始通讯";
tbxIPAdress.Enabled = true;
tbxPort.Enabled = true;
isSetProperty = false;
isOpen = false;
MessageBox.Show(f.Message);
}
}
else
{
try //打开串口状态下
{
socket.Close();
isOpen = false;
isSetProperty = false;
btnStart.Text = "开始通讯";
//关闭串口后,串口设置选项便可以继续使用
tbxIPAdress.Enabled = true;
tbxPort.Enabled = true;
}
catch (Exception g)
{
MessageBox.Show(g.Message);
}
}
}
private void SetSocket()
{
ips = IPAddress.Parse(tbxIPAdress.Text);
ipe = new IPEndPoint(ips, Convert.ToInt32(tbxPort.Text));//把ip和端口转化为IPEndPoint实例
}
private void btnPoint1_Click(object sender, EventArgs e)
{
byte[] tempPoint = { 0xff, 0x01, 0x01, 0x00, 0xff };
if (tbxPoint1.Text == "")
{
MessageBox.Show("请设置湿度控制点", "错误提示");
return;
}
else
{
tempPoint[3] = byte.Parse(tbxPoint1.Text);
SendData(tempPoint);
}
}
private void btnOnOff01_Click(object sender, EventArgs e)
{
//一路电磁阀关闭
if (!SolenoidvalveTemp)
{
SendData(CMD_Solenoidvalve1_On);
SolenoidvalveTemp = true;
btnOnOff01.Text = "关";
}
else
{
SendData(CMD_Solenoidvalve1_Off);
SolenoidvalveTemp = false;
btnOnOff01.Text = "开";
}
}
private bool CheckSocketSetting() //检查socket是否设置
{
if (tbxIPAdress.Text.Trim() == "") return false;
if (tbxPort.Text.Trim() == "") return false;
return true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if(isOpen == true)
{
SendData(CMD_request);
ReciveData();
}
}
}
}
|