You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
174 lines
5.2 KiB
174 lines
5.2 KiB
1 year ago
|
using AKS.EnterpriseLibrary.WebBrowser;
|
||
|
using AKSWebBrowser.Commen;
|
||
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.IO.Ports;
|
||
|
using System.Linq;
|
||
|
using System.Text;
|
||
|
using System.Threading;
|
||
|
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 COMUtils()
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
|
||
|
if (!serialPort.IsOpen)
|
||
|
{
|
||
|
OpenCOM("/dev/ttyCH341USB0");
|
||
|
}
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
Log.Info("服务启动异常ex: " + ex.Message + "");
|
||
|
if (!serialPort.IsOpen)
|
||
|
{
|
||
|
OpenCOM("/dev/ttyCH341USB1");
|
||
|
}
|
||
|
}
|
||
|
catch (Exception ex1)
|
||
|
{
|
||
|
Log.Info("服务启动异常ex1: " + ex1.Message + "");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//打开COM口
|
||
|
public void OpenCOM(string comName)
|
||
|
{
|
||
|
// 设置COM口,波特率,奇偶校验,数据位,停止位
|
||
|
serialPort.PortName = comName; // 请替换为你的串口名称
|
||
|
serialPort.BaudRate = 115200; // 设置波特率
|
||
|
serialPort.Parity = Parity.None;
|
||
|
serialPort.DataBits = 8;
|
||
|
serialPort.StopBits = StopBits.One;
|
||
|
serialPort.Handshake = Handshake.None;
|
||
|
serialPort.DtrEnable = true; //启用控制终端就续信号
|
||
|
serialPort.ReadTimeout = 50000;
|
||
|
//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服务启动成功");
|
||
|
}
|
||
|
|
||
|
//接受数据
|
||
|
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;
|
||
|
jsontemp = string.Empty;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
jsontemp = jsontemp + _jsonstr;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
Log.Error("接受数据数据异常: " + ex.Message + "");
|
||
|
jsonstr= MainModel.str2Base64("{\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" +"接受数据异常:"+ ex.Message + "\"}");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 处理接收到的数据
|
||
|
private void ProcessReceivedData(byte[] data)
|
||
|
{
|
||
|
|
||
|
// 例如,打印出每个字节的值
|
||
|
foreach (byte b in data)
|
||
|
{
|
||
|
Log.Info("串口接收byte数据:" + b.ToString());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//打开串口
|
||
|
private void OnTimedEvent(Object source, ElapsedEventArgs e)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
if (!serialPort.IsOpen)
|
||
|
{
|
||
|
serialPort.Open();
|
||
|
}
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
Log.Error("打开串口异常: " + ex.Message + "");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//发送数据
|
||
|
public string SendData(string data)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
if (serialPort.IsOpen)
|
||
|
{
|
||
|
jsonstr = string.Empty;
|
||
|
jsontemp = string.Empty;
|
||
|
//写入数据并以换行符结束
|
||
|
serialPort.WriteLine(data);
|
||
|
while (string.IsNullOrEmpty(jsonstr))
|
||
|
{
|
||
|
Task.Delay(10).Wait();
|
||
|
}
|
||
|
return jsonstr;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return "";
|
||
|
}
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
Log.Error("发送数据异常3: " + ex.Message + "");
|
||
|
return "";
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
//关闭
|
||
|
public void ClosePort()
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
if (serialPort.IsOpen)
|
||
|
{
|
||
|
serialPort.Close(); // 关闭串口
|
||
|
}
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
Log.Error("关闭异常: " + ex.Message + "");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|