最近接到某电子厂一个需求:检测工件。通过工业相机每次对工件拍3张照片,并通过扫码枪的扫码信号将照片和二维码信息一起存储到数据库。
工业应用大多依赖于Windows平台,Windows上天然集成了.net运行时,封装简化了win32API,使用C# +Sqlsugar+serialPort 就能轻松实现以上需求。
在C# 中,提供了
public MyPageOne(ServiceProvider sp)
{
InitializeComponent();
this.sp = sp;
this.helper = sp.GetService<SqlSugarHelper>();
// 创建 FileSystemWatcher 实例
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = folderPath;
watcher.Created += Watcher_Created;
watcher.EnableRaisingEvents = true;
}
这样的事件监控文件新增,同理还有文件改动,删减等。
事件中写入:
private void Watcher_Created(object sender, FileSystemEventArgs e)
{
// 提取新增文件的文件名
//string fileName = Path.GetFileName(e.FullPath);
//Console.WriteLine($"新增文件:{fileName}");
if (IsImageFile(e.FullPath) && newFiles.Count < 3)
{
newFiles.Add(e.FullPath);
//newFiles.Add(fileName);
}
//以下放入扫码枪事件,写入就删除
if (newFiles.Count == 3)
{
//Console.WriteLine("当前新增文件:");
foreach (string file in newFiles)
{
byte[] newImageBytes = File.ReadAllBytes(file);
PicByte picByte = new PicByte();
picByte.InsertTime = DateTime.Now;
picByte.MyValue = newImageBytes;
helper.Db.Insertable<PicByte>(picByte).ExecuteCommand();
}
newFiles = null;
newFiles = new List<string>();
}
//以上放入扫码枪事件,写入就删除
}
查询和工具函数:
private void BtnQueryById_Click(object sender, EventArgs e)
{
string id = textBox1.Text.Trim();
byte[] imagebytes = null;
SqlSugarHelper helper = sp.GetService<SqlSugarHelper>();
if (id != null)
{
imagebytes = helper.Db.Queryable<PicByte>().Where(x => x.Id == long.Parse(id)).Select(x => x.MyValue).First();
//MemoryStream ms = new MemoryStream(imagebytes);
//Bitmap bmpt = new Bitmap(ms);
//this.PicBox.Image = bmpt;
PicBox.Image = ConvertToImage(imagebytes);
}
}
private bool IsImageFile(string filePath)
{
string extension = Path.GetExtension(filePath).ToLower();
return extension == ".jpg" || extension == ".jpeg" || extension == ".png" || extension == ".bmp";
}
private Image ConvertToImage(byte[] imageData)
{
using (MemoryStream memoryStream = new MemoryStream(imageData))
{
return Image.FromStream(memoryStream);
}
}
namespace 楷新.SqlSugar
{
public interface ISqlSugarService
{
string ConnectionStr { get; set; }
}
}
namespace 楷新.SqlSugar.Impl
{
public class SqlSugarService : ISqlSugarService
{
//private string connectionStr = "Data Source=MyDb.db";
private string connectionStr = "database='" + "mmallss" + "';Data Source = '" + "127.0.0.1" + "'; User Id = '" + "root" + "'; pwd='" + "123456" + "';charset='utf8mb4';pooling=true";
public string ConnectionStr
{
get { return connectionStr; }
set { connectionStr = value; }
}
}
}
using SqlSugar;
namespace 楷新.SqlSugar
{
public interface ISqlSugarHelper
{
SqlSugarClient Db { get; }
}
}
using SqlSugar;
namespace 楷新.SqlSugar.Impl
{
public class SqlSugarHelper : ISqlSugarHelper
{
public SqlSugarHelper()
{
}
public SqlSugarHelper(SqlSugarService sqlSugarService)
{
this.sqlSugarService = sqlSugarService;
}
private SqlSugarService sqlSugarService;
public SqlSugarClient Db
{
get => new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = this.sqlSugarService.ConnectionStr,
//DbType = DbType.Sqlite, //必填, 数据库类型
DbType = DbType.MySql,
IsAutoCloseConnection = true, //默认false, 时候知道关闭数据库连接, 设置为true无需使用using或者Close操作
InitKeyType = InitKeyType.Attribute //默认SystemTable, codefist需要使用Attribute
});
}
}
}
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Windows.Forms;
using 楷新.PLC.Modbus;
using 楷新.PLC.Modbus.Impl;
using 楷新.Robot.Fanuc;
using 楷新.Robot.Fanuc.Utils;
using 楷新.Service;
using 楷新.Service.Impl;
using 楷新.Splash;
using 楷新.SqlSugar.Impl;
namespace 楷新
{
internal static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
ServiceCollection services = new ServiceCollection();
services.AddScoped<IOnlyRun, OnlyRun>();
services.AddScoped<SqlSugarService>();
services.AddScoped<SqlSugarHelper>();
services.AddScoped<CoreData>();
services.AddScoped<MyXyzPwrArr>();
services.AddScoped<MyAlarmArr>();
services.AddScoped<MyDIArr>();
services.AddScoped<MyDOArr>();
services.AddScoped<MyRArr>();
services.AddScoped<MyFanucAxisData>();
services.AddScoped<IModbusHelper, ModbusHelper>();
services.AddScoped<IScannerService, ScannerServiceImpl>();
ServiceProvider sp = services.BuildServiceProvider();
IOnlyRun onlyRun = sp.GetService<IOnlyRun>();
if (!onlyRun.Mutex.WaitOne(0, false))
{
MessageBox.Show("控制系统已经运行!", "系统运行", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
Application.Exit();
return;
}
else
{
Splasher.Show(typeof(FrmSplash));
Application.Run(new FrmMain(sp));
//FrmLogin objFrm = new FrmLogin(sqlSugarHelper);
//objFrm.TopMost = true;
//if (objFrm.ShowDialog() == DialogResult.OK)
//{
// Application.Run(frmMain);
//}
//else
//{
// Application.Exit();
//}
}
}
}
}