Commit 03248b0f 刘韬

添加socket扫码

1 个父辈 3df3ad9e
......@@ -49,6 +49,19 @@ namespace OnlineStore.Common
public static MyConfig<bool> Device_SingleInSingleOut = false;
[MyConfigComment("允许单料口单盘入库")]
public static MyConfig<bool> Device_Allow_SingleIn = false;
[MyConfigComment("是否启用socket扫码")]
public static MyConfig<bool> SocketScanner_enable = false;
[MyConfigComment("socket uri=socket://127.0.0.1:7930")]
public static MyConfig<string> SocketScanner_uri = "socket://127.0.0.1:7930";
[MyConfigComment("socket 扫码等待超时")]
public static MyConfig<int> SocketScanner_receiver_timeout = 10000;
[MyConfigComment("socket 扫码触发代码")]
public static MyConfig<string> SocketScanner_trigger_code= "trigger";
[MyConfigComment("socket 扫码接收匹配正则表达式")]
public static MyConfig<string> SocketScanner_pattern_code= "reel:(.+?)!";
/// <summary>
/// 摄像机名称
/// </summary>
......
......@@ -86,6 +86,7 @@
<Compile Include="DeviceLibrary\OKLEController.cs" />
<Compile Include="DeviceLibrary\ServerCommunication.cs" />
<Compile Include="DeviceLibrary\AxisBean.cs" />
<Compile Include="DeviceLibrary\SocketScanner.cs" />
<Compile Include="theMachine\BoxTransport.cs" />
<Compile Include="theMachine\Common.cs" />
<Compile Include="theMachine\JobList.cs" />
......
using ConfigHelper;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DeviceLibrary
{
public class SocketScanner:ICustEditor
{
static SocketScanner()
{
AdvanceConfigForm.AddCustomEditor<SocketScanner>(Setting_Init.SocketScanner_uri.Key);
}
public object ValueEdit(object value)
{
var r = RobotManage.socketScanner.Scan(Setting_Init.SocketScanner_trigger_code, Setting_Init.SocketScanner_pattern_code, out string code);
MessageBox.Show($"Scan Result:{r},code:{code}");
return value;
}
public SocketScanner()
{ }
Uri server;
int Timeout;
private TcpClient socket = null;
public SocketScanner(string uri, int timeout)
{
server = new Uri(uri);
Timeout = timeout;
}
public bool StartConnect(out string msg)
{
msg = "";
if (socket == null)
socket = new TcpClient();
try
{
if (socket.Connected)
return true;
socket.Close();
socket = new TcpClient();
socket.ReceiveTimeout = Timeout;
var stask = socket.ConnectAsync(server.Host, server.Port);
var result = stask.Wait(3000);
LogUtil.error("SocketScanner Connect:" + result);
return result;
}
catch (Exception ex)
{
LogUtil.error("SocketScanner 出错:" + ex.ToString());
msg = ex.Message;
return false;
}
}
~SocketScanner()
{
if (socket != null && socket.Connected)
socket.Close();
}
public bool Scan(string trigger, string pattern, out string result)
{
result = "";
if (!StartConnect(out _))
{
return false;
}
var bf = Encoding.ASCII.GetBytes(trigger);
socket.Client.Send(bf);
var buff = new byte[200];
int i;
try
{
i = socket.Client.Receive(buff);
}
catch
{
return false;
}
if (i == 0)
return false;
var recv = Encoding.ASCII.GetString(buff, 0, i);
var m = Regex.Match(recv, pattern);
if (m.Success)
{
var index = m.Groups.Count - 1;
result = m.Groups[index].Value;
return true;
}
return false;
}
}
}
......@@ -416,18 +416,18 @@ namespace DeviceLibrary
IOMove(IO_Type.Camera_Led, IO_VALUE.HIGH);
//Task.Delay(10).Wait();
List<CodeInfo> LastCodeList;
LastCodeList = CodeManager.CameraScan(CodeManager.hikNameList);
//BoxParam labelParam = new BoxParam();
//labelParam.codeInfos = new List<CodeInfo>(LastCodeList);
//if (!Common.codeProcess(labelParam, out _))
//{
// Task.Delay(500).Wait();
// LastCodeList2 = CodeManager.CameraScan(new List<string> { Config.CameraName });
// LastCodeList.AddRange(LastCodeList2);
//}
//IOMove(IO_Type.Camera_Led, IO_VALUE.LOW);
if (Setting_Init.SocketScanner_enable) {
LastCodeList = new List<CodeInfo>();
if (RobotManage.socketScanner.Scan(Setting_Init.SocketScanner_trigger_code, Setting_Init.SocketScanner_pattern_code, out string code)) {
LastCodeList.Add(new CodeInfo(code, 0, 0));
}
}
else
{
LastCodeList = CodeManager.CameraScan(CodeManager.hikNameList);
}
return LastCodeList;
}));
......
......@@ -40,7 +40,7 @@ namespace DeviceLibrary
static Thread mainThread;
public static bool haveFixpos=false;
public static SocketScanner socketScanner;
public static HIKCamera CameraA=new HIKCamera();
public static void Init() {
string msg = "";
......@@ -78,6 +78,14 @@ namespace DeviceLibrary
IsLoadOk = false;
msg += crc.GetString(L.iocard_init_fail, "IO板卡初始化失败")+ "\n";
}
if (Setting_Init.SocketScanner_enable) {
socketScanner = new SocketScanner(Setting_Init.SocketScanner_uri, Setting_Init.SocketScanner_receiver_timeout);
if (!socketScanner.StartConnect(out string msg1)) {
IsLoadOk = false;
msg += "连接Socket扫码服务失败:"+msg1 + "\r\n";
}
}
if (!CameraA.LoadCameraConfig("CameraA", out string errmsg))
{
IsLoadOk = false;
......
......@@ -22,6 +22,7 @@ namespace TheMachine
[STAThread]
static void Main()
{
_ = new Mutex(true, Application.ProductName, out bool ret);
if (!ret)
{
......@@ -47,7 +48,6 @@ namespace TheMachine
}
Config.LoadMyConfig(new Setting_Init().GetType());
Application.ThreadException += Application_ThreadException;
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!