Commit 8432f570 by 陶然

CP2101 init

parent 0a0b5331
File added
......@@ -117,7 +117,7 @@
this.lbCurrentPackageNode.Font = new System.Drawing.Font("宋体", 12F);
this.lbCurrentPackageNode.Location = new System.Drawing.Point(16, 180);
this.lbCurrentPackageNode.Name = "lbCurrentPackageNode";
this.lbCurrentPackageNode.Size = new System.Drawing.Size(80, 16);
this.lbCurrentPackageNode.Size = new System.Drawing.Size(79, 16);
this.lbCurrentPackageNode.TabIndex = 7;
this.lbCurrentPackageNode.Text = "当前袋子:";
//
......@@ -127,7 +127,7 @@
this.lbCurrentPackage.Font = new System.Drawing.Font("宋体", 12F);
this.lbCurrentPackage.Location = new System.Drawing.Point(16, 127);
this.lbCurrentPackage.Name = "lbCurrentPackage";
this.lbCurrentPackage.Size = new System.Drawing.Size(80, 16);
this.lbCurrentPackage.Size = new System.Drawing.Size(79, 16);
this.lbCurrentPackage.TabIndex = 6;
this.lbCurrentPackage.Text = "当前箱号:";
//
......@@ -137,7 +137,7 @@
this.lbCurrentLocation.Font = new System.Drawing.Font("宋体", 12F);
this.lbCurrentLocation.Location = new System.Drawing.Point(16, 74);
this.lbCurrentLocation.Name = "lbCurrentLocation";
this.lbCurrentLocation.Size = new System.Drawing.Size(80, 16);
this.lbCurrentLocation.Size = new System.Drawing.Size(79, 16);
this.lbCurrentLocation.TabIndex = 5;
this.lbCurrentLocation.Text = "当前位置:";
//
......@@ -147,7 +147,7 @@
this.lbCurrentDepartment.Font = new System.Drawing.Font("宋体", 12F);
this.lbCurrentDepartment.Location = new System.Drawing.Point(16, 21);
this.lbCurrentDepartment.Name = "lbCurrentDepartment";
this.lbCurrentDepartment.Size = new System.Drawing.Size(80, 16);
this.lbCurrentDepartment.Size = new System.Drawing.Size(79, 16);
this.lbCurrentDepartment.TabIndex = 4;
this.lbCurrentDepartment.Text = "当前部门:";
//
......@@ -577,7 +577,7 @@
this.Controls.Add(this.plLeft);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "FrmMain";
this.Text = "样品入箱客户端 1.0(23.6.08 D500)";
this.Text = "样品入箱客户端 1.0(23.6.09 CP2101)";
this.Load += new System.EventHandler(this.FrmMain_Load);
this.plLeft.ResumeLayout(false);
this.gbInfo.ResumeLayout(false);
......
using Kivii.Client.SamplePackages.Entities;
using Kivii.Linq;
using Kivii.Rfid.D500;
using Kivii.Samples.Entities;
using Kivii.Samples.Transforms;
using System;
......@@ -49,7 +48,7 @@ namespace Kivii.Client.SamplePackages
//加载事件
ReaderInstance.Connected += new EventHandler<ConnectionChangedEventArgs>(reader_Connected);
ReaderInstance.ConnectionLost += reader_ConnectionLost;
ReaderInstance.Disconnected += reader_Disconnected;
//ReaderInstance.Disconnected += reader_Disconnected;
}
private void initControls()
......@@ -453,11 +452,11 @@ namespace Kivii.Client.SamplePackages
_tmr = null;
}
private void reader_Disconnected(object sender, ConnectionChangedEventArgs e)
{
_tmr?.Stop();
_tmr = null;
}
//private void reader_Disconnected(object sender, ConnectionChangedEventArgs e)
//{
// _tmr?.Stop();
// _tmr = null;
//}
private void reader_Connected(object sender, ConnectionChangedEventArgs e)
{
......@@ -477,19 +476,9 @@ namespace Kivii.Client.SamplePackages
{
return;
}
string epc = null;
Thread.Sleep(150);//给下一个写卡让出时间
ReadEpcResult writeResult = ReaderInstance.ReadEpc();
if (writeResult.ReadResult.Result == AccessResult.Success)
{
epc = writeResult.TagAccessed.Epc;
}
var epc = ReaderInstance.ReadEpc();
samplePrepackaging(tid, epc);
//var th = KiviiContext.NewThread(() =>
//{
// samplePrepackaging(tid);
//});
//th.Start();
}
catch (Exception ex)
{
......@@ -648,8 +637,7 @@ namespace Kivii.Client.SamplePackages
#endregion
if (ReaderInstance != null && ReaderInstance.State == ApplicationConnectionState.Connected)
{
ReaderInstance.Stop();
ReaderInstance.DisConnect();
ReaderInstance.CloseSpecComPort();
ReaderInstance = null;
btnScanRfid.Text = "开始扫描";
btnScanRfid.BackColor = Color.PapayaWhip;
......@@ -657,29 +645,12 @@ namespace Kivii.Client.SamplePackages
}
else
{
initReader();
List<string> readers = null;
initReader();
#region 获取读卡器
try
{
readers = ReaderInstance.GetReaderList();
if (readers.Count < 1)
{
MessageBox.Show("未能连接RFID读写终端,请检查连接线!", "连接读卡器失败");
ReaderInstance = null;
return;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "初始化失败");
ReaderInstance = null;
return;
}
try
{
ReaderInstance.Connect(readers[0]);
ReaderInstance.AutoOpenComPort();
}
catch (Exception ex)
{
......
......@@ -47,9 +47,6 @@
<Reference Include="Kivii.Linq.V4.0, Version=5.6.2023.4200, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Kivii.Linq.5.6.2023.4200\lib\net40\Kivii.Linq.V4.0.dll</HintPath>
</Reference>
<Reference Include="Kivii.Rfid.D500.V4.0">
<HintPath>Content\Kivii.Rfid.D500.V4.0.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
......@@ -60,6 +57,9 @@
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="UHFReader18CSharp">
<HintPath>Content\UHFReader18CSharp.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Configs.cs" />
......@@ -73,6 +73,7 @@
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Reader.cs" />
<Compile Include="Requests\RestfulRequests.cs" />
<EmbeddedResource Include="FrmMain.resx">
<DependentUpon>FrmMain.cs</DependentUpon>
......@@ -97,12 +98,13 @@
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
<None Include="Resources\批量读卡器文档.zip" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Content\Kivii.Rfid.D500.V4.0.dll" />
<None Include="Resources\一体机开发包.zip" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Basic.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="Content\UHFReader18CSharp.dll" />
<Content Include="F.ico" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
......
......@@ -13,7 +13,7 @@ namespace Kivii.Client.SamplePackages
private static bool isProgramExisted()
{
bool rtns = false;
_mutex = new Mutex(true, "Kivii.Rfid.D500.V4.0.dll", out rtns);
_mutex = new Mutex(true, "UHFReader18CSharp.dll", out rtns);
//mutex.Dispose();
return !rtns;
}
......
......@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("5.4.2023.6050")]
[assembly: AssemblyFileVersion("5.4.2023.6050")]
[assembly: AssemblyVersion("5.4.2023.6090")]
[assembly: AssemblyFileVersion("5.4.2023.6090")]
using ReaderB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Kivii.Client.SamplePackages
{
public class Reader
{
public event EventHandler<ConnectionChangedEventArgs> Connected;
public event EventHandler<ConnectionChangedEventArgs> ConnectionLost;
public ApplicationConnectionState State { get; private set; }
private byte fComAdr = 0xff; //当前操作的ComAdr
private byte fBaud;
private double fdminfre;
private double fdmaxfre;
private int frmcomportindex;
private int fOpenComIndex; //打开的串口索引号
private int fCmdRet = 30; //所有执行指令的返回值
private bool fIsInventoryScan;
private int ferrorcode;
private string fInventory_EPC_List; //存贮询查列表(如果读取的数据没有变化,则不进行刷新)
private byte Maskadr;
private byte MaskLen;
private byte MaskFlag;
private byte[] fPassWord = new byte[4];
private bool IsConnected { get; set; } = false;
public virtual void OnConnected(ConnectionChangedEventArgs args)
{
State = args.State;
if (null != this.Connected)
{
this.Connected(this, args);
}
}
public virtual void OnConnectionLost(ConnectionChangedEventArgs args)
{
State = args.State;
if (null != this.ConnectionLost)
{
this.ConnectionLost(this, args);
}
}
private string GetErrorCodeDesc(int cmdRet)
{
switch (cmdRet)
{
case 0x00:
return "其它错误";
case 0x03:
return "存储器超限或不被支持的PC值";
case 0x04:
return "存储器锁定";
case 0x0b:
return "电源不足";
case 0x0f:
return "非特定错误";
default:
return "";
}
}
private byte[] HexStringToByteArray(string s)
{
s = s.Replace(" ", "");
byte[] buffer = new byte[s.Length / 2];
for (int i = 0; i < s.Length; i += 2)
buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
return buffer;
}
private string ByteArrayToHexString(byte[] data)
{
StringBuilder sb = new StringBuilder(data.Length * 3);
foreach (byte b in data)
sb.Append(Convert.ToString(b, 16).PadLeft(2, '0'));
return sb.ToString().ToUpper();
}
public bool AutoOpenComPort()
{
if (IsConnected) return IsConnected;
int port = 0;
int openresult;
openresult = 30;
fBaud = Convert.ToByte(6);
fComAdr = Convert.ToByte("FF", 16); // $FF;
openresult = StaticClassReaderB.AutoOpenComPort(ref port, ref fComAdr, fBaud, ref frmcomportindex);
fOpenComIndex = frmcomportindex;
if ((fOpenComIndex == -1) && (openresult == 0x30)) throw new Exception("串口通讯错误");
IsConnected = true;
var args = new ConnectionChangedEventArgs();
args.State = ApplicationConnectionState.Connected;
this.OnConnected(args);
return IsConnected;
}
public bool CloseSpecComPort()
{
if (!IsConnected) return IsConnected;
int port = fOpenComIndex;
if (port < 0) throw new Exception("要关闭的端口号错误");
fCmdRet = StaticClassReaderB.CloseSpecComPort(port);
if (fCmdRet != 0) throw new Exception("串口通讯错误");
IsConnected = false;
var args = new ConnectionChangedEventArgs();
args.State = ApplicationConnectionState.ConnectionLost;
this.OnConnectionLost(args);
return IsConnected;
}
public string ReadEpc()
{
int CardNum = 0;
int Totallen = 0;
int EPClen, m;
byte[] EPC = new byte[5000];
int CardIndex;
string temps;
string s, sEPC = null;
fIsInventoryScan = true;
byte AdrTID = 0;
byte LenTID = 0;
byte TIDFlag = 0;
ListViewItem aListItem = new ListViewItem();
fCmdRet = StaticClassReaderB.Inventory_G2(ref fComAdr, AdrTID, LenTID, TIDFlag, EPC, ref Totallen, ref CardNum, frmcomportindex);
if ((fCmdRet == 1) | (fCmdRet == 2) | (fCmdRet == 3) | (fCmdRet == 4) | (fCmdRet == 0xFB))//代表已查找结束,
{
byte[] daw = new byte[Totallen];
Array.Copy(EPC, daw, Totallen);
temps = ByteArrayToHexString(daw);
fInventory_EPC_List = temps; //存贮记录
m = 0;
if (CardNum == 0)
{
fIsInventoryScan = false;
return string.Empty;
}
EPClen = daw[m];
sEPC = temps.Substring(m * 2 + 2, EPClen * 2);
m = m + EPClen + 1;
if (sEPC.Length != EPClen * 2)
{
fIsInventoryScan = false;
return string.Empty;
}
}
fIsInventoryScan = false;
return sEPC;
}
public string ReadTid()
{
int CardNum = 0;
int Totallen = 0;
int EPClen, m;
byte[] EPC = new byte[5000];
int CardIndex;
string temps;
string s, sTID = null;
fIsInventoryScan = true;
byte AdrTID = Convert.ToByte("00", 16);
byte LenTID = Convert.ToByte("06", 16);
byte TIDFlag = 1;
ListViewItem aListItem = new ListViewItem();
fCmdRet = StaticClassReaderB.Inventory_G2(ref fComAdr, AdrTID, LenTID, TIDFlag, EPC, ref Totallen, ref CardNum, frmcomportindex);
if ((fCmdRet == 1) | (fCmdRet == 2) | (fCmdRet == 3) | (fCmdRet == 4) | (fCmdRet == 0xFB))//代表已查找结束,
{
byte[] daw = new byte[Totallen];
Array.Copy(EPC, daw, Totallen);
temps = ByteArrayToHexString(daw);
fInventory_EPC_List = temps; //存贮记录
m = 0;
if (CardNum == 0)
{
fIsInventoryScan = false;
return string.Empty;
}
EPClen = daw[m];
sTID = temps.Substring(m * 2 + 2, EPClen * 2);
m = m + EPClen + 1;
if (sTID.Length != EPClen * 2)
{
fIsInventoryScan = false;
return string.Empty;
}
}
fIsInventoryScan = false;
return sTID;
}
public bool ProgramEpc(string writeEpc, string accessPassword, out string epc)
{
fIsInventoryScan = true;
byte[] WriteEPC = new byte[100];
byte WriteEPClen;
byte ENum;
if ((writeEpc.Length % 4) != 0)
{
epc = "请输入以字为单位的16进制数!'+#13+#10+'例如:1234、12345678!";
return false;
}
WriteEPClen = Convert.ToByte(writeEpc.Length / 2);
ENum = Convert.ToByte(writeEpc.Length / 4);
byte[] EPC = new byte[ENum];
EPC = HexStringToByteArray(writeEpc);
if (string.IsNullOrEmpty(accessPassword)) { accessPassword = "00000000"; }
fPassWord = HexStringToByteArray(accessPassword);
fCmdRet = StaticClassReaderB.WriteEPC_G2(ref fComAdr, fPassWord, EPC, WriteEPClen, ref ferrorcode, frmcomportindex);
if (fCmdRet != 0)
{
epc = GetErrorCodeDesc(ferrorcode);
return false;
}
fIsInventoryScan = false;
epc = ByteArrayToHexString(EPC);
return true;
}
}
public class ConnectionChangedEventArgs : EventArgs
{
public object ReaderIdentity { get; set; }
public ApplicationConnectionState State { get; set; }
public DateTime Timestamp { get; set; }
}
public enum ApplicationConnectionState
{
NotConnected,
Connected,
Disconnected,
ConnectionLost,
FailedReaderNotOnNetwork,
FailedReaderConnectedToAnother,
FailedReaderOtherReason
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment