add-添加光电信号

master
liuwf 4 months ago
parent 17eafa3366
commit 1b8ae60f2b

@ -13,10 +13,12 @@ using System.Diagnostics;
using System.IO; using System.IO;
using System.Security.Cryptography.Xml; using System.Security.Cryptography.Xml;
using System.ServiceModel.Channels; using System.ServiceModel.Channels;
using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Timers; using System.Timers;
using TouchSocket.Core; using TouchSocket.Core;
using TouchSocket.Sockets;
namespace SlnMesnac.Business namespace SlnMesnac.Business
{ {
@ -69,6 +71,7 @@ namespace SlnMesnac.Business
tcpServer = _tcpServer; tcpServer = _tcpServer;
TcpServer.RefreshMaterialCodeStrEvent += BarCodeHandler; TcpServer.RefreshMaterialCodeStrEvent += BarCodeHandler;
TcpServer.CameraResultEvent += ReceiveCameraResult; TcpServer.CameraResultEvent += ReceiveCameraResult;
TcpServer.SerialSignalPushEvent += SerialSignalPush;
this.baseMaterialService = baseMaterialService; this.baseMaterialService = baseMaterialService;
this.logoIdentifyService = logoIdentifyService; this.logoIdentifyService = logoIdentifyService;
this.logoConfigService = logoConfigService; this.logoConfigService = logoConfigService;
@ -80,6 +83,43 @@ namespace SlnMesnac.Business
CleanOldLogs(PicturePath); CleanOldLogs(PicturePath);
CleanOldLogs(LogPath); CleanOldLogs(LogPath);
InitClearTimer(); InitClearTimer();
test();
}
public void test()
{
Task.Run(async() =>
{
Thread.Sleep(5000);
TcpClient _tcpClient = new TcpClient();
_tcpClient.Setup(new TouchSocketConfig().SetRemoteIPHost($"127.0.0.1:7024"));
_tcpClient.Connect();
var waitClient = _tcpClient.CreateWaitingClient(new WaitingOptions()
{
FilterFunc = response =>
{
return true;
}
});
byte[] release = new byte[] { (byte)0x01, (byte)0x03, (byte)0x02, (byte)0x01, (byte)0xFF, (byte)0xF9, (byte)0x94 };
var reciveBuffer =await waitClient.SendThenResponseAsync(release);
});
}
private void SerialSignalPush(string ip, int flag)
{
// 光电触发拍照
if (flag == 1)
{
}
else //光电释放
{
}
} }
private void ReceiveCameraResult(string result) private void ReceiveCameraResult(string result)

@ -53,6 +53,15 @@ namespace SlnMesnac.Config
set { iniHelper.IniWriteValue("system", "CameraIP", value); } set { iniHelper.IniWriteValue("system", "CameraIP", value); }
} }
/// <summary>
/// 串口模块--IP
/// </summary>
public string SerialIP
{
get { return iniHelper.IniReadValue("system", "SerialIP"); }
set { iniHelper.IniWriteValue("system", "SerialIP", value); }
}
/// <summary> /// <summary>
/// 海康相机拍照存放路径 /// 海康相机拍照存放路径
/// </summary> /// </summary>

@ -32,10 +32,15 @@ namespace SlnMesnac.TouchSocket
public delegate void RefreshMaterialCodeStr(string materialCodeStr, string ip); public delegate void RefreshMaterialCodeStr(string materialCodeStr, string ip);
public static event RefreshMaterialCodeStr RefreshMaterialCodeStrEvent; public static event RefreshMaterialCodeStr RefreshMaterialCodeStrEvent;
//相机拍照识别结果事件 //相机拍照识别结果
public delegate void CameraResult(string result); public delegate void CameraResult(string result);
public static event CameraResult CameraResultEvent; public static event CameraResult CameraResultEvent;
//光电信号推送
public delegate void SerialSignalPush(string ip, int flag);
public static event SerialSignalPush SerialSignalPushEvent;
#endregion #endregion
public TcpServer(ILogger<TcpServer> logger,TcpService tcpService) public TcpServer(ILogger<TcpServer> logger,TcpService tcpService)
@ -67,42 +72,66 @@ namespace SlnMesnac.TouchSocket
{ {
try try
{ {
//从客户端收到信息
var mes = Encoding.UTF8.GetString(e.ByteBlock.Buffer, 0, e.ByteBlock.Len);//注意数据长度是byteBlock.Len // 串口模块光电信号
if (client.IP == config.SerialIP)
//心跳包
if (mes == "heartbeat")
{
//扫码器心跳连接
client.Logger.Info($"心跳{client.IP}:{client.Port}》接收到心跳信息:{mes}");
RefreshStateEvent?.Invoke(client.IP, true);
}
else if (mes == "NoRead")
{ {
client.Logger.Info($"客户端{client.IP}:{client.Port}》NoRead事件{mes}"); int result = ChangeSerialSignal(e.ByteBlock.Buffer,e.ByteBlock.Len);
// TODO if(result == -1)
{
client.Logger.Info($"{client.IP}:{client.Port}》接收到光电信息异常");
}
else
{
string message = result == 1 ? "触发" : "释放";
client.Logger.Info($"{client.IP}:{client.Port}》接收到光电信息:{message}");
SerialSignalPushEvent?.Invoke(client.IP, result);
}
} }
else else //扫码器及相机
{ {
string code = mes.Trim().TrimEnd('\0'); //从客户端收到信息
client.Logger.Info($"客户端{client.IP}:{client.Port}》接收到信息:{mes}"); var mes = Encoding.UTF8.GetString(e.ByteBlock.Buffer, 0, e.ByteBlock.Len);//注意数据长度是byteBlock.Len
// 区分相机扫码器数据
if (client.IP == config.ScannerIP) //心跳包
if (mes == "heartbeat")
{ {
if (code.Contains("heart")) //扫码器心跳连接
{ client.Logger.Info($"心跳{client.IP}:{client.Port}》接收到心跳信息:{mes}");
return Task.CompletedTask; RefreshStateEvent?.Invoke(client.IP, true);
}
RefreshMaterialCodeStrEvent?.Invoke(code, client.IP);
} }
else if (client.IP == config.CameraIP) else if (mes == "NoRead")
{ {
// 相机返回数据 client.Logger.Info($"客户端{client.IP}:{client.Port}》NoRead事件{mes}");
CameraResultEvent?.Invoke(code); // TODO扫码器扫码失败报警
} }
else
{
string code = mes.Trim().TrimEnd('\0');
client.Logger.Info($"客户端{client.IP}:{client.Port}》接收到信息:{mes}");
// 区分相机扫码器数据
if (client.IP == config.ScannerIP)
{
if (code.Contains("heart"))
{
return Task.CompletedTask;
}
RefreshMaterialCodeStrEvent?.Invoke(code, client.IP);
}
else if (client.IP == config.CameraIP)
{
// 相机返回数据
CameraResultEvent?.Invoke(code);
}
}
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -158,5 +187,39 @@ namespace SlnMesnac.TouchSocket
} }
/// <summary>
/// 转换光电信号
/// 释放 01 03 02 01 00 B9 D4
/// 触发 01 03 02 01 FF F9 94
/// </summary>
/// <returns></returns>
public int ChangeSerialSignal(byte[] data,int len)
{
if (data == null || len < 0)
{
return -1;
}
len = Math.Min(len, data.Length);
byte[] CheckByte = new byte[len];
Array.Copy(data, 0, CheckByte, 0, len);
byte[] release = new byte[] { (byte)0x01, (byte)0x03, (byte)0x02, (byte)0x01, (byte)0x00, (byte)0xB9, (byte)0xD4 };
byte[] trigger = new byte[] { (byte)0x01, (byte)0x03, (byte)0x02, (byte)0x01, (byte)0xFF, (byte)0xF9, (byte)0x94 };
if (CheckByte.Length == release.Length && CheckByte.SequenceEqual(release))
{
return 0;
}
else if (CheckByte.Length == trigger.Length && CheckByte.SequenceEqual(trigger))
{
return 1;
}
// 异常
return -1;
}
} }
} }

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<OutputType>WinExe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework> <TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<UseWPF>true</UseWPF> <UseWPF>true</UseWPF>

@ -111,36 +111,36 @@
<RowDefinition Height="0.5*"/> <RowDefinition Height="0.5*"/>
<RowDefinition Height="1.2*"/> <RowDefinition Height="1.2*"/>
</Grid.RowDefinitions> </Grid.RowDefinitions>
<!--<Border Grid.Row="0" BorderThickness="0.5" BorderBrush="#0288d1" CornerRadius="5" > <Border Grid.Row="0" BorderThickness="0.5" BorderBrush="#0288d1" CornerRadius="5" >
<Grid Grid.Row="0" Grid.Column="0"> <Grid Grid.Row="0" Grid.Column="0">
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
<ColumnDefinition Width="*" /> <ColumnDefinition Width="*" />
--><!-- 左侧列 --><!--
<ColumnDefinition Width="*" /> <ColumnDefinition Width="*" />
--><!-- 中间列 --><!--
<ColumnDefinition Width="*" /> <ColumnDefinition Width="*" />
--><!-- 右侧列 --><!--
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
--><!-- 左侧内容 --><!--
<StackPanel Grid.Column="0" VerticalAlignment="Center" Orientation="Horizontal" HorizontalAlignment="Left"> <!--<StackPanel Grid.Column="0" VerticalAlignment="Center" Orientation="Horizontal" HorizontalAlignment="Left">
<TextBlock VerticalAlignment="Center" Text="扫码数:" FontSize="30" Foreground="LawnGreen" /> <TextBlock VerticalAlignment="Center" Text="扫码数:" FontSize="30" Foreground="LawnGreen" />
<TextBox VerticalAlignment="Center" Text="{Binding ScanCount,Mode=TwoWay}" FontSize="30" BorderBrush="Transparent" Background="Transparent" Foreground="LawnGreen" IsReadOnly="True" Margin="0 0 0 0" /> <TextBox VerticalAlignment="Center" Text="{Binding ScanCount,Mode=TwoWay}" FontSize="30" BorderBrush="Transparent" Background="Transparent" Foreground="LawnGreen" IsReadOnly="True" Margin="0 0 0 0" />
</StackPanel> </StackPanel>
--><!-- 中间内容 --><!--
<StackPanel Grid.Column="1" VerticalAlignment="Center" Orientation="Horizontal" HorizontalAlignment="Center"> <StackPanel Grid.Column="1" VerticalAlignment="Center" Orientation="Horizontal" HorizontalAlignment="Center">
<TextBlock VerticalAlignment="Center" Text="识别数:" FontSize="30" Foreground="LawnGreen" /> <TextBlock VerticalAlignment="Center" Text="识别数:" FontSize="30" Foreground="LawnGreen" />
<TextBox VerticalAlignment="Center" Text="{Binding PassCount,Mode=TwoWay}" FontSize="30" BorderBrush="Transparent" Background="Transparent" Foreground="LawnGreen" IsReadOnly="True" Margin="0 0 0 0" /> <TextBox VerticalAlignment="Center" Text="{Binding PassCount,Mode=TwoWay}" FontSize="30" BorderBrush="Transparent" Background="Transparent" Foreground="LawnGreen" IsReadOnly="True" Margin="0 0 0 0" />
</StackPanel> </StackPanel>
--><!-- 右侧内容 --><!--
<StackPanel Grid.Column="2" VerticalAlignment="Center" Orientation="Horizontal" HorizontalAlignment="Right"> <StackPanel Grid.Column="2" VerticalAlignment="Center" Orientation="Horizontal" HorizontalAlignment="Right">
<TextBlock VerticalAlignment="Center" Text="通过率:" FontSize="30" Foreground="LawnGreen" /> <TextBlock VerticalAlignment="Center" Text="通过率:" FontSize="30" Foreground="LawnGreen" />
<TextBox VerticalAlignment="Center" Text="{Binding PassRate,Mode=TwoWay}" FontSize="30" BorderBrush="Transparent" Background="Transparent" Foreground="LawnGreen" IsReadOnly="True" Margin="0 0 0 0" /> <TextBox VerticalAlignment="Center" Text="{Binding PassRate,Mode=TwoWay}" FontSize="30" BorderBrush="Transparent" Background="Transparent" Foreground="LawnGreen" IsReadOnly="True" Margin="0 0 0 0" />
</StackPanel> </StackPanel>-->
</Grid> </Grid>
</Border>--> </Border>
<StackPanel Grid.Row="1" Grid.Column="0" Orientation="Horizontal" VerticalAlignment="Center" > <StackPanel Grid.Row="1" Grid.Column="0" Orientation="Horizontal" VerticalAlignment="Center" >
<TextBlock Text="产品条码" FontSize="22" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0 0 30 0"/> <TextBlock Text="产品条码" FontSize="22" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0 0 30 0"/>
<TextBox FontSize="20" Text="{Binding BoxCode,Mode=TwoWay}" BorderBrush="White" Foreground="White" Width="250" IsReadOnly="True" Margin="0 0 10 0" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" /> <TextBox FontSize="20" Text="{Binding BoxCode,Mode=TwoWay}" BorderBrush="White" Foreground="White" Width="250" IsReadOnly="True" Margin="0 0 10 0" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" />

Loading…
Cancel
Save