542 lines
27 KiB
C#
542 lines
27 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.ComponentModel;
|
|||
|
|
using System.Data;
|
|||
|
|
using System.Diagnostics;
|
|||
|
|
using System.Drawing;
|
|||
|
|
using System.IO;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Net;
|
|||
|
|
using System.Net.Sockets;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Threading;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using System.Windows.Forms;
|
|||
|
|
using System.Xml.Linq;
|
|||
|
|
|
|||
|
|
namespace QXXWCX
|
|||
|
|
{
|
|||
|
|
public partial class Form1 : Form
|
|||
|
|
{
|
|||
|
|
public Form1()
|
|||
|
|
{
|
|||
|
|
InitializeComponent();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//QXXWCXIP
|
|||
|
|
//string _ip = "192.168.1.204";
|
|||
|
|
string _ip = "192.168.1.204";
|
|||
|
|
|
|||
|
|
//端口号
|
|||
|
|
int _port = 11010;
|
|||
|
|
|
|||
|
|
Socket _client_QXXWCX;
|
|||
|
|
|
|||
|
|
bool canSend = false;
|
|||
|
|
|
|||
|
|
//数据接收线程
|
|||
|
|
Thread th_QXXWCX = null;
|
|||
|
|
|
|||
|
|
Thread th_FIle = null;
|
|||
|
|
|
|||
|
|
[Obsolete]
|
|||
|
|
//连接服务器
|
|||
|
|
private void btnConnect_Click(object sender, EventArgs e)
|
|||
|
|
{
|
|||
|
|
_client_QXXWCX = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
IPEndPoint point = new IPEndPoint(IPAddress.Parse(_ip), _port);
|
|||
|
|
_client_QXXWCX.Connect(point);
|
|||
|
|
th_QXXWCX = new Thread(ReceiveMessage_MEMS);
|
|||
|
|
th_QXXWCX.IsBackground = true;
|
|||
|
|
th_QXXWCX.Start();
|
|||
|
|
Console.WriteLine("已连接服务器" + "\r\n");
|
|||
|
|
CallInvokeUIUpdate("UpdatertbMessage", "已连接服务器!\n");
|
|||
|
|
}
|
|||
|
|
catch (Exception)
|
|||
|
|
{
|
|||
|
|
Console.WriteLine("服务器没有开启" + "\r\n");
|
|||
|
|
CallInvokeUIUpdate("UpdatertbMessage", "服务器没有开启!\n");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
public void SendMessage_MEMS(byte[] messageBytes)
|
|||
|
|
{
|
|||
|
|
if (_client_QXXWCX.Connected)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
_client_QXXWCX.Send(messageBytes);
|
|||
|
|
CallInvokeUIUpdate("UpdatertbMessage", "发送数据:" + BitConverter.ToString(messageBytes) + "\n");
|
|||
|
|
}
|
|||
|
|
catch (Exception)
|
|||
|
|
{
|
|||
|
|
Console.WriteLine("发送数据失败!" + "\r\n");
|
|||
|
|
CallInvokeUIUpdate("UpdatertbMessage", "发送数据失败!\n");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
Console.WriteLine("连接断开!" + "\r\n");
|
|||
|
|
CallInvokeUIUpdate("UpdatertbMessage", "连接断开!\n");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
// 弹出窗口 提示
|
|||
|
|
void SendMsgSilent(string msg)
|
|||
|
|
{
|
|||
|
|
void msgSend() { MessageBox.Show(msg, "提示"); }
|
|||
|
|
Thread td_msg = new Thread(msgSend);
|
|||
|
|
td_msg.Start();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private static bool PasswordEquals(byte[] b1, byte[] b2)
|
|||
|
|
{
|
|||
|
|
if (b1 == null || b2 == null) return false;
|
|||
|
|
for (int i = 0; i < b2.Length; i++)
|
|||
|
|
{
|
|||
|
|
if (b1[i] != b2[i])
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
///
|
|||
|
|
/// </summary>
|
|||
|
|
public void ReceiveMessage_MEMS()
|
|||
|
|
{
|
|||
|
|
byte[] messageBytes = null;
|
|||
|
|
int num = 0;
|
|||
|
|
int a = 0;
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
while (true)
|
|||
|
|
{
|
|||
|
|
//while (canSend == false)
|
|||
|
|
//{
|
|||
|
|
messageBytes = new byte[_client_QXXWCX.ReceiveBufferSize];
|
|||
|
|
num = _client_QXXWCX.Receive(messageBytes);
|
|||
|
|
|
|||
|
|
byte[] buffer = new byte[num];
|
|||
|
|
|
|||
|
|
for (int i = 0; i < num; i++)
|
|||
|
|
{
|
|||
|
|
buffer[i] = messageBytes[i];
|
|||
|
|
Console.WriteLine(buffer[i]);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
Console.WriteLine(BitConverter.ToString(buffer));
|
|||
|
|
CallInvokeUIUpdate("UpdatertbMessage", "接收数据:" + BitConverter.ToString(buffer) + "\n");
|
|||
|
|
Console.WriteLine("测试节点==》CallInvokeUIUpdate");
|
|||
|
|
|
|||
|
|
if (canSend == false&&buffer[0] == 0x41 && buffer[1] == 0x35 && buffer[2] == 0x42 && buffer[3] == 0x35)
|
|||
|
|
{
|
|||
|
|
ParsingData(buffer);
|
|||
|
|
}
|
|||
|
|
else if(canSend)
|
|||
|
|
{
|
|||
|
|
//Console.WriteLine("开始接收图片!");
|
|||
|
|
//CallInvokeUIUpdate("UpdatertbMessage", "开始接收图片!\n");
|
|||
|
|
|
|||
|
|
FileStream wrtr; //文件读写类
|
|||
|
|
byte[] data;
|
|||
|
|
//int rect = _client_QXXWCX.Receive(data, 0, 4, 0); //用来接收图片字节流长度
|
|||
|
|
//int size = BitConverter.ToInt32(data, 0); //16进制转成int型
|
|||
|
|
int rect;
|
|||
|
|
int size = picLength;
|
|||
|
|
int dataleft = size - num;
|
|||
|
|
data = new byte[size]; //创建byte组
|
|||
|
|
|
|||
|
|
byte[] result = new byte[size];
|
|||
|
|
|
|||
|
|
//wrtr = new FileStream(Environment.CurrentDirectory + "\\picturefile\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt", FileMode.Create,FileAccess.Write); //创建新文件"new.jpg"
|
|||
|
|
|
|||
|
|
string ascii = Encoding.Default.GetString(buffer);
|
|||
|
|
//result[i] = buffer[i];
|
|||
|
|
Console.WriteLine(ascii);
|
|||
|
|
string CurDir = System.AppDomain.CurrentDomain.BaseDirectory + @"SaveDir\"; //设置当前目录
|
|||
|
|
if (!System.IO.Directory.Exists(CurDir)) System.IO.Directory.CreateDirectory(CurDir); //该路径不存在时,在当前文件目录下创建文件夹"导出.."
|
|||
|
|
|
|||
|
|
//不存在该文件时先创建
|
|||
|
|
string datetime = DateTime.Now.ToString("yyyyMMddHHmmss");
|
|||
|
|
//String filePath = CurDir + datetime + ".txt"; // 生成txt文件
|
|||
|
|
//测试文件 可以生成base64编码txt文件,能用转图片
|
|||
|
|
String filePath = CurDir + "base64" + ".txt"; // 生成txt文件
|
|||
|
|
System.IO.StreamWriter file1 = new System.IO.StreamWriter(filePath, true); //文件已覆盖方式添加内容
|
|||
|
|
|
|||
|
|
file1.Write(ascii); //保存数据到文件
|
|||
|
|
|
|||
|
|
file1.Close(); //关闭文件
|
|||
|
|
file1.Dispose(); //释放对象
|
|||
|
|
/*
|
|||
|
|
for (int i=0;i<num;i++)
|
|||
|
|
{
|
|||
|
|
string ascii = Encoding.Default.GetString(buffer);
|
|||
|
|
//result[i] = buffer[i];
|
|||
|
|
Console.WriteLine(ascii);
|
|||
|
|
string CurDir = System.AppDomain.CurrentDomain.BaseDirectory + @"SaveDir\"; //设置当前目录
|
|||
|
|
if (!System.IO.Directory.Exists(CurDir)) System.IO.Directory.CreateDirectory(CurDir); //该路径不存在时,在当前文件目录下创建文件夹"导出.."
|
|||
|
|
|
|||
|
|
//不存在该文件时先创建
|
|||
|
|
String filePath = CurDir + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt";
|
|||
|
|
System.IO.StreamWriter file1 = new System.IO.StreamWriter(filePath, false); //文件已覆盖方式添加内容
|
|||
|
|
|
|||
|
|
file1.Write(data); //保存数据到文件
|
|||
|
|
|
|||
|
|
file1.Close(); //关闭文件
|
|||
|
|
file1.Dispose(); //释放对象
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
int total = num;
|
|||
|
|
|
|||
|
|
CallInvokeUIUpdate("UpdatertbMessage", "total:" + total + "\n");
|
|||
|
|
CallInvokeUIUpdate("UpdatertbMessage", "size:" + size + "\n");
|
|||
|
|
while (total < size) //当接收长度小于总长度时继续执行
|
|||
|
|
{
|
|||
|
|
//
|
|||
|
|
rect = _client_QXXWCX.Receive(data, total, dataleft, 0); //接收字节流,receive方法返回int获取已接收字节个数,第一个参数是需要写入的字节组,第二个参数是起始位置,第三个参数是接收字节的长度
|
|||
|
|
|
|||
|
|
CallInvokeUIUpdate("UpdatertbMessage", "rect:" + rect + "\n");
|
|||
|
|
|
|||
|
|
int dataLength = 0;
|
|||
|
|
for(int i=total;i<total+rect;i++)
|
|||
|
|
{
|
|||
|
|
result[i] = data[dataLength];
|
|||
|
|
dataLength++;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
total += rect; //已接收个数-下一次从当前个数开始接收
|
|||
|
|
dataleft -= rect; //剩下的字节长度
|
|||
|
|
|
|||
|
|
CallInvokeUIUpdate("UpdatertbMessage", "total:" + total + "\n");
|
|||
|
|
CallInvokeUIUpdate("UpdatertbMessage", "dataleft:" + dataleft + "\n");
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
//Array.Reverse(data);
|
|||
|
|
string text = "";
|
|||
|
|
for (int i = 0; i < data.Length; i++) {
|
|||
|
|
text += data[i].ToString();
|
|||
|
|
}
|
|||
|
|
Console.WriteLine(text);
|
|||
|
|
//Console.WriteLine("text");
|
|||
|
|
//Console.WriteLine(text);
|
|||
|
|
//string text = Encoding.Unicode.GetString(data);
|
|||
|
|
System.IO.File.WriteAllText(@"C:\Users\LENOVO\Desktop\111.txt", text);
|
|||
|
|
|
|||
|
|
CallInvokeUIUpdate("UpdatertbMessage", "接收图片结束,开始存储图片!\n");
|
|||
|
|
|
|||
|
|
//byte[] totalData = new byte[buffer.Length + data.Length];
|
|||
|
|
////buffer.CopyTo(totalData, 0);
|
|||
|
|
////data.CopyTo(totalData, buffer.Length);
|
|||
|
|
|
|||
|
|
//Buffer.BlockCopy(buffer, 0, totalData, 0, buffer.Length);//这种方法仅适用于字节数组
|
|||
|
|
//Buffer.BlockCopy(data, 0, totalData, data.Length, data.Length);
|
|||
|
|
|
|||
|
|
|
|||
|
|
////string ascData = Encoding.ASCII.GetString(result); // 将整个ASCII类型的数组(byte[])解码为(string)
|
|||
|
|
//string ascData = BitConverter.ToString(result);
|
|||
|
|
//CallInvokeUIUpdate("UpdatertbMessage", "ascDataLength:"+ ascData.Length);
|
|||
|
|
//CallInvokeUIUpdate("UpdatertbMessage", "ascData:" + ascData);
|
|||
|
|
|
|||
|
|
////string hexData = HexToString(ascData);
|
|||
|
|
//byte[] finData = strToToHexByte(ascData);
|
|||
|
|
//CallInvokeUIUpdate("UpdatertbMessage", "2222\n");
|
|||
|
|
for(int i=0;i < size/2; i++)
|
|||
|
|
{
|
|||
|
|
byte b = System.Text.Encoding.ASCII.GetBytes(result[i].ToString()+ result[i+1].ToString())[0];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
wrtr.Write(data, 0, data.Length); //输出文件
|
|||
|
|
wrtr.Flush(); //强制输出
|
|||
|
|
wrtr.Close(); //关闭文件流对象
|
|||
|
|
|
|||
|
|
CallInvokeUIUpdate("UpdatertbMessage", "图片接收结束!\n");
|
|||
|
|
|
|||
|
|
canSend = false;
|
|||
|
|
|
|||
|
|
/*FileStream fs = new FileStream("E:\\BaiduNetdiskDownload\\图片\\logo.png", FileMode.Open, FileAccess.Read);
|
|||
|
|
byte[] byteArray = new byte[fs.Length];
|
|||
|
|
fs.Read(byteArray, 0, byteArray.Length);
|
|||
|
|
FileStream fw = new FileStream("E:\\BaiduNetdiskDownload\\QXXWCX2\\QXXWCX\\QXXWCX\\bin\\Debug\\picturefile\\test.png", FileMode.OpenOrCreate, FileAccess.Write);
|
|||
|
|
fw.Write(data, 0, data.Length);*/
|
|||
|
|
}
|
|||
|
|
//}
|
|||
|
|
|
|||
|
|
//CallInvokeUIUpdate("UpdatertbMessage", "开始接收图片!\n");
|
|||
|
|
|
|||
|
|
//FileStream wrtr; //文件读写类
|
|||
|
|
//byte[] data;
|
|||
|
|
////int rect = _client_QXXWCX.Receive(data, 0, 4, 0); //用来接收图片字节流长度
|
|||
|
|
////int size = BitConverter.ToInt32(data, 0); //16进制转成int型
|
|||
|
|
//int rect;
|
|||
|
|
//int size = picLength;
|
|||
|
|
//int dataleft = size;
|
|||
|
|
//data = new byte[size]; //创建byte组
|
|||
|
|
|
|||
|
|
////Directory.CreateDirectory(Environment.CurrentDirectory + "\\picturefile");
|
|||
|
|
|
|||
|
|
//wrtr = new FileStream(Environment.CurrentDirectory + "\\picturefile\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".png", FileMode.Create); //创建新文件"new.jpg"
|
|||
|
|
|
|||
|
|
//int total = 0;
|
|||
|
|
//while (total < size) //当接收长度小于总长度时继续执行
|
|||
|
|
//{
|
|||
|
|
// rect = _client_QXXWCX.Receive(data, total, dataleft, 0); //接收字节流,receive方法返回int获取已接收字节个数,第一个参数是需要写入的字节组,第二个参数是起始位置,第三个参数是接收字节的长度
|
|||
|
|
// CallInvokeUIUpdate("UpdatertbMessage", "rect:" + rect + "\n");
|
|||
|
|
// total += rect; //已接收个数-下一次从当前个数开始接收
|
|||
|
|
// dataleft -= rect; //剩下的字节长度
|
|||
|
|
//}
|
|||
|
|
|
|||
|
|
//string ascData = Encoding.ASCII.GetString(data); // 将整个ASCII类型的数组(byte[])解码为(string)
|
|||
|
|
// //Console.WriteLine(ascData);
|
|||
|
|
//string hexData = HexToString(ascData);
|
|||
|
|
////Console.WriteLine(hexData);
|
|||
|
|
|
|||
|
|
//byte[] finData = strToToHexByte(hexData);
|
|||
|
|
////Console.WriteLine(BitConverter.ToString(finData));
|
|||
|
|
|
|||
|
|
//wrtr.Write(finData, 0, finData.Length); //输出文件
|
|||
|
|
//wrtr.Flush(); //强制输出
|
|||
|
|
//wrtr.Close(); //关闭文件流对象
|
|||
|
|
|
|||
|
|
//canSend = false;
|
|||
|
|
|
|||
|
|
//Console.WriteLine("文件写入结束");
|
|||
|
|
//CallInvokeUIUpdate("UpdatertbMessage", "图片接收结束!\n");
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception err)
|
|||
|
|
{
|
|||
|
|
Console.WriteLine(err.Message);
|
|||
|
|
SendMsgSilent(err.Message);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void ParsingData(byte[] byteArray)
|
|||
|
|
{
|
|||
|
|
if (byteArray.Length == 32)
|
|||
|
|
{
|
|||
|
|
if (PasswordEquals(new byte[12] { byteArray[0], byteArray[1], byteArray[2], byteArray[3], byteArray[4], byteArray[5], byteArray[6], byteArray[7], byteArray[8], byteArray[9], byteArray[10], byteArray[11] }, new byte[12] { 0x41, 0x35, 0x42, 0x35, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 }))
|
|||
|
|
{
|
|||
|
|
//float him = float.Parse(HexToString(ByteToString(new byte[8] { byteArray[18], byteArray[19], byteArray[16], byteArray[17], byteArray[14], byteArray[15], byteArray[12], byteArray[13] })));
|
|||
|
|
//float err = float.Parse(HexToString(ByteToString(new byte[8] { byteArray[26], byteArray[27], byteArray[24], byteArray[25], byteArray[22], byteArray[23], byteArray[20], byteArray[21] })));
|
|||
|
|
short him = BitConverter.ToInt16(strToToHexByte(Encoding.ASCII.GetString(new byte[4] { byteArray[26], byteArray[27], byteArray[24], byteArray[25] })), 0);
|
|||
|
|
short err = BitConverter.ToInt16(strToToHexByte(Encoding.ASCII.GetString(new byte[4] { byteArray[22], byteArray[23], byteArray[20], byteArray[21] })), 0);
|
|||
|
|
CallInvokeUIUpdate("UpdatertbMessage", "接收到状态数据:\n温度:" + him / 10f + "\n湿度:" + err / 10f + "\n");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (PasswordEquals(byteArray, new byte[32] { 0x41, 0x35, 0x42, 0x35, 0x30, 0x32, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x46, 0x46, 0x46, 0x46 }))
|
|||
|
|
{
|
|||
|
|
//canSend = true;
|
|||
|
|
Console.WriteLine("采集图片成功");
|
|||
|
|
CallInvokeUIUpdate("UpdatertbMessage", "采集图片成功!\n");
|
|||
|
|
}
|
|||
|
|
else if (PasswordEquals(new byte[12] { byteArray[0], byteArray[1], byteArray[2], byteArray[3], byteArray[4], byteArray[5], byteArray[6], byteArray[7], byteArray[8], byteArray[9], byteArray[10], byteArray[11] }, new byte[12] { 0x41, 0x35, 0x42, 0x35, 0x30, 0x32, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 }))
|
|||
|
|
{
|
|||
|
|
Console.WriteLine("采集图片失败");
|
|||
|
|
CallInvokeUIUpdate("UpdatertbMessage", "采集图片失败!\n");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (PasswordEquals(new byte[20] { byteArray[0], byteArray[1], byteArray[2], byteArray[3], byteArray[4], byteArray[5], byteArray[6], byteArray[7], byteArray[8], byteArray[9], byteArray[10], byteArray[11], byteArray[12], byteArray[13], byteArray[14], byteArray[15], byteArray[16], byteArray[17], byteArray[18], byteArray[19] },
|
|||
|
|
new byte[20] { 0x41, 0x35, 0x42, 0x35, 0x30, 0x33, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 }))
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
|
|||
|
|
canSend = true;
|
|||
|
|
Console.WriteLine("请求图片成功");
|
|||
|
|
Console.WriteLine("测试节点");
|
|||
|
|
//CallInvokeUIUpdate("UpdatertbMessage", "请求图片成功!\n");
|
|||
|
|
|
|||
|
|
picLength = BitConverter.ToInt32(strToToHexByte(Encoding.ASCII.GetString(new byte[8] { byteArray[26], byteArray[27], byteArray[24], byteArray[25], byteArray[22], byteArray[23], byteArray[20], byteArray[21] })), 0); //ASCII的长度
|
|||
|
|
//picLength = BitConverter.ToInt32(strToToHexByte(Encoding.ASCII.GetString(new byte[8] { byteArray[20], byteArray[21], byteArray[22], byteArray[23], byteArray[24], byteArray[25], byteArray[26], byteArray[27] })), 0);
|
|||
|
|
//Console.WriteLine(picLength);
|
|||
|
|
//CallInvokeUIUpdate("UpdatertbMessage", "图片长度:" + picLength + "\n");
|
|||
|
|
|
|||
|
|
//th_FIle = new Thread(ReceiveFile);
|
|||
|
|
//th_FIle.IsBackground = true;
|
|||
|
|
//th_FIle.Start();
|
|||
|
|
|
|||
|
|
//th_QXXWCX.Suspend();
|
|||
|
|
}
|
|||
|
|
else if (PasswordEquals(new byte[12] { byteArray[0], byteArray[1], byteArray[2], byteArray[3], byteArray[4], byteArray[5], byteArray[6], byteArray[7], byteArray[8], byteArray[9], byteArray[10], byteArray[11] }, new byte[12] { 0x41, 0x35, 0x42, 0x35, 0x30, 0x33, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 }))
|
|||
|
|
{
|
|||
|
|
canSend = false;
|
|||
|
|
Console.WriteLine("请求图片失败");
|
|||
|
|
CallInvokeUIUpdate("UpdatertbMessage", "请求图片失败!\n");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (PasswordEquals(byteArray, new byte[32] { 0x41, 0x35, 0x42, 0x35, 0x30, 0x34, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x46, 0x46, 0x46, 0x46 }))
|
|||
|
|
{
|
|||
|
|
Console.WriteLine("请求关机成功");
|
|||
|
|
CallInvokeUIUpdate("UpdatertbMessage", "请求关机成功!\n");
|
|||
|
|
}
|
|||
|
|
else if (PasswordEquals(new byte[12] { byteArray[0], byteArray[1], byteArray[2], byteArray[3], byteArray[4], byteArray[5], byteArray[6], byteArray[7], byteArray[8], byteArray[9], byteArray[10], byteArray[11] }, new byte[12] { 0x41, 0x35, 0x42, 0x35, 0x30, 0x34, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 }))
|
|||
|
|
{
|
|||
|
|
Console.WriteLine("请求关机失败");
|
|||
|
|
CallInvokeUIUpdate("UpdatertbMessage", "请求关机失败!\n");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
int picLength;
|
|||
|
|
|
|||
|
|
//[Obsolete]
|
|||
|
|
private void ReceiveFile()
|
|||
|
|
{
|
|||
|
|
CallInvokeUIUpdate("UpdatertbMessage", "开始接收图片!\n");
|
|||
|
|
|
|||
|
|
FileStream wrtr; //文件读写类
|
|||
|
|
byte[] data;
|
|||
|
|
//int rect = _client_QXXWCX.Receive(data, 0, 4, 0); //用来接收图片字节流长度
|
|||
|
|
//int size = BitConverter.ToInt32(data, 0); //16进制转成int型
|
|||
|
|
int rect;
|
|||
|
|
int size = picLength;
|
|||
|
|
int dataleft = size;
|
|||
|
|
data = new byte[size]; //创建byte组
|
|||
|
|
|
|||
|
|
Directory.CreateDirectory(Environment.CurrentDirectory + "\\picturefile");
|
|||
|
|
CallInvokeUIUpdate("UpdatertbMessage", "图片存储地址:"+ Environment.CurrentDirectory + "\n");
|
|||
|
|
wrtr = new FileStream(Environment.CurrentDirectory + "\\picturefile\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".png", FileMode.Create); //创建新文件"new.jpg"
|
|||
|
|
|
|||
|
|
int total = 0;
|
|||
|
|
while (total < size) //当接收长度小于总长度时继续执行
|
|||
|
|
{
|
|||
|
|
rect = _client_QXXWCX.Receive(data, total, dataleft, 0); //接收字节流,receive方法返回int获取已接收字节个数,第一个参数是需要写入的字节组,第二个参数是起始位置,第三个参数是接收字节的长度
|
|||
|
|
total += rect; //已接收个数-下一次从当前个数开始接收
|
|||
|
|
dataleft -= rect; //剩下的字节长度
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
string hexData = Encoding.ASCII.GetString(data); // 将整个ASCII类型的数组(byte[])解码为(string)
|
|||
|
|
byte[] finData = strToToHexByte(hexData);
|
|||
|
|
|
|||
|
|
wrtr.Write(finData, 0, finData.Length); //输出文件
|
|||
|
|
wrtr.Flush(); //强制输出
|
|||
|
|
wrtr.Close(); //关闭文件流对象
|
|||
|
|
|
|||
|
|
canSend = false;
|
|||
|
|
|
|||
|
|
CallInvokeUIUpdate("UpdatertbMessage", "图片接收完成!\n");
|
|||
|
|
|
|||
|
|
th_QXXWCX.Resume();
|
|||
|
|
th_FIle.Abort();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void btnStatueData_Click(object sender, EventArgs e)
|
|||
|
|
{
|
|||
|
|
SendMessage_MEMS(new byte[32] { 0x41, 0x35, 0x42, 0x35, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x46, 0x46, 0x46, 0x46 });
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void btnPicture_Click(object sender, EventArgs e)
|
|||
|
|
{
|
|||
|
|
SendMessage_MEMS(new byte[32] { 0x41, 0x35, 0x42, 0x35, 0x30, 0x32, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x46, 0x46, 0x46, 0x46 });
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void btnRequestPic_Click(object sender, EventArgs e)
|
|||
|
|
{
|
|||
|
|
//if (canSend == false)
|
|||
|
|
//{
|
|||
|
|
// Console.WriteLine("未采集图片或采集图片不成功!");
|
|||
|
|
// CallInvokeUIUpdate("UpdatertbMessage", "未采集图片或采集图片不成功!\n");
|
|||
|
|
// return;
|
|||
|
|
//}
|
|||
|
|
|
|||
|
|
|
|||
|
|
SendMessage_MEMS(new byte[32] { 0x41, 0x35, 0x42, 0x35, 0x30, 0x33, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x46, 0x46, 0x46, 0x46 });
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void btnTurnOff_Click(object sender, EventArgs e)
|
|||
|
|
{
|
|||
|
|
SendMessage_MEMS(new byte[32] { 0x41, 0x35, 0x42, 0x35, 0x30, 0x34, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x46, 0x46, 0x46, 0x46 });
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void btnClear_Click(object sender, EventArgs e)
|
|||
|
|
{
|
|||
|
|
rtbMessage.Clear();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void rtbMessage_TextChanged(object sender, EventArgs e)
|
|||
|
|
{
|
|||
|
|
rtbMessage.SelectionStart = rtbMessage.Text.Length; //Set the current caret position at the end
|
|||
|
|
rtbMessage.ScrollToCaret(); //Now scroll it automatically
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
public void CallInvokeUIUpdate(String code, string arg)
|
|||
|
|
{
|
|||
|
|
//创建一个委托,用于封装一个方法,在这里是封装了 控制更新控件 的方法
|
|||
|
|
Action<String, string> invokeAction = new Action<String, string>(CallInvokeUIUpdate);
|
|||
|
|
//判断操作控件的线程是否创建控件的线程
|
|||
|
|
//调用方调用方位于创建控件所在的线程以外的线程中,如果在其他线程则对控件进行方法调用时必须调用 Invoke 方法
|
|||
|
|
if (this.InvokeRequired)
|
|||
|
|
{
|
|||
|
|
//与调用线程不同的线程上创建(说明您必须通过 Invoke 方法对控件进行调用)
|
|||
|
|
this.Invoke(invokeAction, code, arg);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
//窗体线程,即主线程
|
|||
|
|
switch (code)
|
|||
|
|
{
|
|||
|
|
case "UpdatertbMessage":
|
|||
|
|
rtbMessage.AppendText(arg);
|
|||
|
|
break;
|
|||
|
|
|
|||
|
|
default:
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 16进制转普通字符串
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static string HexToString(string Hexdata)
|
|||
|
|
{
|
|||
|
|
string result = string.Empty;
|
|||
|
|
byte[] arrByte = new byte[Hexdata.Length / 2];
|
|||
|
|
int index = 0;
|
|||
|
|
for (int i = 0; i < Hexdata.Length; i += 2)
|
|||
|
|
{
|
|||
|
|
arrByte[index++] = Convert.ToByte(Hexdata.Substring(i, 2), 16);
|
|||
|
|
}
|
|||
|
|
result = System.Text.Encoding.UTF8.GetString(arrByte);
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static string ByteToString(byte[] bytes)
|
|||
|
|
{
|
|||
|
|
string length = BitConverter.ToString(bytes, 0, bytes.Length);
|
|||
|
|
string[] length1 = length.Split('-');
|
|||
|
|
string length2 = "";
|
|||
|
|
for (int i = 0; i < length1.Length; i++)
|
|||
|
|
{
|
|||
|
|
length2 += length1[i];
|
|||
|
|
}
|
|||
|
|
return length2;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
///
|
|||
|
|
/// 字符串转16进制字节数组
|
|||
|
|
///
|
|||
|
|
private static byte[] strToToHexByte(string hexString)
|
|||
|
|
{
|
|||
|
|
hexString = hexString.Replace(" ", "");
|
|||
|
|
if ((hexString.Length % 2) != 0)
|
|||
|
|
hexString += " ";
|
|||
|
|
byte[] returnBytes = new byte[hexString.Length / 2];
|
|||
|
|
for (int i = 0; i < returnBytes.Length; i++)
|
|||
|
|
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
|
|||
|
|
return returnBytes;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|