⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pc_comm.c

📁 一款收款机C源代码!因为是几年前的代码了
💻 C
📖 第 1 页 / 共 5 页
字号:
/******************************************************************************
 * Module: PC_Comm.C
 * Modified by: X.C.Zheng WeiHua
 * Modified on: Date: 2003-12-8 11:50
 * Copyright(c) WeiHua Tech Ltd.
 * Description:	Communication with PC.
 *	Note: In the communication function the ECR only act as a slave function.
 *	!!!	This function must work in the restaurant function and scanner function !!!
******************************************************************************/

#include "PC_Comm.h"
#include "ecrsys.h"
#include "keydef.h"
#include "data.h"
#include "sysdata.h"
#include "ftype.h"
#include "disp.h"
#include "string.h"
#include "mathes.h"


#ifdef PC_COMM					/* Support the PC communication function */

//#define	COMM_PORT					PORT0				/* use the port0 for PC communication */
#define	DATA_AREA_LEN				1024			/* The max data area length */
#define	COMM_DEPT_NUM				6					/* The department number in one communication */
#define	COMM_PLU_NUM				5					/* The PLU number in one communication */
#define	COMM_CLERK_NUM				5					/* The clerk number in one communication */
#ifdef VER_RSNT		/* Support the restaurant function */
#define	COMM_TABLE_NUM				5					/* The table number in one communication */
#define	COMM_WAITER_NUM			5					/* The waiter number in ont communication */
#endif /* End VER_RSNT */
#define	COMM_PLU_INDEX_NUM		100				/* The PLU index number in one communication */
#define	SYS_PARA_LEN				21+6					/* The system parameter transaction length */
#define	RAM_LOGO_DWN_TIMES		30					/* RAM logo download times */
#define	RAM_LOGO_DWN_SIZE			240				/* The RAM logo download size */
#define	COMM_REGI_DESC_NUM		3					/* The communication register descriptor number */
#ifdef	ELEC_JRNL
#define	COMM_JRNL_NUM				20					/* The communication electronic journal number */
#define	COMM_JRNL_NUM2				5					/* The communication electronic journal(non-condensing format) number */
#define	COMM_JRNL_LEN2				34					/* The 32 printable data and the end tag(0x0D, 0x0A) */
#endif


byte clr_PLU_flag;		/* The PLU report's cleared flag */	
byte Comd_Type;			/* Command type */
byte Data_Len;				/* The data area's length */
word Rpt_Data_Len;		/* Communicate the report's data length */
byte Data_Area[DATA_AREA_LEN];		/* The data area */
byte Check_Sum;			/* The data area's check sum */
byte Send_Buff[6+1+1+DATA_AREA_LEN+1];			/* The send information buffer */
//int  Sindex_Bak[MAX_PLU_CNT];			/* The backup of the PLU index table, when receive the PLU index
//													can receive the all index then update the index table. */
//int  Sngl_PLU_Index_Bak[PLU_RANGE_DWN_LEN];			/* When down the single PLU parameter, first 
//																		record the number, when exit the communication, 
//																		then update the PLU index table
//																		*/
#ifdef SCANNER 		/* Support the scanner function */
byte signal = 0;			/* The flag of the PLU index table, PLU communication */
//int  OldPosi[PLU_RANGE_DWN_LEN];		/* Save the old PLU position in the index table */
//int  NewPosi[PLU_RANGE_DWN_LEN];		/* Save the new PLU position in the index tabble */
//int  Length;			/* Index the length of the above array */
#endif /* End SCANNER */
#ifdef	ELEC_JRNL
static word		last_sale_jrnl_up_idx;			/* The last sale journal up-load index */
static byte		*last_sale_jrnl_up_ptr;			/* The last sale jounrla up-load pointer address */
static word		last_dc_jrnl_up_idx;				/* The last discount journal up-load index */
static byte		*last_dc_jrnl_up_ptr;			/* The last discount journal up-load pointer address */
static word		last_rm_jrnl_up_idx;				/* The last return merchandise journal up-load index */
static byte		*last_rm_jrnl_up_ptr;			/* The last return merchandise journal up-load address */

static byte		elec_jrnl_send_idx;				/* The send buffer index length */
#endif

//#define	comm_disp_port()	{\
//	if (COMM_PORT == PORT0)\
//		Insert_Num(1, 9+6);\
//	else\
//		Insert_Num(2, 9+6);\
//}

/******************************************************************************/
void Communication(void)
{
	while(CheckSdBufEmpty(COMM_PORT));
		// 在退出这个程序的时候, 由于端口会被复位, 所以这个地方需要进行等待一下. 2004-9-28 16:12
	PC_Comm_Port_Init(0);
	PC_Comm_Para_Init();				/* Ininialize the parameter, such as the backup index table */
	if(HandShake() == NOTOK)		/* When the handshake is terminal, reurn */
	{
		PC_Comm_Exit_Send();			/* Send the exit command to PC */
		PC_Comm_Restore();			/* Restore the serial port's data */
		bellcnt = 120;
		return;
	}
	while(TRUE)
	{
		GetMainMode();
		if(MainMode != CurrMode)
			break;
		VFDDisplay(Disp_Comm_Type,NOTCLEARD,LEFT);
//		comm_disp_port();

		if(Get_Data() == NOTOK)
			break;
		if(Comd_Type == ECHO_EXIT_COMM)
		{
			if((Data_Len == 1) && (Check_Sum == 0) && (Data_Area[0] == 0))
				break;
			else
				Send_Error_Info(NOT_MATCH_FORMAT);
		}
		else if(((Comd_Type >= 0x01) && (Comd_Type <= 0x10)) || ((Comd_Type >= 0xA1) && (Comd_Type <= 0xC0)))
			Read_Data_PC();
		else if(((Comd_Type >= 0x31) && (Comd_Type <= 0x60)) || ((Comd_Type >= 0xC1) && (Comd_Type <= 0xE4)))
			Send_Data_PC();
	}
	#ifdef SCANNER 		/* Support the scanner function */
	if(signal == 1)			/* When download the PLU index table, but not send the PLU information, 
										prompt an error to the user */
		errorType(ERR_PLU_DOWN_INT);
///	if(Length != 0)				/* When send the single PLU parameter */
///	{
///		Sngl_PLU_Index_Update();
///	}
	#endif /* End SCANNER */
	PC_Comm_Exit_Send();			/* Send the exit command to PC */
	PC_Comm_Restore();			/* Restore the serial port's data */
	bellcnt = 120;
}


/*----------------------------------------------------------------------*
				Initialize the PC communication port.
 *	input	:	0 for start the communication with PC send auto handshake data
 * 		: 	1 for end the communication, to set the new port set option.
 *----------------------------------------------------------------------*/
void PC_Comm_Port_Init(char flag)
{
	if ( flag == 0 )							/*Before comm. */
	{
		ClrRsBuf(COMM_PORT);					/* Clear the receive buffer */
		ClrWrBuf(COMM_PORT);					/* Clear the send buffer */
//		tmp_port_sel1 = port_sel1;
//		tmp_port_sel2 = port_sel2;
	}
	else if (flag == 1)						/*Set the port default value*/
	{
		while(CheckSdBufEmpty(COMM_PORT));/*wait buffer empty*/
//		port_sel1 = tmp_port_sel1;			 /*Update with new option*/
//		port_sel2 = tmp_port_sel2;			 /*Update with new option*/
//		do_port_mean_inverse_convert();
	}
}


/*----------------------------------------------------------------------*
				Initialize the PC communication parameter.
 *----------------------------------------------------------------------*/
void PC_Comm_Para_Init(void)
{
	int j;

	VFDDisplay(Disp_Comm_Wait,NOTCLEARD,LEFT);
//	comm_disp_port();
//	#ifdef SCANNER 		/* Support the scanner function */
//	Length = 0;
//	#endif /* End SCANNER */
	clr_PLU_flag = 0;
//	for(j = 0; j < MAX_PLU_CNT; j ++)
//	{
//		Sindex_Bak[j] = MAX_PLU_CNT;			/* Set it a null value */
//	}
	#ifdef	ELEC_JRNL
	last_sale_jrnl_up_idx = 0;
	last_sale_jrnl_up_ptr = sale_jrnl_first_ptr;
	last_dc_jrnl_up_idx = 0;
	last_dc_jrnl_up_ptr = dc_jrnl_first_ptr;
	last_rm_jrnl_up_idx = 0;
	last_rm_jrnl_up_ptr = rm_jrnl_first_ptr;

	Clr_EJ_Para();
	#endif
}


/*-------------------------------------------------------------------*
					Send the exit information to the PC.
 *-------------------------------------------------------------------*/
void PC_Comm_Exit_Send(void)
{
	Write_Head();
	Uart_WriteByte(COMM_PORT,ECHO_EXIT_COMM);
	Uart_WriteByte(COMM_PORT,0x01);
	Uart_WriteByte(COMM_PORT,0x00);
	Uart_WriteByte(COMM_PORT,0x00);
//	SPLU_Index_Update();						/* Update the PLUs bar code */

	while(CheckSdBufEmpty(COMM_PORT));
		// 在退出这个程序的时候, 由于端口会被复位, 所以这个地方需要进行等待一下. 2004-9-28 16:12
}


/*--------------------------------------------------------------------*
					Restore the serial ports data.
 *--------------------------------------------------------------------*/
void PC_Comm_Restore(void)
{
	PC_Comm_Port_Init(1);		/*restore the port 0's original option*/
}


/*--------------------------------------------------------------------------*
					Hand shake with the PC.
ECR S/W Version No. --> ECR status --> PLU Number --> Index --> Table number
 *--------------------------------------------------------------------------*/
#define	HAND_DATA_LEN			20			/* The handshakes data length */
byte HandShake(void)
{
	word *p;

	while(TRUE)
	{
		if(Get_Data() == NOTOK)
			return (NOTOK);
		if((Comd_Type == HAND_SHAKE) && (Data_Len == 0) && (Check_Sum == 0))			/* The PC sended head */
		{
			Write_Head();
			Send_Buff[0] = HAND_SHAKE;
			Send_Buff[1] = HAND_DATA_LEN;
			p = (word *)(Send_Buff + 2);
			*p = Version;
			Send_Buff[4] = 0x00;
			#ifdef VER_RSNT 			/* Support the restaurant function */
			if(work_mode == RETAIL)
				Send_Buff[5] = 0;		/* Retail mode */
			else
				Send_Buff[5] = 1;		/* Restaurant mode */
			#else /* Else VER_RSNT */
				Send_Buff[5] = 0;
			#endif /* End VER_RSNT */
			if(sysflag->Tax_System)
				Send_Buff[5] |= 0x02;
			Send_Buff[5] |= (sysflag->sysdots << 2);
			p = (word *)(Send_Buff + 6);
			*p ++ = Max_Dept_No;
			*p ++ = Max_Clerk_No;
			*p ++ = Max_Plu_No;
			#ifdef VER_RSNT			/* Support the restaurant function */
				*p ++ = Max_Table_No;
				*p ++ = Max_Waiter_No;
			#else /* Else VER_RSNT */
				*p ++ = 0;
				*p ++ = 0;
			#endif /* End VER_RSNT */
			#ifdef ELEC_JRNL			/* Support the electronic journal function */
				*p ++ = sale_jrnl_len;
				*p ++ = dc_jrnl_len;
				*p ++ = rm_jrnl_len;
			#else /* Else VER_RSNT */
				*p ++ = 0;
				*p ++ = 0;
				*p ++ = 0;
			#endif /* end ELEC_JRNL */

			CalCS(Send_Buff+2, HAND_DATA_LEN, &Check_Sum);
			Send_Buff[2+HAND_DATA_LEN] = Check_Sum;
			Wr_Str_Uart(COMM_PORT,Send_Buff,2+HAND_DATA_LEN+1);
			return (OK);
		}
		else if(Comd_Type == ECHO_EXIT_COMM)		/* Exit the communication */
		{
			if((Data_Len == 1) && (Check_Sum == 0) && (Data_Area[0] == 0))
				return (NOTOK);
		}
		else if(Comd_Type == ECHO_STATUS)			/* ECHO the status */
		{
			if((Data_Len == 0) && (Check_Sum == 0))
			{
				void Comm_LinkCmdReply(byte port, byte status);
				Comm_LinkCmdReply(COMM_PORT, STATUS_COMM);
			}
		}
	}
	return (OK);
}


/*---------------------------------------------------------------------*
					Verify the check sum si right or not.
 *---------------------------------------------------------------------*/
byte Verify_CS(byte *vfy_str, word length, byte check_sum)
{
	byte sum;
	word i;

	sum = 0;
//	while(length --)
//	{
//		sum += vfy_str[length];
//	}
	for(i = 0; i < length; i ++)
	{
		sum += vfy_str[i];
	}
	if(sum == check_sum)
		return (OK);
	else
		return (NOTOK);
}


/*----------------------------------------------------------------------*
					Read the start head sended from the PC side.
					Only read 6 0xfe is OK,
 *----------------------------------------------------------------------*/
byte Read_Head(void)
{
	byte cnt;					/* The counter of 0xFE received */
	byte read_byte;

	cnt = 0;

	while(cnt != 6)			
			/* When received 6 0xFE, return, if the data more than 6 0xFE, 
				it will dealed in the function Read_Start_Head */
	{
		if(Uart_ReadByte(COMM_PORT,&read_byte) == NOTOK)
			return (NOTOK);
		if(Chk_KeyPrs_LockChg(0) == NOTOK)
			return (NOTOK);
		if(read_byte == 0xFE)
			cnt ++;
		else
			cnt = 0;
	}
	return (OK);
}


/*-----------------------------------------------------------------------*
				Get the data sended from the PC side.
 *-----------------------------------------------------------------------*/
#define LOOPCNT			6				/* The loop counter, for the serial's time over is 0.5s, 
												the communication's time over is 3s */
byte Get_Data(void)
{
	byte flag;
//	byte i;
	byte j;
	word i;

	while(TRUE)
	{
		flag = 0;
		/***********************************/
		if(Read_Head() == NOTOK)					/* Read the communication data */
		{
			if(Chk_KeyPrs_LockChg(1) == NOTOK)			/* When a key pressed or control lock changed return */
				return (NOTOK);
			else 
				continue;
		}

		/***********************************/
		for(j = 0; j < LOOPCNT; )
		{
			if(Chk_KeyPrs_LockChg(1) == NOTOK)
				return (NOTOK);
			while(TRUE)
			{
				if(Uart_ReadByte(COMM_PORT,&Comd_Type) == NOTOK)			/* Get the comamnd type */
				{
					flag = 1;
					break;
				}
				if(Comd_Type == 0xFE)		/* If is the 0xFE, indicate is the head(more than 6 0xFE) */
					continue;
				else
					break;
			}
			if(flag == 0)			/* Read the right byte, break */
				break;
			j ++;
		}
		if(j == LOOPCNT)				/* Not received the demand data, continue */
		{
			Send_Error_Info(COMM_TIME_OVER);
			continue;
		}

		/***********************************/
		for(j = 0; j < LOOPCNT; )
		{
			if(Chk_KeyPrs_LockChg(1) == NOTOK)
				return (NOTOK);
			if(Uart_ReadByte(COMM_PORT,&Data_Len) == OK)				/* Get the data's length */
				break;
			j ++;
		}
		if(j == LOOPCNT)
		{
			Send_Error_Info(COMM_TIME_OVER);
			continue;
		}
		if((Comd_Type >= 0x40) && (Comd_Type < 0x60))				/* The report communication, the data length occupy 2 bytes */
		{
			Rpt_Data_Len = Data_Len;
			for(j = 0; j < LOOPCNT; )
			{
				if(Chk_KeyPrs_LockChg(1) == NOTOK)
					return (NOTOK);
				if(Uart_ReadByte(COMM_PORT,&Data_Len) == OK)
					break;
				j ++;
			}
			if(j == LOOPCNT)
			{
				Send_Error_Info(COMM_TIME_OVER);
				continue;
			}
			*(((byte *)(&Rpt_Data_Len)) + 1) = Data_Len;

			/***********************************/
			for(i = 0; i < Rpt_Data_Len; i ++)			/* The Rpt_Data_Len can be 0 */
			{
				for(j = 0; j < LOOPCNT; )
				{
					if(Chk_KeyPrs_LockChg(1) == NOTOK)
						return (NOTOK);
					if(Uart_ReadByte(COMM_PORT,Data_Area+i) == OK)
						break;
					j ++;
				}
				if(j == LOOPCNT)
				{
					flag = 1;
					Send_Error_Info(COMM_TIME_OVER);
					break;
				}
			}
			if(flag == 1)
				continue;
	
			/***********************************/
			for(j = 0; j < LOOPCNT; )
			{
				if(Chk_KeyPrs_LockChg(1) == NOTOK)
					return (NOTOK);
				if(Uart_ReadByte(COMM_PORT,&Check_Sum) == OK)				/* Get the data's check sum */
					break;
				j ++;
			}
			if(j == LOOPCNT)
			{
				Send_Error_Info(COMM_TIME_OVER);
				continue;
			}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -