change-引入虚拟串口解决扫码枪条码分发多串口

master
liuwf 1 year ago
parent e2069a464f
commit e2ee22ff8e

@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace Khd.Core.Library.Extensions
{
public class NetworkHelper
{
/// <summary>
/// 获取本地计算机的所有 IPv4 地址。
/// </summary>
/// <returns>IPv4 地址列表。如果没有找到,则返回一个空列表。</returns>
public static List<string> GetLocalIPv4Addresses()
{
List<string> ipAddresses = new List<string>();
try
{
// 获取所有网络接口
var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (var networkInterface in networkInterfaces)
{
// 检查网络接口是否处于正常状态且不是回环接口
if (networkInterface.OperationalStatus == OperationalStatus.Up &&
networkInterface.NetworkInterfaceType != NetworkInterfaceType.Loopback)
{
// 获取该接口的IP属性
var ipProperties = networkInterface.GetIPProperties();
// 获取单播地址集合
var unicastAddresses = ipProperties.UnicastAddresses;
foreach (var address in unicastAddresses)
{
// 检查是否是 IPv4 地址
if (address.Address.AddressFamily == AddressFamily.InterNetwork)
{
// 获取 IPv4 地址并添加到列表
var ipAddress = address.Address.ToString();
if (!string.IsNullOrEmpty(ipAddress) && !ipAddress.StartsWith("169.254")) // 排除自动私有地址
{
ipAddresses.Add(ipAddress);
}
}
}
}
}
}
catch
{
Console.WriteLine("获取IP地址时发生错误");
}
return ipAddresses;
}
/// <summary>
/// 获取本地计算机的第一个有效的 IPv4 地址。
/// </summary>
/// <returns>第一个有效的 IPv4 地址,如果没有找到则返回 null。</returns>
public static string GetFirstLocalIPv4Address()
{
var ipAddresses = GetLocalIPv4Addresses();
return ipAddresses.Count > 0 ? ipAddresses[0] : null;
}
}
}

@ -4,6 +4,7 @@ using Khd.Core.Domain.Auth;
using Khd.Core.Domain.Dto.webapi; using Khd.Core.Domain.Dto.webapi;
using Khd.Core.Domain.Models; using Khd.Core.Domain.Models;
using Khd.Core.EntityFramework; using Khd.Core.EntityFramework;
using Khd.Core.Library.Extensions;
using Khd.Core.Library.Mapper; using Khd.Core.Library.Mapper;
using Khd.Core.Plc; using Khd.Core.Plc;
using Khd.Core.Plc.S7; using Khd.Core.Plc.S7;
@ -62,6 +63,7 @@ namespace Khd.Core.Wpf.Form
private object order_code; private object order_code;
private int UpState;//对应上件站点的状态,0为良好 1为损坏 private int UpState;//对应上件站点的状态,0为良好 1为损坏
private SerialPortModel serialPortModel; private SerialPortModel serialPortModel;
private SerialPortModel virtualSerialPortModel;
private SerialPortHelper serialPortHelper; private SerialPortHelper serialPortHelper;
List<BasePlcpoint> basePlcpoints = new List<BasePlcpoint>(); List<BasePlcpoint> basePlcpoints = new List<BasePlcpoint>();
object timerjilu; object timerjilu;
@ -245,17 +247,40 @@ namespace Khd.Core.Wpf.Form
try try
{ {
serialPortModel = new SerialPortModel(); string ip = NetworkHelper.GetFirstLocalIPv4Address();
serialPortModel.PortName = "COM1"; if ("192.168.2.26".Equals(ip))
serialPortModel.BaudRate = 9600; {
serialPortModel.DataBits = 8; // 获取所有可用的串口名称
serialPortModel.Parity = Parity.None; string[] portNames = SerialPort.GetPortNames();
serialPortModel.StopBits = StopBits.One; //虚拟串口
serialPortHelper = new SerialPortHelper(); virtualSerialPortModel = new SerialPortModel();
serialPortHelper.OpenMyConn(serialPortModel); virtualSerialPortModel = new SerialPortModel();
Thread scanThread = new Thread(ScanMessage); virtualSerialPortModel.PortName = "COM10";
scanThread.IsBackground = true; virtualSerialPortModel.BaudRate = 9600;
scanThread.Start(); virtualSerialPortModel.DataBits = 8;
virtualSerialPortModel.Parity = Parity.None;
virtualSerialPortModel.StopBits = StopBits.One;
serialPortHelper = new SerialPortHelper();
serialPortHelper.OpenVirtualMyConn(virtualSerialPortModel);
//扫码枪串口
serialPortModel = new SerialPortModel();
serialPortModel.PortName = "COM1";
serialPortModel.BaudRate = 9600;
serialPortModel.DataBits = 8;
serialPortModel.Parity = Parity.None;
serialPortModel.StopBits = StopBits.One;
// serialPortHelper = new SerialPortHelper();
serialPortHelper.OpenMyConn(serialPortModel);
Thread scanThread = new Thread(ScanMessage);
scanThread.IsBackground = true;
scanThread.Start();
}
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -342,6 +367,8 @@ namespace Khd.Core.Wpf.Form
} }
private void TaskOrderTimer(object? obj) private void TaskOrderTimer(object? obj)
{ {
while (true) while (true)

@ -20,6 +20,7 @@ namespace Khd.Core.Wpf.Scan
//缓冲信息 //缓冲信息
public string ReceiveData = ""; public string ReceiveData = "";
private SerialPort myCom = new SerialPort();//串口对象 private SerialPort myCom = new SerialPort();//串口对象
private SerialPort virtualCom = new SerialPort();//虚拟串口对象
/// <summary> /// <summary>
/// 串口属性 /// 串口属性
/// </summary> /// </summary>
@ -35,6 +36,22 @@ namespace Khd.Core.Wpf.Scan
} }
} }
/// <summary>
/// 串口属性
/// </summary>
public SerialPort VirtualCom
{
get
{
return virtualCom;
}
set
{
virtualCom = value;
}
}
private byte myReceiveByte = 0;//接收字节 private byte myReceiveByte = 0;//接收字节
private byte[] bData = new byte[1024];//接收的字节数组 private byte[] bData = new byte[1024];//接收的字节数组
private int index = 0; private int index = 0;
@ -58,6 +75,22 @@ namespace Khd.Core.Wpf.Scan
myCom.DataReceived += MyCom_DataReceived;//串口接收数据事件 myCom.DataReceived += MyCom_DataReceived;//串口接收数据事件
myCom.Open();//打开串口 myCom.Open();//打开串口
} }
public void OpenVirtualMyConn(SerialPortModel entiry)
{
if (virtualCom.IsOpen)
{
virtualCom.Close();//关闭
}
virtualCom.PortName = entiry.PortName;//串口名称
virtualCom.BaudRate = entiry.BaudRate;//波特率
virtualCom.DataBits = entiry.DataBits;//数据位
virtualCom.StopBits = entiry.StopBits;//停止位
virtualCom.Parity = entiry.Parity;//校验位
virtualCom.ReceivedBytesThreshold = 1;//设置串口缓冲区的字节数
virtualCom.DataReceived += virtualCom_DataReceived;//串口接收数据事件
virtualCom.Open();//打开串口
}
/// <summary> /// <summary>
/// 串口接收事件 /// 串口接收事件
/// </summary> /// </summary>
@ -70,10 +103,13 @@ namespace Khd.Core.Wpf.Scan
//读取串口缓冲区的字节数据,分两次传递结果 //读取串口缓冲区的字节数据,分两次传递结果
byte[] result = new byte[MyCom.BytesToRead]; byte[] result = new byte[MyCom.BytesToRead];
MyCom.Read(result, 0, MyCom.BytesToRead); MyCom.Read(result, 0, MyCom.BytesToRead);
// 将读取到的数据写入virtualCom
VirtualCom.Write(result, 0, result.Length);
ReceiveData += Encoding.UTF8.GetString(result); ReceiveData += Encoding.UTF8.GetString(result);
if (ReceiveData.Contains('\r')) if (ReceiveData.Contains('\r'))
{ {
message = ReceiveData.Replace("\r", "").Trim(); message = ReceiveData.Replace("\r", "").Trim();
SystemData.message = message; SystemData.message = message;
ReceiveData = ""; ReceiveData = "";
} }
@ -86,6 +122,24 @@ namespace Khd.Core.Wpf.Scan
} }
private void virtualCom_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
//读取串口缓冲区的字节数据,分两次传递结果
byte[] result = new byte[VirtualCom.BytesToRead];
VirtualCom.Read(result, 0, VirtualCom.BytesToRead);
}
catch (Exception ex)
{
LogManager.Error(ex);
}
}
/// <summary> /// <summary>
/// 关闭串口 /// 关闭串口
/// </summary> /// </summary>

Loading…
Cancel
Save