64 lines
2.0 KiB
C#
64 lines
2.0 KiB
C#
using System.IO.Ports;
|
|
using System.Security.AccessControl;
|
|
using System.Text;
|
|
using Serilog;
|
|
using VisionBuilder.UI.Common;
|
|
using VisionBuilder.UI.Common.Processing;
|
|
|
|
namespace PackstrasseBarcodeReader
|
|
{
|
|
public class PackstrasseBarcodeReaderModule : IVisionBuilderModule
|
|
{
|
|
private readonly PackstrasseBarcodeReaderSettings _settings;
|
|
private readonly IRecognitionControl _recognitionControl;
|
|
private SerialPort _port;
|
|
|
|
|
|
public PackstrasseBarcodeReaderModule(PackstrasseBarcodeReaderSettings settings, IRecognitionControl recognitionControl)
|
|
{
|
|
_settings = settings;
|
|
_recognitionControl = recognitionControl;
|
|
}
|
|
|
|
private void _port_DataReceived(object sender, SerialDataReceivedEventArgs e)
|
|
{
|
|
Thread.Sleep(100);
|
|
byte[] buffer = new byte[_port.BytesToRead];
|
|
_port.Read(buffer, 0, _port.BytesToRead);
|
|
var data = Encoding.ASCII.GetString(buffer).Trim();
|
|
var recipes = _recognitionControl.GetRecipesData();
|
|
|
|
string searchString = data;
|
|
if (_settings.BarcodeRecipeMappings.Any(x=>x.Barcode==searchString))
|
|
{
|
|
searchString = _settings.BarcodeRecipeMappings.First(x => x.Barcode == searchString).RecipeName;
|
|
}
|
|
|
|
var existingRecipe=recipes.FirstOrDefault(x => x.RecipeName == searchString);
|
|
if (existingRecipe == null)
|
|
{
|
|
Log.Warning("No matching recipe found for data: {Data}", data);
|
|
return;
|
|
}
|
|
|
|
_recognitionControl.SetRecipe(existingRecipe);
|
|
}
|
|
|
|
|
|
public void InitializeModule()
|
|
{
|
|
try
|
|
{
|
|
_port = new SerialPort(_settings.ComPort, 9600);
|
|
_port.Open();
|
|
_port.DataReceived += _port_DataReceived;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.Warning(e, "Failed to open com-port");
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|