📄 sim.c
字号:
return 0;
}
for(i=0;i<8;i++) //比较机器编号
if(dat[i]!=RxdBuf[i+10]) //多一个返回的命令字
{
LcdDisplay(16,0,"机器编号不一致!",3);
LcdDisplay(32,0,"非本机税控卡!",0);
return 0;
}
if(x==0)
{
if(RxdBuf[1]==0)
{//卡已经注册
LcdDisplay(16,0,"本税控卡已经注册!",3);
LcdDisplay(32,0,"不能再次注册!",0);
return 0;
}
}
else
{
if(RxdBuf[1]==1)
{//卡没有注册
LcdDisplay(16,0,"本税控卡没有注册!",3);
return 0;
}
}
return 1;
}
uchar UpdataUser()
{
// gyt2 *xp;
uchar n=0x50;
// UserIdentify(1);
// RxdBuf[0]=0xEF;
// RxdBuf[1]=0x08;
// SelectFile(0x00,0x00,0x02,RxdBuf,1);
// ReadBinary(0x88,0,2,1);
// if(xp->dat!=0xFFFF)
// {
// xp=&RxdBuf[1];
// n=xp->dat*2+2;
// if(n>0x50)n=0x50;
// }
// else n=0x50;
ReadBinary(0x88,0,n,1);
I2cWrite(MultiUserInfo,n,RxdBuf);
return 1;
}
uchar UpdateTaxIndex()
{
uint adr;
uchar i,j,dat[50],str[8];
RxdBuf[0]=0xEF;
RxdBuf[1]=0x03;
SelectFile(0x00,0x00,0x02,RxdBuf,1);
adr=TaxItemsIndex;
for(i=0;i<6;i++)
{//读出税种税目索引文件并写入Flash
j=ReadRecord(0,i+1,0x04,47,1);
str[i]=RxdBuf[0];
for(j=0;j<47;j++)dat[j]=RxdBuf[j];
I2cWrite(adr,47,dat);
adr+=47;
}
I2cWrite(IntradayIndex,6,str); //日缓冲区税种税目索引号
I2cWrite(DecTaxIndex,6,str); //申报数据缓冲区税种税目索引号
I2cWrite(IndexItems,6,str);
return 1;
}
uchar UpdateControls()
{//MAC2是从用户卡中读出的
uint statecode;
uchar i,j,k,dat[30],str[8];
bit flg;
//---------------------------------------------------------------------------------------------
ReadBinary(0x82,57,40,0); //检查纳税人单位名称是否一致
for(i=0;i<40;i++)dat[i]=RxdBuf[i];
RxdBuf[0]=0xEF;
RxdBuf[1]=0x02;
SelectFile(0x00,0x00,0x02,RxdBuf,1);
ReadBinary(0x82,21,40,1);
flg=0;
for(i=0;i<40;i++)
{
if(dat[i]!=RxdBuf[i])flg=1;
break;
}
if(flg==0)
I2cWrite(TaxpayerName,40,dat); //如果一致,更新纳税人单位名称
//-----------------------------------------------------------------------------------------
if(PwrFlg!=0)while(1);
UpdateTaxIndex(); //考虑拔卡,则本部分应该在发送完税命令前进行
//-----------------------------------------------------------------------------------------
if(PwrFlg!=0)while(1);
//查找与本机税控卡对应的数据文件
// ReadBinary(0x82,2,8,0);
// for(i=0;i<8;i++)str[i]=RxdBuf[i];
I2cRead(FiscalCardNo,8,str);
j=1;k=0;
while(1)
{
statecode=ReadRecord(0,j,0x0C,37,1); //读取用户卡的税控卡编号
if(statecode==0x6A83)
{
LcdDisplay(16,0,"未找到对应信息!",1);
LcdDisplay(32,0,"非本机用户卡!",1);
return 0;
}
for(i=0;i<8;i++)
if(RxdBuf[i] != str[i])
{
k=1;
}
if(k==0)break;
else {j++;k=0;}
}
for(i=0;i<29;i++)dat[i]=RxdBuf[i+8];
//------------------------------------------------------------------------------
if(PwrFlg!=0)while(1);
RxdBuf[0]=0xEF;
RxdBuf[1]=0x01;
SelectFile(0x00,0x00,0x02,RxdBuf,0);
if(PwrFlg!=0)while(1);
VerifyFiscalPin();
TxdBuf[0]=0xC0; //UpdateControls
TxdBuf[1]=0xF6;
TxdBuf[2]=0x00;
TxdBuf[3]=0x00;
TxdBuf[4]=0x1D; //29
for(i=0;i<0x1D;i++)
TxdBuf[5+i]=dat[i]; //0x1D+5=34
statecode=SimCmd(34,0);
if(statecode != 0x9000)
{
if(statecode==0x9303)AppBlockHint(0,1);
ErrorHint("税控卡监控数据更新出错!",statecode);
} //读取税种税目索引号、明细申报标志与税控码密钥标识符共23字节
ReadBinary(0x81,0,23,0); //读取开票截止日期、单张发票金额限额、发票累计金额限额、退票累计金额限额
for(i=0;i<23;i++)
dat[i]=RxdBuf[i];
I2cWrite(InvoiceEndData,23,dat);
// ReadBinary(0x82,46,8,0); //需要更新吗?
// for(i=0;i<8;i++)dat[i]=RxdBuf[i];
// I2cWrite(AppStartTime,8,dat); //更新应用启用日期、应用有效日期
dat[0]=0;
I2cWrite(DeclareFlg,1,dat); //更新报税标志
return 1;
}
void FiscalCashRegisterIni()
{
uchar i,dat[20],str[120];
uint statecode;
//----------------------------------------------------------------------------
UpdateTaxIndex();
UpdataUser();
//----------------------------------------------------------------------------
TxdBuf[0]=0xC0;
TxdBuf[1]=0xF0; //GET_REGISTER_NB
TxdBuf[2]=0x00;
TxdBuf[3]=0x00;
TxdBuf[4]=0x10;
statecode=SimCmd(5,0); //向税控卡发送
if(statecode != 0x9000)
{
if(statecode==0x9303)AppBlockHint(0,1);
ErrorHint("取税控卡编号出错!",statecode);
}
for(i=0;i<16;i++)dat[i]=RxdBuf[i];
if(PwrFlg!=0)while(1);
RxdBuf[0]=0xEF;
RxdBuf[1]=0x01;
SelectFile(0x00,0x00,0x02,RxdBuf,1);
if(PwrFlg!=0)while(1);
TxdBuf[0]=0xC0;
TxdBuf[1]=0xE4; //REGISTER_SIGN
TxdBuf[2]=0x00;
TxdBuf[3]=0x00;
TxdBuf[4]=0x10;
for(i=0;i<16;i++)
TxdBuf[5+i]=dat[i];
TxdBuf[21]=0x04;
statecode=SimCmd(22,1);
if(statecode != 0x6104)
{
if(statecode==0x9303)AppBlockHint(1,1);
ErrorHint("注册签字错误!",statecode);
}
statecode=GetResponse(4,1);
if(statecode != 0x9000)
{
ErrorHint("返回MAC2出错!",statecode);
}
for(i=0;i<4;i++)dat[i]=RxdBuf[i];
if(PwrFlg!=0)while(1);
TxdBuf[0]=0xC0;
TxdBuf[1]=0xF1; //TERMINAL_REGISTER
TxdBuf[2]=0;
TxdBuf[3]=0;
TxdBuf[4]=0x04;
for(i=0;i<4;i++)
TxdBuf[5+i]=dat[i];
statecode=SimCmd(9,0); //天喻
if(statecode != 0x6108)
{
if(statecode==0x9303)AppBlockHint(0,1);
ErrorHint("结束注册出错!",statecode);
}
statecode=GetResponse(8,0);
if(statecode != 0x9000)
{
ErrorHint("获取使用口令出错!",statecode);
}
for(i=0;i<8;i++)dat[i]=RxdBuf[i];
//---------------------------------------------------------------------
ReadBinary(0x82,0,103,0); //读取税控卡基本信息
for(i=0;i<103;i++)str[i]=RxdBuf[i];
//-----写入用户信息---------------------------------------------------
I2cWrite(FiscalCardNo,8,&str[2]); //写入税控卡编号
I2cWrite(FiscalPwd,8,dat); //8字节税控卡使用口令
I2cWrite(AppStartTime,8,&str[46]); //写入应用启用日期和有效日期
I2cWrite(TaxpayerName,40,&str[57]); //纳税人名称
I2cWrite(TaxpayerNo,28,&str[18]); //纳税人编码和税号
I2cWrite(ManagerCode,5,&str[97]); //主管分局代码和申报方式
//----------------------------------------------------------------------------------
I2cWrite(OriginalPwd,8,dat); //8字节税控卡使用口令,写入原始口令
//----------------------------------------------------------------------------------
ReadBinary(0x81,0,23,0);//读取开票截止日期、单张发票金额限额、发票累计金额限额、退票累计金额限额
for(i=0;i<23;i++)str[i]=RxdBuf[i];//读取明细申报标志与税控码密钥标识符共23字节
I2cWrite(InvoiceEndData,16,str);
I2cWrite(MingXiKeyFlg,1,&str[22]);
//----------------------------------------------------------------------------------
#if ClockChip
M41st87Ini(); //如果是M41ST87必须初始化
#endif
//---------------------------------------------------------------------
TimeSetup(1);
//---------------------------------------------------------------------
ReadDate(); //必须要重新读出时间,否则没有“世纪”
I2cWrite(DecBeginDate,4,InvoDate); //申报数据起始日期
I2cWrite(IntradayTime,4,InvoDate); //当天日期
//---------------------------------------------------------------------
dat[0]=0x04;
I2cWrite(MacState,1,dat);
//---------------------------------------------------------------------
}
uchar InvoiceProtect()
{
uchar i,f,k,dat[24];
uint adr;
I2cRead(DistriError,1,&i);
if(i==0)return 1; //分发过程中出现异常才检查临时文件
I2cRead(InvoiceNum,1,&i);
ReadBinary(0x87,0,22,1);
if(PwrFlg!=0)while(1);
// f=0;
// for(k=0;k<22;k++)
// if(RxdBuf[k]!=0)f=1; //分发不成功时,数据是零
// if(f==0)return 1; //用户卡还没有分发过发票
I2cRead(DistriInvo,22,dat);
f=0;
for(k=0;k<22;k++)
if(RxdBuf[k]!=dat[k])
{f=1;break;}
if(f)
{
adr=InvoInfo+i*22;
I2cWrite(adr,22,RxdBuf);
i++;
I2cWrite(InvoiceNum,1,&i);
}
i=0;
I2cWrite(DistriError,1,&i);
return 1;
}
uchar DistributeInvoice(uchar x)
{//分发用户卡中的发票,是一次将所有购买的发票分发完毕还是由用户选择分发???
uint statecode,adr;
uchar i,dat[25],str[]="已经分发 卷发票!";
InvoiceProtect();
RxdBuf[0]=0xEF;
RxdBuf[1]=0x05;
SelectFile(0x00,0x00,0x02,RxdBuf,1);
if(PwrFlg!=0)
PowerOffHint(" ","分发发票未完成!",0xAA);
TxdBuf[0]=0xC0;
TxdBuf[1]=0xE9; //Distribute_Invoice_NB
TxdBuf[2]=0x00;
TxdBuf[3]=0x00;
TxdBuf[4]=0x16;
statecode=SimCmd(5,1);
// if(SimType==SimWoQi)
// {
// if(statecode != 0x6116)
// {
// ErrorHint("渥奇用户卡分发发票出错!",statecode);
// }
// statecode=GetResponse(0x16,1);
// }
if(statecode != 0x9000)
{
i=1;
I2cWrite(DistriError,1,&i);
if(statecode==0x9303)AppBlockHint(1,1);
ErrorHint("用户卡分发发票异常!",statecode);
}
for(i=0;i<22;i++)dat[i]=RxdBuf[i];
I2cRead(InvoiceNum,1,&i);
adr=i*22+InvoInfo;
I2cWrite(adr,22,dat);
I2cWrite(DistriInvo,22,dat); //将本次成功分发的数据写入发票分发临时文件
i++;
I2cWrite(InvoiceNum,1,&i);
if(PwrFlg==1)
{
if(x<10)str[8]=x+0x31;
else{str[8]='1';str[9]=x+0x26;} //x-10+'0' //注意最多10卷发票
PowerOffHint(" ",str,0xAA);
}
return 1;
}
uchar InputInvoice(uchar *dat)
{//录入已分发发票
uchar i;
uint statecode;
gyt4 hp;
if(!FiscalIdentify(1))
{
LcdDisplay(48,0,"按任意键继续!",0);
KeyScan();
return 0;
}
RxdBuf[0]=0xEF;
RxdBuf[1]=0x05;
SelectFile(0x00,0x00,0x02,RxdBuf,0);
if(PwrFlg==1)
PowerOffHint(" ","录入发票未完成!",0xAA);
VerifyFiscalPin();
TxdBuf[0]=0xC0;
TxdBuf[1]=0xF7; //Input_Invoice_NB
TxdBuf[2]=0x00;
TxdBuf[3]=0x00;
TxdBuf[4]=0x16;
for(i=0;i<22;i++)
TxdBuf[5+i]=dat[i];
statecode=SimCmd(27,0); //向税控卡发送
if(statecode != 0x9000)
{
if(statecode==0x9303)AppBlockHint(0,1);
ErrorHint("税控卡录入发票出错!",statecode);
}
I2cWrite(InvoiceCode,18,dat); //保存新的发票代码和单卷发票起始号和终止号
hp.dat=0; //清零废票数量
I2cWrite(UntreadNum,1,hp.str);
ReadDate();
I2cWrite(TomeTime,4,InvoDate); //更新开票起始时间
for(i=0;i<11;i++)dat[i]=0; //清零单卷发票使用汇总数据
I2cWrite(TomeNum1,11,dat); //开票起始时间已经更新,不能清零
SimDeactive(0);
return 1;
}
void IssueInvoice(uchar *dat)
{//生成发票税控码,更新日交易记录;执行前需要核对税控卡使用口令????
uchar i,k;
uint statecode;
RxdBuf[0]=0xEF;
RxdBuf[1]=0x03;
SelectFile(0x00,0x00,0x02,RxdBuf,0);
VerifyFiscalPin();
TxdBuf[0]=0xC0;
TxdBuf[1]=0xF2; //IssueInvoice
TxdBuf[2]=0x00;
TxdBuf[3]=0x00;
TxdBuf[4]=0x2C;
k=0;
for(i=0;i<43;i++)
{
k ^= dat[i];
TxdBuf[5+i] = dat[i];
}
TxdBuf[48]=k;
TxdBuf[49]=0x08;
statecode=SimCmd(50,0);
if(statecode != 0x6108)
{
if(statecode==0x6A85)ErrorHint("税种税目找不到!",statecode);
if(statecode==0x9303)AppBlockHint(0,1);
if(statecode==0x9401)
{
if(TxdBuf[9]==1)ErrorHint("超出发票累计金额限额!",statecode);
else ErrorHint("超出退票累计金额限额!",statecode);
}
else ErrorHint("生成发票税控码出错!",statecode);
}
statecode=GetResponse(8,0);
if(statecode != 0x9000)
{
ErrorHint("获取税控码出错!",statecode);
}
for(i=0;i<8;i++) //得到8字节税控码
dat[i]=RxdBuf[i];
}
void DeclareDataStat(ulong start,ulong end,uchar *str)
{//要先做日交易签名再统计申报数据
uint i;
ulong adr,gs;
gyt4 *xp,*wp,*mp;
gyt2 hp,*fp,*tp;
uchar k,x,y,ms[66];
xp=&str[0];
xp->dat = start;
xp=&str[4];
xp->dat = end;
I2cRead(DailyDatNum,2,hp.str);
gs = hp.dat-1;
adr = DailyTradeDat + gs*192;
for(i=0;i<hp.dat;i++)
{
SerialFlashRead(FlashDat,adr,64,ms); //电子签名不需要读出
mp=&ms[0];
if(mp->dat < start)break; //如果日交易数据的日期小于申报起始日期,结束查询
if(mp->dat>=start && mp->dat<=end)
{
wp=&str[8];
fp=&ms[4];
wp->dat += fp->dat; //累加正常发票份数
tp=&str[12];
fp=&ms[6];
tp->dat += fp->dat; //累加退票份数
tp=&str[14];
fp=&ms[8];
tp->dat += fp->dat; //累加废票份数
x=16;y=22;
for(k=0;k<12;k++)
{
xp=&ms[x];
wp=&str[y];
wp->dat += xp->dat; //累加正常发票分类累计金额和退票分类累计金额
x+=4;y+=4;
}
}
if(adr<=DailyTradeDat)
break;
else
adr-=192;
}
I2cRead(IndexItems,6,&str[16]);
}
void DeclareDuty(ulong time,uchar mode)
{//用于生成申报数据,执行前需要效验使用口令
uchar i,k,tp[150],dat[80];
uint statecode,adr;
gyt4 *xp,*fp,wp;
ulong wc;
//=======================================================================================
//处理日交易签名
AutoDailySign();
//=======================================================================================
memset(tp,0,sizeof(tp));
memset(dat,0,sizeof(dat));
if(mode!=0)
{//已经报过税了,还没有完税,用补发的用户卡报税,不用截止日期
I2cRead(DeclareNum,1,&k); //如果k=0,DeclareFlg不可能=1;flg=1时,k肯定大于0
I2cRead(DeclareFull,1,&i);
if(i==1 && k==0)
k=DeclareBakMax; //如果申报备份数据刚好写满
adr=DeclareBak+(k-1)*83;
I2cRead(adr,70,dat); //从申报备份数据读出申报数据
}
else
{//统计申报数据
I2cRead(Declare,4,wp.str);
DeclareDataStat(wp.dat,time,dat); //按输入截止日期统计报税数据
}
//=======================================================================================
RxdBuf[0]=0xEF;
RxdBuf[1]=0x04;
SelectFile(0x00,0x00,0x02,RxdBuf,0);
if(PwrFlg!=0)while(1);
VerifyFiscalPin();
if(PwrFlg!=0)while(1);
TxdBuf[0]=0xC0;
TxdBuf[1]=0xF4; //DeclareDuty
TxdBuf[2]=0x00;
TxdBuf[3]=0x00;
TxdBuf[4]=0x47;
k=0;
for(i=0;i<70;i++)
{
k ^= dat[i];
TxdBuf[5+i] = dat[i];
}
TxdBuf[75]=k;
TxdBuf[76]=0x8E;
statecode=SimCmd(77,0);
if(statecode != 0x618E)
{
if(statecode==0x9303)AppBlockHint(0,1);
ErrorHint("生成申报数据出错!",statecode);
}
statecode=GetResponse(0x8E,0);
if(statecode != 0x9000)
{
ErrorHint("返回申报数据出错!",statecode);
}
//是否要检测效验和的正确性?
for(i=0;i<0x8D;i++)tp[i]=RxdBuf[i];
RxdBuf[0]=0xEF;
RxdBuf[1]=0x04;
SelectFile(0x00,0x00,0x02,RxdBuf,1);
I2cRead(FiscalCardNo,8,RxdBuf); //读出税控卡编号
TxdBuf[0]=0xC0;
TxdBuf[1]=0xE6; //Data_Collect
TxdBuf[2]=0x00;
TxdBuf[3]=0x00;
TxdBuf[4]=0xDC;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -