工具
打开:
某个设备的设置
工作模式是重点,必须是server
///
/// 泥人网络继电器
///
public class NRNetRelayBLL
{
Models.TerminalM haredWareCommonM;
Socket client;
public NRNetRelayBLL(Models.TerminalM terminalM)
{
haredWareCommonM = terminalM;
}
#region 打开某个输出口
//stach begin 1 2 3 4 5 6 ..................
///
/// 打开某个输出口
///
///
///
public bool HandleStach(int num)
{
try
{
///创建客户端
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
///IP地址
IPAddress ip = IPAddress.Parse(haredWareCommonM.ip);
///端口号
IPEndPoint endPoint = new IPEndPoint(ip, (int)haredWareCommonM.port);
///建立与服务器的远程连接
client.Connect(endPoint);
///线程问题
//Thread thread = new Thread(ReciveMsg);
//thread.IsBackground = true;
//thread.Start(client);
//将字符串指令转换为byte数组
byte[] buf = System.Text.Encoding.Default.GetBytes("AT+STACH" + num + "=1,1\r\n");
//发送AT指令
client.Send(buf);
byte[] recbuf = new byte[10];//真正使用时,接收缓冲区需要适当的调整
//等待硬件响应命令,接收到的数据为byte数组
//会等待到有数据返回为止
int recLen = client.Receive(recbuf);
client.Close();
//将byte数组转为字符串
string str = System.Text.Encoding.Default.GetString(recbuf);
str = str.Replace("\r\n", "").Trim().ToLower();
if (!str.Contains("ok"))
{
Common.Log4.LogManage.WriteErr(str);
Common.DelegateHelper.InfoMessageEvent?.Invoke(str);
return false;
}
return true;
}
catch (Exception ex)
{
Common.Log4.LogManage.WriteErr(ex.ToString());
Common.DelegateHelper.InfoMessageEvent?.Invoke(ex.Message);
return false;
}
}
#endregion
#region 获取对应输入的结果
///
/// 获取对应输入的结果
///
///
///
public bool SearchStateIn(int num = 0)
{
try
{
///创建客户端
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
///IP地址
IPAddress ip = IPAddress.Parse(haredWareCommonM.ip);
///端口号
IPEndPoint endPoint = new IPEndPoint(ip, (int)haredWareCommonM.port);
///建立与服务器的远程连接
client.Connect(endPoint);
///线程问题
//Thread thread = new Thread(ReciveMsg);
//thread.IsBackground = true;
//thread.Start(client);
//将字符串指令转换为byte数组
byte[] buf = System.Text.Encoding.Default.GetBytes("AT+OCCH" + num + "=?\r\n");
//发送AT指令
client.Send(buf);
byte[] recbuf = new byte[10];//真正使用时,接收缓冲区需要适当的调整
//等待硬件响应命令,接收到的数据为byte数组
//会等待到有数据返回为止
int recLen = client.Receive(recbuf);
client.Close();
//将byte数组转为字符串
string str = System.Text.Encoding.Default.GetString(recbuf);
str = str.Replace("\r\n", "").Trim().ToLower();
if (!str.Contains("ok"))
{
Common.Log4.LogManage.WriteErr(str);
Common.DelegateHelper.InfoMessageEvent?.Invoke(str);
return false;
}
return true;
}
catch (Exception ex)
{
Common.Log4.LogManage.WriteErr(ex.ToString());
Common.DelegateHelper.InfoMessageEvent?.Invoke(ex.Message);
return false;
}
}
#endregion
}
调用
public partial class 泥人网络继电器 : Form
{
HardWareBLL.NRNetRelayBLL nRNetRelayBLL;
//泥人网络继电器 i202
public 泥人网络继电器()
{
InitializeComponent();
}
Socket client;
private void button1_Click(object sender, EventArgs e)
{
#if false
///创建客户端
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
///IP地址
IPAddress ip = IPAddress.Parse(textBoxIp.Text.Trim());
///端口号
IPEndPoint endPoint = new IPEndPoint(ip, int.Parse(textBoxPort.Text.Trim()));
///建立与服务器的远程连接
client.Connect(endPoint);
///线程问题
//Thread thread = new Thread(ReciveMsg);
//thread.IsBackground = true;
//thread.Start(client);
//将字符串指令转换为byte数组
byte[] buf = System.Text.Encoding.Default.GetBytes("AT+STACH1=1,1\r\n");
//发送AT指令
client.Send(buf);
byte[] recbuf = new byte[10];//真正使用时,接收缓冲区需要适当的调整
//等待硬件响应命令,接收到的数据为byte数组
//会等待到有数据返回为止
int recLen = client.Receive(recbuf);
client.Close();
//将byte数组转为字符串
string str = System.Text.Encoding.Default.GetString(recbuf);
str = str.Replace("\r\n", "");
MessageBox.Show(str);
#endif
byte num = Convert.ToByte(textBoxOut.Text.Trim());
Common.ShowBlackBox.WriteLine("1 num is " + num);
bool res = nRNetRelayBLL.HandleStach(num);
if (!res)
{
Common.ShowBlackBox.WriteLine("3 send message fail");
return;
}
Common.ShowBlackBox.WriteLine("1 send message success");
}
//init
private void button3_Click(object sender, EventArgs e)
{
Models.TerminalM terminalM = new Models.TerminalM();
terminalM.ip = textBoxIp.Text.Trim();
terminalM.port = Convert.ToInt32(textBoxPort.Text.Trim());
nRNetRelayBLL = new HardWareBLL.NRNetRelayBLL(terminalM);
Common.ShowBlackBox.WriteLine("1 init success");
}
private void button4_Click(object sender, EventArgs e)
{
int num = 0;
if (!string.IsNullOrEmpty(textBox1.Text.Trim()))
{
num = Convert.ToInt32(textBox1.Text.Trim());
}
Common.ShowBlackBox.WriteLine("1 nRNetRelayBLL SearchStateIn "+ nRNetRelayBLL.SearchStateIn(num));
Common.ShowBlackBox.WriteLine("1 nRNetRelayBLL success");
}
}