RFID
public class Root : MonoBehaviour
{
ReaderMethod reader;
ReaderSetting m_curSetting = new ReaderSetting();
InventoryBuffer m_curInventoryBuffer = new InventoryBuffer();
string strComPort = "COM4", strException;
int nBaudrate = 115200;
bool m_bInventory;//是否正在盘存操作中
void Start()
{
//初始化访问读写器实例
reader = new ReaderMethod();
reader.AnalyCallback = AnalyData;
reader.ReceiveCallback = ReceiveData;
reader.SendCallback = SendData;
OpenRFID();
}
private void ReceiveData(byte[] btAryReceiveData)
{
string strLog = CCommondMethod.ByteArrayToString(btAryReceiveData, 0, btAryReceiveData.Length);
Debug.LogError("接受原始数据:" + strLog);
}
private void SendData(byte[] btArySendData)
{
string strLog = CCommondMethod.ByteArrayToString(btArySendData, 0, btArySendData.Length);
Debug.LogError("发送原始数据:"+strLog);
}
void Update()
{
}
void SettingRFID()
{
try
{
m_curInventoryBuffer.ClearInventoryPar();
m_curInventoryBuffer.btRepeat = 0x01;
m_curInventoryBuffer.lAntenna.Add(0x00);
m_bInventory = true;
m_curInventoryBuffer.bLoopInventory = true;
m_curInventoryBuffer.bLoopInventoryReal = true;
m_curInventoryBuffer.ClearInventoryRealResult();
byte btWorkAntenna = m_curInventoryBuffer.lAntenna[m_curInventoryBuffer.nIndexAntenna];
int State = reader.SetWorkAntenna(m_curSetting.btReadId, btWorkAntenna);
m_curSetting.btWorkAntenna = btWorkAntenna;
// Debug.LogError(State == 0 ? "天线设置成功!" : "天线设置失败!");
}
catch (System.Exception ex)
{
Debug.LogError(ex.Message);
}
}
private void AnalyData(MessageTran msgTran)
{
Debug.LogError("返回数据");
if (msgTran.PacketType != 0xA0)
{
return;
}
switch (msgTran.Cmd)
{
case 0x74:
ProcessSetWorkAntenna(msgTran);
break;
default:
break;
}
}
private void ProcessSetWorkAntenna(MessageTran msgTran)
{
int intCurrentAnt = 0;
intCurrentAnt = m_curSetting.btWorkAntenna + 1;
string strCmd = "设置工作天线成功,当前工作天线: 天线" + intCurrentAnt.ToString();
Debug.LogError(strCmd);
string strErrorCode = string.Empty;
/*
if (msgTran.AryData.Length == 1)
{
if (msgTran.AryData[0] == 0x10)
{
m_curSetting.btReadId = msgTran.ReadId;
//校验是否盘存操作
if (m_bInventory)
{
RunLoopInventroy();
}
return;
}
else
{
// strErrorCode = CCommondMethod.FormatErrorCode(msgTran.AryData[0]);
}
}
else
{
strErrorCode = "未知错误";
}
string strLog = strCmd + "失败,失败原因: " + strErrorCode;
WriteLog(lrtxtLog, strLog, 1);
if (m_bInventory)
{
m_curInventoryBuffer.nCommond = 1;
m_curInventoryBuffer.dtEndInventory = DateTime.Now;
RunLoopInventroy();
}*/
}
void OpenRFID()
{
int nRet = reader.OpenCom(strComPort, nBaudrate, out strException);
if (nRet != 0)
{
Debug.LogError("连接读写器失败,失败原因: " + strException);
return;
}
else
{
Debug.LogError("连接读写器 " + strComPort + "@" + nBaudrate.ToString());
reader.GetFirmwareVersion(m_curSetting.btReadId);
//SettingRFID();
}
}
void CloseRFID()
{
//处理串口断开连接读写器
reader.CloseCom();
}
private void OnDestroy()
{
CloseRFID();
}
}