using System;
using System.IO.Ports;
namespace Nathan_Soft
{
//直流电源IT6800PC控制软件 专用类
public class It6800_Command {
public static string pc_remote = "AA 09 20 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 D4";
public static string local_control = "AA 09 20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 D3";
public static string Set_5V = "AA 09 23 88 13 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 71";
public static string Set_12V = "AA 09 23 E0 2E 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 E4";
}
public class Serial_It6800
{
byte[] command = new byte[26];
SerialPort serialPort;
public Serial_It6800() {
}
public string Portname;
public Int16 BaudRate = 9600;
private void command_set_zero() {
command = new byte[26];
for (int i = 0; i < 26; i++) command[i] = 0;
}
public string set_remote_control() {
command_set_zero();
serialPort = Get_SerialPort();
command[0] = 0xAA;
command[1] = Convert.ToByte(config_json.SerialPort_Addr);
command[2] = 0x20;
command[3] = 0x01;
command[25] = gen_check();
return SendCommandToIT6800();
}
public string send_command(string command)
{
serialPort = Get_SerialPort();
try
{
serialPort.Open();
try
{
serialPort.Write(HexToByte(command), 0, 26);
System.Threading.Thread.Sleep(1000);
serialPort.Close();
}
catch (Exception ex){
serialPort.Close();
return ex.Message;
}
return "Portname:" + serialPort.PortName + " Baudrate:" + serialPort.BaudRate + " DataBits:" + serialPort.DataBits + " Stopbit:" + serialPort.StopBits + " Parity:" + serialPort.Parity
+ "\r\n command:" + command+"\r\n";
}
catch (Exception ex)
{
return ex.Message;
}
}
/// <summary>
/// HexToByte
/// </summary>
private byte[] HexToByte(string msg)
{
msg = msg.Replace(" ", "");
byte[] comBuffer = new byte[msg.Length / 2];
for (int i = 0; i < ms