📄 cs2010.cpp
字号:
// CS2010.cpp: implementation of the CCS2010 class.
//
//////////////////////////////////////////////////////////////////////
#include "CS2010.h"
#include <uBase.h>
#include <stdio.h>
#include <sys/iuart.h>
#include <stdlib.h>
#include <sys/ispeaker.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CCS2010::CCS2010()
{
_ComPort=COM3;
_BaudRate=9600;
_Data=8;
_Parity=NOPARITY;
_Stop=ONESTOPBIT;
_Status=CLOSE;
_Timeout=TIME_OUT;
}
CCS2010::~CCS2010()
{
if (_Status==OPEN)
{
CloseDevice();
}
}
int CCS2010::SetParam(int ComPort,int BaudRate,int Data,int Parity,int Stop,int Timeout)
{
if ((ComPort<1)|(ComPort>8))
{
sprintf(_Error,"Error with ComPort!");
return 0;
}
if (!(BaudRate==9600))
{
sprintf(_Error,"Error with BaudRate!");
return 0;
}
if (!(Data==8))
{
sprintf(_Error,"Error with Data!");
return 0;
}
if (!(Stop==ONESTOPBIT))
{
sprintf(_Error,"Error with Stop!");
return 0;
}
if (!(Parity==NOPARITY))
{
sprintf(_Error,"Error with Parity!");
return 0;
}
if (Timeout<=0)
{
sprintf(_Error,"Error with Timeout!");
return 0;
}
_ComPort=ComPort;
_BaudRate=BaudRate;
_Data=Data;
_Parity=Parity;
_Stop=Stop;
_Timeout=Timeout;
return 1;
}
int CCS2010::OpenDevice()
{
UartOpen(_ComPort);
//设置参数
UART_PARAM param;
param.baudrate = _BaudRate;
param.data = _Data;
param.parity = _Parity;
param.stop = _Stop;
if (UartSetParam(_ComPort, ¶m)==-1)
{
sprintf(_Error,"Open device with UartSetParam Error!");
return 0;
}
_Status=OPEN;
return 1;
}
int CCS2010::CloseDevice()
{
UartClose(_ComPort);
_Status=CLOSE;
return 1;
}
int CCS2010::ReadCardID(unsigned char *CardId)
{
if (_Status!=OPEN)
{
sprintf(_Error,"The device has not been opened!");
return 0;
}
int m_return;
char buf[10];
memset(buf, 0x20, 10);
UartClear(_ComPort);
UartWriteChar(_ComPort,'R',100);
UartClear(_ComPort);
m_return=UartRead(_ComPort,buf,10, _Timeout);
if (m_return==0)
{
sprintf(_Error,"No Card!");
}
if (m_return==10 && buf[0]=='0' && buf[1]=='0' && buf[2]=='0' && buf[3]=='0' && buf[4]=='0' && buf[5]=='0' && buf[6]=='0' && buf[7]=='0' && buf[8]=='0' && buf[9]=='0')//0x0D
{
sprintf(_Error,"No Card!");
m_return=0;
}
if (m_return==10 && buf[9]==0x0D)
{
char SS;
SS=buf[0]+buf[1]+buf[2]+buf[3]+buf[4]+buf[5]+buf[6]+buf[7];
if (SS!=buf[8])
{
sprintf(_Error,"Check Error!");
m_return=0;
}
else
{
CardId[0] = buf[0];
CardId[1] = buf[1];
CardId[2] = buf[2];
CardId[3] = buf[3];
CardId[4] = buf[4];
CardId[5] = buf[5];
CardId[6] = buf[6];
CardId[7] = buf[7];
CardId[8] = '\0';
m_return=1;
}
}
UartClear(_ComPort);
return m_return;
}
int CCS2010::BeepStatus(int Status)
{
if (Status==1)
beep_frq(2000,150);
else
beep_frq(4000,500);
return 0;
}
int CCS2010::GetError(char *Error)
{
sprintf(Error,"%s",_Error);
return 0;
}
int CCS2010::ShakeStatus(int Status)
{
if (Status==1)
Vibra(100);
else
Vibra(300);
return 0;
}
int CCS2010::FlashStatus(int Status)
{
//每通过发送“L”指令,使读头发光一次
if (Status==1)
{
UartWriteChar(_ComPort,'L',100);
}
else
{
UartWriteChar(_ComPort,'L',300);
UartWriteChar(_ComPort,'L',301);
UartWriteChar(_ComPort,'L',303);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -