using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Model
{
public class GetTrace
{
///
/// 电压
///
public string Voltage { get; set; }
///
/// 电流
///
public string Electric { get; set; }
///
/// 机器人功率
///
public string TableRobotTrace { get; set; }
///
/// 真空1
///
public string Nozzle1Trace { get; set; }
///
/// 真空2
///
public string Nozzle2Trace { get; set; }
SerialPort SerialPort;
public GetTrace(string portName)
{
SerialPort = new System.IO.Ports.SerialPort();
SerialPort.PortName = portName;//"COM35";
SerialPort.BaudRate = 9600;//BaudRate;
SerialPort.DataBits = 8;//DataBits;
SerialPort.Parity = System.IO.Ports.Parity.None;//parity;
SerialPort.StopBits = System.IO.Ports.StopBits.One;//stopbit;
SerialPort.DataReceived += comPort_DataReceived;
}
public string ReadData;
byte[] arrMsgRec = new byte[21];
byte[] GetByte = new byte[2];
private void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
Thread.Sleep(100);
int length = SerialPort.Read(arrMsgRec, 0, 21);
ReadData = BitConverter.ToString(arrMsgRec);
string[] ReadMonitor = ReadData.Split('-');
for (int Index = 3; Index < 12; Index++)
{
if (Index % 2 == 1)
{
if (Index == 5)
{
AI[(Index - 3) / 2] = (HexToTen(ReadMonitor[Index] + ReadMonitor[Index + 1]) / 18.613).ToString("0.000");
}
else if (Index == 3)
{
AI[(Index - 3) / 2] = (HexToTen(ReadMonitor[Index] + ReadMonitor[Index + 1])).ToString("0.000");
}
else if (Index == 7)
{
AI[(Index - 3) / 2] = (HexToTen(ReadMonitor[Index] + ReadMonitor[Index + 1]) / 40.9).ToString("0.000");
}
else if (Index == 9)
{
if (HexToTen(ReadMonitor[Index] + ReadMonitor[Index + 1]) > 47)
{
AI[3] = "-0.4";
}
else if (HexToTen(ReadMonitor[Index] + ReadMonitor[Index + 1]) < 47 && HexToTen(ReadMonitor[Index] + ReadMonitor[Index + 1]) > 44)
{
AI[3] = "-12.4";
}
else
{
AI[3] = "-91.7";
}
}
else if (Index == 11)
{
AI[(Index - 3) / 2] = ((HexToTen(ReadMonitor[Index] + ReadMonitor[Index + 1])) / 204.8).ToString("0.000");
}
}
}
Voltage = AI[1] + "V";
Electric = AI[0] + "mA";
TableRobotTrace = ((double.Parse(AI[2])) / 0.72).ToString("0.00") + "MPa";
Nozzle1Trace = AI[3] + "kPa";
Nozzle2Trace = ((double.Parse(AI[4]) - 3) / 0.031).ToString("0.00") + "kPa";
}
catch
{
}
}
public bool Open()
{
try
{
if (SerialPort.IsOpen)
{
SerialPort.Close();
SerialPort.Open();
return true;
}
else
{
SerialPort.Open();
return true;
}
}
catch (Exception ex)
{
ex.ToString();
return false;
}
}
public void WriteData()
{
try
{
ReadData = "";
string SendMsg = ToModbusCRC16("01 04 00 00 00 08");//00 00 00 00 00 06
SendMsg = "010400000008" + SendMsg;
byte[] SendByte = StringToHexByte(SendMsg);
SerialPort.DiscardInBuffer();
SerialPort.Write(SendByte, 0, SendByte.Length);
}
catch (Exception ex)
{
ex.ToString();
}
}
string[] AI = new string[5];
#region 16 -> 10
private double HexToTen(string str)
{
double Result = 0;
if (str.Substring(0, 1) == "A")
{ Result = Result + 4096 * 10; }
else if (str.Substring(0, 1) == "B")
{ Result = Result + 4096 * 11; }
else if (str.Substring(0, 1) == "C")
{ Result = Result + 4096 * 12; }
else if (str.Substring(0, 1) == "D")
{ Result = Result + 4096 * 13; }
else if (str.Substring(0, 1) == "E")
{ Result = Result + 4096 * 14; }
else if (str.Substring(0, 1) == "F")
{ Result = Result + 4096 * 15; }
else
{ Result = Result + 4096 * double.Parse(str.Substring(0, 1)); }
if (str.Substring(1, 1) == "A")
{ Result = Result + 256 * 10; }
else if (str.Substring(1, 1) == "B")
{ Result = Result + 256 * 11; }
else if (str.Substring(1, 1) == "C")
{ Result = Result + 256 * 12; }
else if (str.Substring(1, 1) == "D")
{ Result = Result + 256 * 13; }
else if (str.Substring(1, 1) == "E")
{ Result = Result + 256 * 14; }
else if (str.Substring(1, 1) == "F")
{ Result = Result + 256 * 15; }
else
{ Result = Result + 256 * double.Parse(str.Substring(1, 1)); }
if (str.Substring(2, 1) == "A")
{ Result = Result + 16 * 10; }
else if (str.Substring(2, 1) == "B")
{ Result = Result + 16 * 11; }
else if (str.Substring(2, 1) == "C")
{ Result = Result + 16 * 12; }
else if (str.Substring(2, 1) == "D")
{ Result = Result + 16 * 13; }
else if (str.Substring(2, 1) == "E")
{ Result = Result + 16 * 14; }
else if (str.Substring(2, 1) == "F")
{ Result = Result + 16 * 15; }
else
{ Result = Result + 16 * double.Parse(str.Substring(2, 1)); }
if (str.Substring(3, 1) == "A")
{ Result = Result + 1 * 10; }
else if (str.Substring(3, 1) == "B")
{ Result = Result + 1 * 11; }
else if (str.Substring(3, 1) == "C")
{ Result = Result + 1 * 12; }
else if (str.Substring(3, 1) == "D")
{ Result = Result + 1 * 13; }
else if (str.Substring(3, 1) == "E")
{ Result = Result + 1 * 14; }
else if (str.Substring(3, 1) == "F")
{ Result = Result + 1 * 15; }
else
{ Result = Result + 1 * double.Parse(str.Substring(3, 1)); }
return Result;
}
#endregion
#region CRC16
public static byte[] CRC16(byte[] data)
{
int len = data.Length;
if (len > 0)
{
ushort crc = 0xFFFF;
for (int i = 0; i < len; i++)
{
crc = (ushort)(crc ^ (data[i]));
for (int j = 0; j < 8; j++)
{
crc = (crc & 1) != 0 ? (ushort)((crc >> 1) ^ 0xA001) : (ushort)(crc >> 1);
}
}
byte hi = (byte)((crc & 0xFF00) >> 8); //高位置
byte lo = (byte)(crc & 0x00FF); //低位置
return new byte[] { hi, lo };
}
return new byte[] { 0, 0 };
}
#endregion
#region ToCRC16
public static string ToCRC16(string content)
{
return ToCRC16(content, Encoding.UTF8);
}
public static string ToCRC16(string content, bool isReverse)
{
return ToCRC16(content, Encoding.UTF8, isReverse);
}
public static string ToCRC16(string content, Encoding encoding)
{
return ByteToString(CRC16(encoding.GetBytes(content)), true);
}
public static string ToCRC16(string content, Encoding encoding, bool isReverse)
{
return ByteToString(CRC16(encoding.GetBytes(content)), isReverse);
}
public static string ToCRC16(byte[] data)
{
return ByteToString(CRC16(data), true);
}
public static string ToCRC16(byte[] data, bool isReverse)
{
return ByteToString(CRC16(data), isReverse);
}
#endregion
#region ToModbusCRC16
public static string ToModbusCRC16(string s)
{
return ToModbusCRC16(s, true);
}
public static string ToModbusCRC16(string s, bool isReverse)
{
return ByteToString(CRC16(StringToHexByte(s)), isReverse);
}
public static string ToModbusCRC16(byte[] data)
{
return ToModbusCRC16(data, true);
}
public static string ToModbusCRC16(byte[] data, bool isReverse)
{
return ByteToString(CRC16(data), isReverse);
}
#endregion
#region ByteToString
public static string ByteToString(byte[] arr, bool isReverse)
{
try
{
byte hi = arr[0], lo = arr[1];
return Convert.ToString(isReverse ? hi + lo * 0x100 : hi * 0x100 + lo, 16).ToUpper().PadLeft(4, '0');
}
catch (Exception ex) { throw (ex); }
}
public static string ByteToString(byte[] arr)
{
try
{
return ByteToString(arr, true);
}
catch (Exception ex) { throw (ex); }
}
#endregion
#region StringToHexString
public static string StringToHexString(string str)
{
StringBuilder s = new StringBuilder();
foreach (short c in str.ToCharArray())
{
s.Append(c.ToString("X4"));
}
return s.ToString();
}
#endregion
#region StringToHexByte
private static string ConvertChinese(string str)
{
StringBuilder s = new StringBuilder();
foreach (short c in str.ToCharArray())
{
if (c <= 0 || c >= 127)
{
s.Append(c.ToString("X4"));
}
else
{
s.Append((char)c);
}
}
return s.ToString();
}
private static string FilterChinese(string str)
{
StringBuilder s = new StringBuilder();
foreach (short c in str.ToCharArray())
{
if (c > 0 && c < 127)
{
s.Append((char)c);
}
}
return s.ToString();
}
///
/// 字符串转16进制字符数组
///
///
///
public static byte[] StringToHexByte(string str)
{
return StringToHexByte(str, false);
}
///
/// 字符串转16进制字符数组
///
///
/// 是否过滤掉中文字符
///
public static byte[] StringToHexByte(string str, bool isFilterChinese)
{
string hex = isFilterChinese ? FilterChinese(str) : ConvertChinese(str);
//清除所有空格
hex = hex.Replace(" ", "");
//若字符个数为奇数,补一个0
hex += hex.Length % 2 != 0 ? "0" : "";
byte[] result = new byte[hex.Length / 2];
for (int i = 0, c = result.Length; i < c; i++)
{
result[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
}
return result;
}
#endregion
}
}