146 lines
4.2 KiB
C#
146 lines
4.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
//using System.Windows;
|
|
//using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace WPFSerialAssistant
|
|
{
|
|
public static class Utilities
|
|
{
|
|
public static double m_Resolution = 1; //分辨率设置 0.1um
|
|
public static string BytesToText(List<byte> bytesBuffer, ReceiveMode mode, Encoding encoding)
|
|
{
|
|
string result = "";
|
|
|
|
{
|
|
switch (mode)
|
|
{
|
|
case ReceiveMode.Character: //
|
|
return encoding.GetString(bytesBuffer.ToArray<byte>());
|
|
|
|
case ReceiveMode.Hex: //十六进制
|
|
int totalBytes = bytesBuffer.ToArray<byte>().Length;
|
|
byte[] pointByteArray = bytesBuffer.ToArray<byte>();
|
|
|
|
result += "\n当前字节数: " + totalBytes.ToString() + ", 取整后点数: " + Math.Floor((double)totalBytes / 12) + "\n";
|
|
|
|
if (bytesBuffer.Count() == 2)
|
|
{
|
|
return "\n" +encoding.GetString(bytesBuffer.ToArray<byte>()) + "\n";
|
|
}
|
|
else if (bytesBuffer.Count() % 12 != 0)
|
|
{
|
|
foreach (var item in bytesBuffer)
|
|
{
|
|
result += Convert.ToString(item, 16).ToUpper() + " "; //普通显示
|
|
}
|
|
}
|
|
else //当返回字节数为 12倍数,认为XYZ坐标点
|
|
{
|
|
|
|
for (int i=0; i< Math.Floor((double)totalBytes/12); i++) //获得总点数
|
|
{
|
|
//每12个点一解析,循环
|
|
int tempX2 = pointByteArray[12 * i + 0] | pointByteArray[12 * i + 1] << 8 | pointByteArray[12 * i + 2] << 16 | pointByteArray[12 * i + 3] << 24;
|
|
int tempY2 = pointByteArray[12 * i + 4] | pointByteArray[12 * i + 5] << 8 | pointByteArray[12 * i + 6] << 16 | pointByteArray[12 * i + 7] << 24;
|
|
int tempZ2 = pointByteArray[12 * i + 8] | pointByteArray[12 * i + 9] << 8 | pointByteArray[12 * i + 10] << 16 | pointByteArray[12 * i + 11] << 24;
|
|
result += "Point: " + (i + 1).ToString()+"\t X: " + (tempX2 * m_Resolution).ToString() + "\t Y: " + (tempY2 * m_Resolution).ToString() + "\t Z: " + (tempZ2 * m_Resolution).ToString() + "\n";
|
|
}
|
|
}
|
|
|
|
break;
|
|
case ReceiveMode.Decimal: //十进制
|
|
foreach (var item in bytesBuffer)
|
|
{
|
|
result += Convert.ToString(item, 10) + " ";
|
|
}
|
|
break;
|
|
case ReceiveMode.Octal: //八进制
|
|
foreach (var item in bytesBuffer)
|
|
{
|
|
result += Convert.ToString(item, 8) + " ";
|
|
}
|
|
break;
|
|
case ReceiveMode.Binary: //二进制
|
|
|
|
foreach (var item in bytesBuffer)
|
|
{
|
|
result += Convert.ToString(item, 2) + " ";
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
public static string ToSpecifiedText(string text, SendMode mode, Encoding encoding)
|
|
{
|
|
string result = "";
|
|
switch (mode)
|
|
{
|
|
case SendMode.Character:
|
|
text = text.Trim();
|
|
|
|
// 转换成字节
|
|
List<byte> src = new List<byte>();
|
|
|
|
string[] grp = text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
foreach (var item in grp)
|
|
{
|
|
src.Add(Convert.ToByte(item, 16));
|
|
}
|
|
|
|
// 转换成字符串
|
|
result = encoding.GetString(src.ToArray<byte>());
|
|
break;
|
|
|
|
case SendMode.Hex:
|
|
|
|
byte[] byteStr = encoding.GetBytes(text.ToCharArray());
|
|
|
|
foreach (var item in byteStr)
|
|
{
|
|
result += Convert.ToString(item, 16).ToUpper() + " ";
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return result.Trim();
|
|
}
|
|
|
|
public static void sleep(int milliSecond)
|
|
{
|
|
int start = Environment.TickCount;
|
|
while (Math.Abs(Environment.TickCount - start) < milliSecond)
|
|
{
|
|
Application.DoEvents();
|
|
}
|
|
}
|
|
|
|
public static bool Delay(int delayTime)
|
|
{
|
|
DateTime now = DateTime.Now;
|
|
int s;
|
|
do
|
|
{
|
|
TimeSpan spand = DateTime.Now - now;
|
|
s = spand.Seconds;
|
|
Application.DoEvents();
|
|
}
|
|
while (s < delayTime);
|
|
return true;
|
|
}
|
|
|
|
}
|
|
}
|