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

📄 prntproc.c

📁 基于ARM与μCOS-II的嵌入式操作系统实现针式和热敏微型打印程序.
💻 C
字号:

#include "intfBsp.h"
#include "intfPrnt.h"


#include "prntMain.h"
#include "prntProc.h"

/*------------------ ----全局变量定义-- ----------------------*/
UINT2 g_usPatternCount = 0;  			 /* 坐标定位横坐标相对字模计数点 */
UINT1 g_ucMarkCutPaper = 0;				 /* 热敏打印解析打印命令含走纸到打印切纸标志 
											(0 不含此命令  1含有此命令)  */
UINT1 g_ucPrntOrderMode = 0;			 /* 票据打印对齐方式(0 左对齐、1右对齐)*/
UINT1 g_ucPrntChoiceLang = 0;			 /* 选择打印语言标志(0 中文、1阿拉伯语、2英语)*/								
/*------------------ ----静态全局变量定义-- ------------------*/
static UINT2 s_usPageLength = 0;		 /* 票据页长 (单位:步数)*/
static UINT2 s_usCutPaperLen = 0;		 /* 票据切纸位置 (单位:步数)  */
static UINT1 s_ucDealComBuffer[30];		 /* 解析打印命令处理缓冲区    */
static UINT1 s_ucDealCmdIn = 0;			 /* 命令缓冲区解析数据计数    */
static UINT2 s_usDealDataIn = 0;		 /* 数据缓冲区解析数据计数    */
static UINT1 s_ucCmdContinue = 0;		 /* 是命令后续字节(0不是;1是)*/
static UINT1 s_ucBitMapPrnt = 0;		 /* 存在位图打印请求(0无,1有)*/			 
/*------------------------------------------------------------*/
/*
*********************************************************
*
* 函数名:prntDotPrintData
*
* 功  能:穿孔打印一段数据函数;
*
* 输  入:g_ucprntLinePrinted			
*
* 输  出:
*
* 返  回: FAULT   处理失败
*		  SUCCESS 处理成功
*
*********************************************************
*/
INT1 prntDotPrintData
(
	UINT1 *_pData,         /* [in]待打印数据 */
	UINT2  _usLength        /* 待打印数据长度 */
)
{
	INT1 iRet;
	if (_usLength == 0)
	{
		return FAULT;
	}
	g_ucprntLinePrinted = 0;
	iRet = prntDotPrintGeneralFormat (_pData, _usLength);
	if (iRet == FAULT)
	{
		return FAULT;
	}
	return SUCCESS;
}

/*
*********************************************************
*
* 函数名:prntTSPrintData
*
* 功  能:利用热敏机芯打印一块数据主函数;
*
* 输  入:g_ucPaperType
*		  g_ucPrinterLineSpace
*		  s_usPageLength
*		  s_usCutPaperLen
*
* 输  出:无
*
* 返  回: FAULT   处理失败
*		  SUCCESS 处理成功
*
*********************************************************
*/
INT1 prntTSPrintData
(
	UINT1 *_pData,         /* [in]待打印数据 */
	UINT2  _usLength       /* 待打印数据长度 */
)
{
	if (_usLength == 0)
	{
		return FAULT;
	}
	/* 当前打印机状态异常不执行打印操作直接返回打印机状态 */
	if (g_ucprntState&0x0f)
	{
		return FAULT;
	}
	if (_pData[0] == 0x0C)    /* 进纸到打印起始位置 */
	{
		bspTSPrntGoToBeginPlace(g_ucPaperType);
		bspTSPrntData(&_pData[1], _usLength - 1 , g_ucPrinterLineSpace, 0);
	}
	else
	{
		bspTSPrntData(&_pData[0], _usLength, g_ucPrinterLineSpace, 0);
	}
	if (g_ucMarkCutPaper)
	{
		g_ucMarkCutPaper = 0;
		/* 走纸到打印切纸位置 */
		bspTSPrntGoToCutPlace (g_ucPaperType, s_usPageLength, s_usCutPaperLen);
	}
	
	return SUCCESS;
}

/*
*********************************************************
*
* 函数名:prntDotPrintGeneralFormat
*
* 功  能:穿孔打印基本打印数据解析、处理函数
*
* 输  入:s_ucBitMapPrnt		
*
* 输  出:s_ucCmdContinue
*		  s_usDealDataIn
*
* 返  回: FAULT   处理失败
*		  SUCCESS 处理成功
*
********************************************************
*/
INT1 prntDotPrintGeneralFormat
(
	UINT1 *_pData,         /* [in]待打印数据 */
	UINT2  _usLength       /* 待打印数据长度 */
)
{
	UINT2 usBitMapCount = 0;
	if ((_pData== NULL)||(_usLength == 0))
	{
		return FAULT;
	}

	s_usDealDataIn = 0;
	/* 解析打印数据 */
	while (s_usDealDataIn < _usLength)
	{
		/* 系统掉电 */
		if (g_syscPowOff == SUCCESS) 
		{
			g_ucprntState = g_ucprntState |LOSEPOWER;
		}
		if (g_ucprntState&0x0f)
		{
			break; /* 打印机状态异常 */
		}
		/* 打印命令数据解析处理 */
		prntDealCommand(_pData);
		if (s_ucBitMapPrnt)/* 位图数据 */
		{
			/* 存取打印位图数据字模 */
			usBitMapCount = _pData[s_usDealDataIn]+ 256*_pData[s_usDealDataIn + 1];
			s_usDealDataIn += 2;
			bspDotPrntBitMap(_pData + s_usDealDataIn, usBitMapCount);
			s_usDealDataIn += usBitMapCount;
			s_ucCmdContinue = 0;
			s_ucDealCmdIn = 0;
		}
	}
	return SUCCESS;
}

/*
*********************************************************
*
* 函数名:prntDealCommand
*
* 功  能:打印命令、数据解析处理
*
* 输  入:g_ucPaperType
*		  s_usPageLength
*         s_usCutPaperLen
*         g_usPatternCount
*
* 输  出:s_ucBitMapPrnt
*		  s_usDealDataIn
*		  s_ucDealCmdIn
*		  s_ucCmdContinue
*		  g_ucPrinterLineSpace
*
* 返  回: N/A
*
********************************************************
*/
void prntDealCommand
(
	UINT1 *_pData   /* [in]待解析打印数据 */
)
{
	if (!s_ucCmdContinue)		   /* 不是命令后续字节*/
	{							
		if (prntIsComHead(_pData)) /* 不是打印命令头 */
		{	
			/* 打印横向坐标定位当前字符对应相应区域坐标点 */
			bspDotPrntOrientation(&_pData[s_usDealDataIn]);
			/* 存取数据到打印缓冲区 */
			bspDotprntPutDataToBuff (&_pData[s_usDealDataIn], 1);
			s_usDealDataIn ++;
			return;
		}
	}
	
	/* 是命令后续字节或是命令头 */	
	s_ucDealComBuffer[s_ucDealCmdIn] = _pData[s_usDealDataIn];
	s_ucDealCmdIn ++;
	s_usDealDataIn ++;
	
	switch (s_ucDealCmdIn)
	{
		case 1:
			switch (s_ucDealComBuffer[0])
			{
				case 0x0e:/* 换页打印操作 */
					bspDotPrntGoToCutPlace (g_ucPaperType, s_usPageLength, s_usCutPaperLen);
					bspDotPrntGoToBeginPlace(g_ucPaperType); 
					break;

				case 0x0c:/* 走纸到打印起始位置命令 */
					/* 走纸到打印起始位置 */
					bspDotPrntGoToBeginPlace(g_ucPaperType);
					break;
				#if 0	  /* 倍宽、倍高命令改为底层解析处理 */
				case 0x0b:/* 设置倍宽打印 */
					break;	
				case 0x14:/* 取消倍宽打印 */
					break;
				#endif
				
				case 0x0a:
				case 0x0d:
					if ((s_ucDealComBuffer[0] == 0x0A)&&(_pData[s_usDealDataIn - 2] == 0x0d))
					{
						;/* 命令0x0d 0x0a表示单次打印换行操作 */
					}
					else
					{
						g_usPatternCount = 0;
						if (s_ucBitMapPrnt) /* 打印单行位图数据 */
						{
							s_ucBitMapPrnt = 0;
							bspDotprntPrintOneLine(0);
							bspDotPrntPFLineSpace(1, g_ucPrinterLineSpace - 1);
						}
						else
						{
							bspDotprntAndNewline(1, g_ucPrinterLineSpace);
						}
					}
					break;

				case 0x1b:/* 位图、行间距命令 */
				case 0x1d:/* 走纸到切纸位置 */
				case 0xff:/* 定位打印绝对坐标 */
					s_ucCmdContinue = 1;
					return;

				default:
					break;
			}
			s_ucCmdContinue = 0;
			s_ucDealCmdIn = 0;
			return;

		case 2:
			switch (s_ucDealComBuffer[1])
			{
				case 0x56:
					/* 走纸到打印切纸位置 */
					bspDotPrntGoToCutPlace (g_ucPaperType, s_usPageLength, s_usCutPaperLen);
					break;
					
				case 0x33:/* 设置行间距 */
					s_ucCmdContinue = 1;
					return;
					
				case 0x4c:/* 位图命令 */
					s_ucCmdContinue = 1;
					s_ucBitMapPrnt = 1;
					return;
					
				default:
					/* 坐标定位命令 */
					if (s_ucDealComBuffer[0] == 0xff)
					{
						s_ucCmdContinue = 1;
						return;
					}
					break;
			}
			s_ucCmdContinue = 0;
			s_ucDealCmdIn = 0;
			return;

		case 3:
			switch (s_ucDealComBuffer[1])
			{
				case 0x33:/* 设置行间距 */
					g_ucPrinterLineSpace = s_ucDealComBuffer[2];
					break;

				default:
					if (s_ucDealComBuffer[0] == 0xff)
					{
						/* 横向坐标确定点 */
						bspDotSetWidthCoordinate(s_ucDealComBuffer[1],g_usPatternCount);
						/* 进纸到绝对打印位置 */
						if (!g_ucPaperType)
						{
							/* 不定长票据相对纵坐标定位 */
							prntFeedPaparToFixedPosition(s_ucDealComBuffer[2]);
						}
						else
						{
							/* 定长票据绝对坐标定位 */
							prntFeedPaparToYCoordinate(s_ucDealComBuffer[2]);
						}
					}
					break;
			}
			s_ucCmdContinue = 0;
			s_ucDealCmdIn = 0;
			return;
			
		default:
			s_ucCmdContinue = 0;
			s_ucDealCmdIn = 0;
			return;
	}
}

/*
*******************************************************
*
* 函数名:prntIsComHead
*
* 功  能:判断单字节数据是否打印命令头
*
* 输  入:s_ucDealCmdIn
*
* 输  出:无
*
* 返  回:0 是打印命令头
*		  1 非打印命令头
*
*******************************************************
*/
UINT1 prntIsComHead
(
	UINT1 *_pData   /* [in]待解析打印数据 */
)
{
	switch (_pData[s_usDealDataIn])
	{
		case 0x0E:	/* 换页 */
		case 0x0A:	/* 打印换行 */
		case 0x0D:	
		//case 0x0b:  /* 设置倍宽打印 */
		//case 0x14:  /* 取消倍宽打印 */
		case 0x05:	/* 查询打印状态 */
		case 0x0C:	/* 进纸到打印起始位置 */
		case 0x1b:  /* 设置行间距、位图命令*/
		case 0x1D:	/* 进纸到打印切纸位置 */
		case 0xff:  /* 绝对坐标定位 */
			s_ucDealCmdIn = 0;
			return 0;
			
		default:
			return 1;
	} 
}

/*
********************************************************
*
* 函数名:prntFeedPaparToFixedPosition
*
* 功  能:打印进纸到固定位置(非黑标纸)
*
* 输  入:g_usprntPaperLen3
*
* 输  出:无
*
* 返  回:FAULT   处理失败
*		  SUCCESS 处理成功
*
*******************************************************
*/
INT1 prntFeedPaparToFixedPosition
(
	UINT1 _ucPosition		/* 进纸到固定位置(单位:mm) */ 
)
{
	UINT2 _usStep;

	/* 穿孔机芯mm与步距换算:1mm = 1440/254步 */
	_usStep = _ucPosition *1440/254 ;
	if (_usStep <= g_usprntPaperLen3)
	{
		return FAULT;
	}
	else
	{
		/* 进纸到绝对坐标位置 */
		_usStep = _usStep - g_usprntPaperLen3 ;
		bspDotPrntPFLineSpace(1, _usStep); 
		return SUCCESS;
	}
}

/*
*******************************************************
*
* 函数名:prntFeedPaparToYCoordinate
*
* 功  能:打印进纸到纵坐标
*
* 输  入:g_usprntPaperLen1
*
* 输  出:无
*
* 返  回:FAULT   处理失败
*		  SUCCESS 处理成功
*
*******************************************************
*/
INT1 prntFeedPaparToYCoordinate
(
	UINT1 _ucPosition		/* 进纸到纵坐标绝对位置(单位:mm) */ 
)
{
	UINT2 _usStep;

    /* 其中17mm为黑标距离打印点基准距离 */
	_usStep = (_ucPosition + 17) *1440 /254 ;
	if (_usStep <= g_usprntPaperLen1)
	{
		return FAULT;
	}
	else
	{
		/* 进纸到绝对坐标位置 */
		_usStep = _usStep - g_usprntPaperLen1 ;
		bspDotPrntPFLineSpace(1, _usStep); 
		return SUCCESS;
	}
}

/*
*******************************************************
*
* 函数名:prntSetPrinterPara
*
* 功  能:设置打印参数
*
* 输  入:无
*
* 输  出:s_usPageLength
*		  g_ucPrntWidthAdjust
*		  g_ucPaperType
*		  g_usPrintLocatOffset
*		  s_usCutPaperLen
*         g_ucPrinterLineSpace
*
* 返  回:无
*
********************************************************
*/
void prntSetPrinterPara
(
	const PrntPara* _pPrntPar	/* (in)打印参数指针 */
)
{
	if (PRINTER_TYPE)
	{
		g_ucPrntChoiceLang = _pPrntPar->ucLangFlag;				   /* 打印语言种类(0 中文 1阿拉伯语、2英语) */
		g_ucPrntOrderMode = _pPrntPar->ucCharOrder;	           	   /* 票据打印对齐模式0左,1右对齐 */
		s_usPageLength = 72*(((_pPrntPar->usLength) + 63)/127);   /* 穿孔打印纸长 (0.1mm)    */
		g_ucPrntWidthAdjust = (360 - 2*(_pPrntPar->usWidth))/2;	   /* 穿孔打印纸宽 (0.295mm)  */
		g_ucPaperType = _pPrntPar->ucPaper;						   /* 票据类型(0不定长、1定长)*/
		g_usPrintLocatOffset=((_pPrntPar->ucLocalOffset)*1440)/254;/* 穿孔打印初始位置(1mm)   */
		s_usCutPaperLen = ((_pPrntPar->ucCutOffset)*1440)/254; 	   /* 穿孔打印切纸位置(1mm)   */
		if (g_ucPaperType)
		{
			g_usPrintLocatOffset += 96;           /* 17mm机械距离对应的步数(17*1440/254 = 96) */
			s_usCutPaperLen += 96;
		}
		g_ucPrinterLineSpace = _pPrntPar->ucLineSpace;			   /* 穿孔打印行间距(0.176mm) */
	}
	else
	{
		g_ucPrntChoiceLang = _pPrntPar->ucLangFlag;				   /* 打印语言种类(0 中文 1阿拉伯语 2英语) */
		if (g_ucPrntChoiceLang == LAN_CN)
		{
   			g_ucPrntChoiceLang = PRNT_CHINESE;
		}
		else if (g_ucPrntChoiceLang == LAN_ARAB)
		{
			g_ucPrntChoiceLang =  PRNT_ARABIC;
		}
		else
		{
			g_ucPrntChoiceLang =  PRNT_ENGLISH;
		}
		g_ucPrntOrderMode = _pPrntPar->ucCharOrder;	               /* 票据打印对齐模式0左,1右对齐 */
		s_usPageLength = 4*8*(_pPrntPar->usLength);				   /* 热敏打印纸长 (1mm)      */
		g_ucPrntWidthAdjust = 48 - (_pPrntPar->usWidth)/8;	       /* 热敏横向起始打印位置(1mm)*/
		g_ucPaperType = _pPrntPar->ucPaper;						   /* 票据类型(0不定长、1定长)*/
		g_usPrintLocatOffset = 4* 8 * (_pPrntPar->ucLocalOffset);  /* 热敏起始打印位置(1mm)   */
		s_usCutPaperLen = 4* 8 * (_pPrntPar->ucCutOffset);		   /* 热敏切纸位置(1mm)       */
		if (g_ucPaperType)
		{
			g_usPrintLocatOffset += 4*8*9 + g_usPrintLocatOffset;  /* 9mm机械距离对应的步数   */
			s_usCutPaperLen = 4*8*(9 + 15) + s_usCutPaperLen;      /* 切纸位置机械距离对应25mm */
		}
		g_ucPrinterLineSpace = _pPrntPar->ucLineSpace;			   /* 热敏打印行间距(0.125mm) */
	}
	return;
}

⌨️ 快捷键说明

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