1、类库源码:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace LS_HIK_5x
{
public class Led
{
public string ip_add { get; set; }
public ushort port { get; set; }
public Led(string _ip_add, ushort _port)
{
ip_add = _ip_add;
port = _port;
}
public Led(string _ip_add)
{
ip_add = _ip_add;
port = 10000; //默认端口 TCP:10000
}
Encoding GB2312 = Encoding.GetEncoding("GB2312");
public bool led_show(string text, Color color, out string e)
{
try
{
//文本内码更新
byte[] cmd_text_0 = { 0x55, 0xAA, 0x00, 0x00, 0x01, 0x00, 0x00, 0xDA, 0x00, 0x00 };
byte[] cmd_text_1 = { 0x3D, 0x00, 0x00, 0x00 };//包长度(数据长度+51)
byte[] cmd_text_2 = { 0x01, 0x01 };
byte[] cmd_text_3 = { 0x3D, 0x00, 0x00, 0x00 };//帧长度(数据长度+51)
byte[] cmd_text_4 = { 0x01, 0x01 };
byte[] cmd_text_5 = { 0x3C, 0x00, 0x00, 0x00 };//节目长度(数据长度+50)
byte[] cmd_text_6 = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };
byte[] cmd_text_7 = { 0x24, 0x00, 0x00, 0x00 };//区域数据长度 (数据长度+26)
byte[] cmd_text_8 = { 0x0E, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x00, 0x3F, 0x00 };
byte[] cmd_text_9 = { 0x02 };//颜色 01 红 02 绿
byte[] cmd_text_10 = { 0x00, 0x00, 0x01, 0xFF, 0x01, 0x01, 0x10 };
byte[] cmd_text_11 = { 0x0A, 0x00, 0x00, 0x00 };//数据长度 低位在前
byte[] cmd_text_12 = { 0xD2, 0xBB, 0xC2, 0xB7, 0xC6, 0xBD, 0xB0, 0xB2, 0x38, 0x36 };//数据内容
byte[] cmd_text_13 = { 0x00, 0x00, 0x0D, 0x0A };//帧尾
//打开连接
TcpClient led = new TcpClient();
led.Connect(IPAddress.Parse(ip_add), port);
NetworkStream ns = led.GetStream();
if (color == Color.Red)
{
cmd_text_9 = new byte[] { 0x01 };
}
else if (color == Color.Green)
{
cmd_text_9 = new byte[] { 0x02 };
}
else if (color == Color.Yellow)
{
cmd_text_9 = new byte[] { 0x03 };
}
cmd_text_12 = GB2312.GetBytes(text);
cmd_text_11 = BitConverter.GetBytes(cmd_text_12.Length);
cmd_text_7 = BitConverter.GetBytes(cmd_text_12.Length + 26);
cmd_text_5 = BitConverter.GetBytes(cmd_text_12.Length + 50);
cmd_text_3 = BitConverter.GetBytes(cmd_text_12.Length + 51);
cmd_text_1 = BitConverter.GetBytes(cmd_text_12.Length + 51);
byte[] cmd_text = new byte[cmd_text_0.Length + cmd_text_1.Length + cmd_text_2.Length + cmd_text_3.Length + cmd_text_4.Length + cmd_text_5.Length + cmd_text_6.Length + cmd_text_7.Length + cmd_text_8.Length + cmd_text_9.Length + +cmd_text_10.Length + cmd_text_11.Length + cmd_text_12.Length + cmd_text_13.Length];
cmd_text = cmd_text_0.Concat(cmd_text_1).ToArray();
cmd_text = cmd_text.Concat(cmd_text_2).ToArray();
cmd_text = cmd_text.Concat(cmd_text_3).ToArray();
cmd_text = cmd_text.Concat(cmd_text_4).ToArray();
cmd_text = cmd_text.Concat(cmd_text_5).ToArray();
cmd_text = cmd_text.Concat(cmd_text_6).ToArray();
cmd_text = cmd_text.Concat(cmd_text_7).ToArray();
cmd_text = cmd_text.Concat(cmd_text_8).ToArray();
cmd_text = cmd_text.Concat(cmd_text_9).ToArray();
cmd_text = cmd_text.Concat(cmd_text_10).ToArray();
cmd_text = cmd_text.Concat(cmd_text_11).ToArray();
cmd_text = cmd_text.Concat(cmd_text_12).ToArray();
cmd_text = cmd_text.Concat(cmd_text_13).ToArray();
ns.Write(cmd_text, 0, cmd_text.Length);
//关闭连接
Thread.Sleep(1000);//稍作等待
ns.Close();
led.Close();
e = "发送成功";
return true;
}
catch (Exception ex)
{
e = "发送失败,原因:" + ex.Message;
return false;
//throw;
}
}
public void led_show_asyn(string text, Color color)
{
string info = "";
Thread th = new Thread(() =>
{
led_show(text, color, out info);
});
}
public bool led_speak(string text, out string e)
{
try
{
//语音播报
byte[] cmd_speak_0 = { 0x55, 0xAA, 0x00, 0x00, 0x01, 0x00, 0x00, 0xDA, 0x00, 0x00 };
byte[] cmd_speak_1 = { 0x36, 0x00, 0x00, 0x00 };//包长度(数据长度+51)
byte[] cmd_speak_2 = { 0x00, 0x00 };
byte[] cmd_speak_3 = { 0x36, 0x00, 0x00, 0x00 };//帧长度(数据长度+51)
byte[] cmd_speak_4 = { 0x01, 0x01 };
byte[] cmd_speak_5 = { 0x35, 0x00, 0x00, 0x00 };//节目长度(数据长度+50)
byte[] cmd_speak_6 = { 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01 };
byte[] cmd_speak_7 = { 0x1D, 0x00, 0x00, 0x00 };//区域数据长度 (数据长度+26)
byte[] cmd_speak_8 = { 0x2D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x01 };
byte[] cmd_speak_9 = { 0x03, 0x00, 0x00, 0x00 };//数据长度 低位在前
byte[] cmd_speak_10 = { 0x32, 0x33, 0x35 };//数据内容 (语音:“二百三十五”)
byte[] cmd_speak_11 = { 0x00, 0x00, 0x0D, 0x0A };//帧尾
//打开连接
TcpClient led = new TcpClient();
led.Connect(IPAddress.Parse(ip_add), port);
NetworkStream ns = led.GetStream();
cmd_speak_10 = GB2312.GetBytes(text);
cmd_speak_9 = BitConverter.GetBytes(cmd_speak_10.Length);
cmd_speak_7 = BitConverter.GetBytes(cmd_speak_10.Length + 26);
cmd_speak_5 = BitConverter.GetBytes(cmd_speak_10.Length + 50);
cmd_speak_3 = BitConverter.GetBytes(cmd_speak_10.Length + 51);
cmd_speak_1 = BitConverter.GetBytes(cmd_speak_10.Length + 51);
byte[] cmd_speak = new byte[cmd_speak_0.Length + cmd_speak_1.Length + cmd_speak_2.Length + cmd_speak_3.Length + cmd_speak_4.Length + cmd_speak_5.Length + cmd_speak_6.Length + cmd_speak_7.Length + cmd_speak_8.Length + cmd_speak_9.Length + +cmd_speak_10.Length + cmd_speak_11.Length];
cmd_speak = cmd_speak_0.Concat(cmd_speak_1).ToArray();
cmd_speak = cmd_speak.Concat(cmd_speak_2).ToArray();
cmd_speak = cmd_speak.Concat(cmd_speak_3).ToArray();
cmd_speak = cmd_speak.Concat(cmd_speak_4).ToArray();
cmd_speak = cmd_speak.Concat(cmd_speak_5).ToArray();
cmd_speak = cmd_speak.Concat(cmd_speak_6).ToArray();
cmd_speak = cmd_speak.Concat(cmd_speak_7).ToArray();
cmd_speak = cmd_speak.Concat(cmd_speak_8).ToArray();
cmd_speak = cmd_speak.Concat(cmd_speak_9).ToArray();
cmd_speak = cmd_speak.Concat(cmd_speak_10).ToArray();
cmd_speak = cmd_speak.Concat(cmd_speak_11).ToArray();
ns.Write(cmd_speak, 0, cmd_speak.Length);
//关闭连接
Thread.Sleep(1000);//稍作等待
ns.Close();
led.Close();
e = "发送成功";
return true;
}
catch (Exception ex)
{
e = "发送失败,原因:" + ex.Message;
return false;
//throw;
}
}
}
}
2、调用示例:
using LS_HIK_5x;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 海康DS_TVL224LED屏文本与语音测试
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e) //语音播报
{
string err;
Led led = new Led(textBox1.Text,Convert.ToUInt16(textBox2.Text));
led.led_speak(textBox4.Text,out err);
Debug.Print(err);
}
private void button1_Click(object sender, EventArgs e) //文本更新
{
string err;
Led led = new Led(textBox1.Text, Convert.ToUInt16(textBox2.Text));
Color text_color;
if (radioButton1.Checked)
{
text_color = Color.Red;
}
else
{
text_color = Color.Green;
}
led.led_show(textBox4.Text, text_color, out err);
Debug.Print(err);
}
}
}