|
|
|
using AKS.EnterpriseLibrary.WebBrowser;
|
|
|
|
using AksWebBrowser;
|
|
|
|
using AKSWebBrowser.Commen;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
using System;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.IO;
|
|
|
|
using System.IO.Ports;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using System.Timers;
|
|
|
|
using Timer = System.Timers.Timer;
|
|
|
|
namespace AKSWebBrowser.Common
|
|
|
|
{
|
|
|
|
public class COMUtils
|
|
|
|
{
|
|
|
|
private static SerialPort serialPort = new SerialPort();
|
|
|
|
public string jsonstr = string.Empty;
|
|
|
|
public string jsontemp = string.Empty;
|
|
|
|
public int maxCHunkSize = 1024;
|
|
|
|
public string callback = string.Empty;
|
|
|
|
public string ml = "COM4";
|
|
|
|
public COMUtils()
|
|
|
|
{
|
|
|
|
OpenCOM();
|
|
|
|
}
|
|
|
|
|
|
|
|
//打开COM口
|
|
|
|
public void OpenCOM()
|
|
|
|
{
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
//要执行的Linux命令
|
|
|
|
string[] cmd = LinuxCmdArea("ls /dev");
|
|
|
|
if (cmd.Length > 0)
|
|
|
|
{
|
|
|
|
string parm = string.Empty;
|
|
|
|
foreach (string line in cmd)
|
|
|
|
{
|
|
|
|
if (line.Contains("ttyCH341USB"))
|
|
|
|
{
|
|
|
|
parm = line;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(parm))
|
|
|
|
{
|
|
|
|
ml = "/dev/" + parm;
|
|
|
|
//Log.Info("输出结果:" + ml);
|
|
|
|
//给管理权限
|
|
|
|
LinuxCmd(ml);
|
|
|
|
//打开串口
|
|
|
|
// 设置COM口,波特率,奇偶校验,数据位,停止位
|
|
|
|
serialPort.PortName = ml; // 请替换为你的串口名称
|
|
|
|
serialPort.BaudRate = 115200; // 设置波特率
|
|
|
|
serialPort.Parity = Parity.None;
|
|
|
|
serialPort.DataBits = 8;
|
|
|
|
serialPort.StopBits = StopBits.One;
|
|
|
|
serialPort.Handshake = Handshake.None;
|
|
|
|
serialPort.DtrEnable = true; //启用控制终端就续信号
|
|
|
|
//serialPort.ReadTimeout = 18000;
|
|
|
|
//serialPort.RtsEnable = true; //启用请求发送信号
|
|
|
|
serialPort.NewLine = "\n";
|
|
|
|
serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
|
|
|
|
if (!serialPort.IsOpen)
|
|
|
|
{
|
|
|
|
serialPort.Open();
|
|
|
|
}
|
|
|
|
Timer timer = new Timer(3000);//1秒钟的时间间隔
|
|
|
|
timer.Elapsed += OnTimedEvent;
|
|
|
|
timer.AutoReset = true;//重复执行
|
|
|
|
timer.Enabled = true;//启动定时器
|
|
|
|
//Log.Info("浏览器COM服务启动成功");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Log.Info("串口类型不匹配");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Log.Info("当前设备没有串口设备");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
//Log.Info("服务启动异常ex: " + ex.Message + "");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//接受数据
|
|
|
|
public static string bsid = string.Empty;
|
|
|
|
public static string bsext = string.Empty;
|
|
|
|
public static string bspath = string.Empty;
|
|
|
|
public static bool sfjswc = false;
|
|
|
|
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
SerialPort sp = (SerialPort)sender;
|
|
|
|
string _jsonstr = sp.ReadExisting();
|
|
|
|
if (!string.IsNullOrEmpty(_jsonstr))
|
|
|
|
{
|
|
|
|
if (_jsonstr.Contains("\n"))
|
|
|
|
{
|
|
|
|
jsonstr = jsontemp + _jsonstr;
|
|
|
|
//向js发送数据
|
|
|
|
//CShaseBJavaScript(jsonstr);
|
|
|
|
//jsontemp = string.Empty;
|
|
|
|
//jsonstr = string.Empty;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
jsontemp = jsontemp + _jsonstr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
//Log.Info("接受数据数据异常: " + ex.Message + "");
|
|
|
|
jsonstr = MainModel.str2Base64("{\"callback\":\"" + this.callback + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + "接受数据数据超时:" + ex.Message + "\"}");
|
|
|
|
// CShaseBJavaScript(jsonstr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//打开串口
|
|
|
|
private void OnTimedEvent(Object source, ElapsedEventArgs e)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (!serialPort.IsOpen)
|
|
|
|
{
|
|
|
|
serialPort.Open();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
//Log.Info("定时任务打开串口异常: " + ex.Message + "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//发送数据
|
|
|
|
public string SendData(string data, string callback)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
this.callback = callback;
|
|
|
|
if (serialPort.IsOpen)
|
|
|
|
{
|
|
|
|
jsontemp = string.Empty;
|
|
|
|
jsonstr = string.Empty;
|
|
|
|
//写入数据并以换行符结束
|
|
|
|
serialPort.WriteLine(data);
|
|
|
|
//Log.Info("发送数据成功: " + data + "");
|
|
|
|
while (string.IsNullOrEmpty(jsonstr))
|
|
|
|
{
|
|
|
|
Task.Delay(10).Wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//重新打开串口
|
|
|
|
OpenCOM();
|
|
|
|
if (serialPort.IsOpen)
|
|
|
|
{
|
|
|
|
jsontemp = string.Empty;
|
|
|
|
jsonstr = string.Empty;
|
|
|
|
//写入数据并以换行符结束
|
|
|
|
serialPort.WriteLine(data);
|
|
|
|
//Log.Info("发送数据成功: " + data + "");
|
|
|
|
while (string.IsNullOrEmpty(jsonstr))
|
|
|
|
{
|
|
|
|
Task.Delay(10).Wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
jsontemp = string.Empty;
|
|
|
|
jsonstr = string.Empty;
|
|
|
|
//Log.Info("发送数据失败");
|
|
|
|
jsonstr = MainModel.str2Base64("{\"callback\":\"" + this.callback + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + "串口未打开" + "\"}");
|
|
|
|
//CShaseBJavaScript(jsonstr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
jsontemp = string.Empty;
|
|
|
|
jsonstr = string.Empty;
|
|
|
|
//Log.Info("发送数据异常3: " + ex.Message + "");
|
|
|
|
jsonstr = MainModel.str2Base64("{\"callback\":\"" + this.callback + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + "发送数据异常:" + ex.Message + "\"}");
|
|
|
|
//CShaseBJavaScript(jsonstr);
|
|
|
|
}
|
|
|
|
return jsonstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
//关闭
|
|
|
|
public void ClosePort()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (serialPort.IsOpen)
|
|
|
|
{
|
|
|
|
serialPort.Close(); // 关闭串口
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
Log.Error("关闭异常: " + ex.Message + "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// COM接收文件
|
|
|
|
/// </summary>
|
|
|
|
public void NewMethod1(SerialPort sp, string path)
|
|
|
|
{
|
|
|
|
byte[] buffer = new byte[sp.ReadBufferSize];
|
|
|
|
int bytesRead = sp.Read(buffer, 0, buffer.Length);
|
|
|
|
using (FileStream fileStream = new FileStream(path, FileMode.Append))
|
|
|
|
{
|
|
|
|
fileStream.Write(buffer, 0, bytesRead);
|
|
|
|
fileStream.Close();
|
|
|
|
fileStream.Dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// COM口公共发送文件
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="url"></param>
|
|
|
|
public void NewMethod(string url, string id)
|
|
|
|
{
|
|
|
|
string ext = Path.GetExtension(url);
|
|
|
|
serialPort.WriteLine("Start_" + id + "_" + ext);
|
|
|
|
// 发送端
|
|
|
|
byte[] documentBytes = File.ReadAllBytes(url);
|
|
|
|
serialPort.Write(documentBytes, 0, documentBytes.Length);
|
|
|
|
serialPort.WriteLine("End");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//向js传输数据
|
|
|
|
public void CShaseBJavaScript(string param)
|
|
|
|
{
|
|
|
|
Task.Run(async () =>
|
|
|
|
{
|
|
|
|
param = MainModel.Base64str2(param);
|
|
|
|
//Log.Info("返回数据:" + param);
|
|
|
|
JObject jo = (JObject)JsonConvert.DeserializeObject(param);
|
|
|
|
bool fieldExists = jo.ContainsKey("callback");
|
|
|
|
if (fieldExists)
|
|
|
|
{
|
|
|
|
string callback = jo["callback"].ToString();
|
|
|
|
//Log.Info("回调js方法:" + callback);
|
|
|
|
string _parm = callback + "('" + param + "')";
|
|
|
|
await Parame.webBrowser.ExecuteJavaScript(_parm);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Log.Info("回调js方法为空");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
//执行命令
|
|
|
|
public void LinuxCmd(string command)
|
|
|
|
{
|
|
|
|
command = $"echo 'aks@123456' sudo -S chmod 777 {command}";
|
|
|
|
//Log.Info("执行命令:" + command);
|
|
|
|
// 启动进程
|
|
|
|
var process = new Process
|
|
|
|
{
|
|
|
|
StartInfo = new ProcessStartInfo
|
|
|
|
{
|
|
|
|
FileName = "/bin/bash",
|
|
|
|
Arguments = $"-c \"{command}\"",
|
|
|
|
UseShellExecute = false,
|
|
|
|
RedirectStandardOutput = true,
|
|
|
|
RedirectStandardError = true,
|
|
|
|
CreateNoWindow = true
|
|
|
|
}
|
|
|
|
};
|
|
|
|
process.Start();
|
|
|
|
string output = process.StandardOutput.ReadToEnd();
|
|
|
|
string error = process.StandardError.ReadToEnd();
|
|
|
|
process.WaitForExit();
|
|
|
|
//Log.Info("执行命令结果返回:" + output);
|
|
|
|
//Log.Info("执行命令错误结果返回:" + error);
|
|
|
|
}
|
|
|
|
|
|
|
|
//执行命令返回数组
|
|
|
|
public string[] LinuxCmdArea(string command)
|
|
|
|
{
|
|
|
|
// 使用ProcessStartInfo设置启动参数
|
|
|
|
ProcessStartInfo startInfo = new ProcessStartInfo
|
|
|
|
{
|
|
|
|
FileName = "/bin/bash", // 指定bash shell
|
|
|
|
Arguments = $"-c \"{command}\"", // 要执行的命令
|
|
|
|
RedirectStandardOutput = true, // 重定向标准输出
|
|
|
|
UseShellExecute = false, // 不使用系统外壳程序启动
|
|
|
|
CreateNoWindow = true // 不创建新窗口
|
|
|
|
};
|
|
|
|
|
|
|
|
// 启动进程
|
|
|
|
using (Process process = Process.Start(startInfo))
|
|
|
|
{
|
|
|
|
using (System.IO.StreamReader reader = process.StandardOutput)
|
|
|
|
{
|
|
|
|
string result = reader.ReadToEnd(); // 读取全部输出
|
|
|
|
return result.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); // 转换为字符串数组
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|