📄 printer.c
字号:
#include <stdio.h>#include "w83977.h"const unsigned char ImageFileName[] = {"mnt/nand_disk1/dtt4.bmp"};const unsigned char Efilename[] = {"mnt/nand_disk1/e24x12.fon"}; //西文字库文件名const unsigned char Cfilename[] = {"mnt/nand_disk1/hzm24h.fon"}; //汉字字库文件名FILE *EFontFile; //西文字库文件FILE *CFontFile; //汉字字库文件#define E24FONT_W 2 /* width of E24x16 font */#define C24FONT_W 3 /* width of C24x24 font */#define Font_Line_W 48 /* BYTES / print one line */#ifndef PRINTER_STATUS#define PRINTER_STATUS#define PPT_nBUSY 0x80#define PPT_nACK 0x40#define PPT_PE 0x20#define PPT_SLCT 0x10#define PPT_nERROR 0x08#define PPT_TMOUT 0x01 //this bit only be used in EPP mode#endif//函数功能: 打开字库文件int InitFileHandle(void){ int err1 = 0,err2 = 0; EFontFile = fopen(Efilename,"rb"); if(EFontFile == NULL) err1 = 1; CFontFile = fopen(Cfilename,"rb"); if(CFontFile == NULL) err2 = 1; if( (err1 + err2) > 0 ) { LCD6963_ClearScreen(); //清屏幕 if(err1) LCD_DisplayOneLine( 2,0 ,"无西文打印字库",'D',1,0 ); if(err2) LCD_DisplayOneLine( 4,0 ,"无中文打印字库",'D',1,0 ); sleep( 3 ); } return err1+err2;}//---------------------------------------------------------------------------//函数功能: 关闭字库文件void ColseFileHandle(void){ fclose(EFontFile); /* close font file */ fclose(CFontFile); /* close font file */}//---------------------------------------------------------------------------//函数功能: 得到单个汉字字符的字型void GetCFont(unsigned char Hchr,unsigned char Lchr,unsigned char *Font){ int i; //unsigned int curpos; long curpos; unsigned char *point; point = Font; for(i=0;i<72;i++) point[i]=0; Hchr = (Hchr & 0x7f)-0x20; Lchr = (Lchr & 0x7f)-0x20; curpos = (long)(((long)Hchr-16) * 94 + ((long)Lchr-1)) * 72; //printf("%d.%d,%d\n",Hchr,Lchr,curpos); fseek(CFontFile, curpos, SEEK_SET); for(i=0;i<72;i++) point[i] = fgetc(CFontFile);}//---------------------------------------------------------------------------//函数功能: 得到单个西文字符的字型void GetEFont(unsigned char chr,unsigned char *Font){ int i; unsigned char tmpChr; unsigned int curpos; unsigned char *point; point = Font; tmpChr = chr; for(i=0;i<48;i++) point[i]=0; tmpChr = tmpChr-0x20; curpos = (unsigned int)tmpChr * 48; fseek(EFontFile, curpos, SEEK_SET); for(i=0;i<48;i++) point[i] = fgetc(EFontFile);}//---------------------------------------------------------------------------//功能: 打印软字库字符串//参数: PrintStr:打印内容// PrintLength:打印字符串长度//返回: 打印状态 =0:打印成功 !=0:打印失败int PrintSoftStr(unsigned char *PrintStr){ int i,j,point,Pwidth,StrLength,err; unsigned char CFont24[80]; //单个汉字的字型(用于软字库) 72字节 unsigned char EFont24[60]; //单个西文字符的字型(用于软字库) 此字型为24x16点阵 48字节 unsigned char Line_Font[1160]; //一行汉字的字型(用于软字库,一行最多打印16个汉字,32个西文字符) //热敏打印机一'点'行为384=48*8个点 24*48=1152 一行为24点 打印一行字符图形点阵需要1152个字节 err = InitFileHandle(); // open font file if(err) { printf("the font file can't open .%d\n",err); return err; } if( LptStatus() != 0xdf ) { //无打印机 printf("the printer can't open .%d\n",err); return -1; } memset(Line_Font,0x0,1153); StrLength = strlen(PrintStr); point = 0; for(i=0;i<StrLength;i++) //得到一行字符的字型 { if(PrintStr[i]>0x80) //汉字 { if((point + C24FONT_W) > Font_Line_W) break; //如果大于本行最大宽度跳出for循环 GetCFont(PrintStr[i],PrintStr[i+1],CFont24); // read chinise card 24 x 24 dot font for(j=0;j<24;j++) //将单个汉字字型放入整行字型中 ByteMove(&Line_Font[point+j*48],&CFont24[j*C24FONT_W],C24FONT_W); //将得到的单个汉字字型移入整行字型Buf i++; point = point + C24FONT_W; } else //西文 { if((point + E24FONT_W) > Font_Line_W) break; //如果大于本行最大宽度跳出for循环 GetEFont(PrintStr[i],EFont24); // read chinise card 24 x 24 dot font for(j=0;j<24;j++) //将单个西文字型放入整行字型中 ByteMove(&Line_Font[point+j*48],&EFont24[j*E24FONT_W],E24FONT_W); //将得到的单个西文字型移入整行字型Buf point = point + E24FONT_W; } } ColseFileHandle(); //关闭字库文件 SpaceLine( 4 ); //err = PrintSpace(4,0); //走n点空行 for(i=0;i<24;i++) //一行点阵字型送打印机 { //驱动打印 err = PrintDotLine(&Line_Font[i*48],48,1); if(err) return err; } return err;}//---------------------------------------------------------------------------//*****************************************************************************static void Delay(int time){ while(time --);}//*****************************************************************************static void Ext_Funct_Reg_W( unsigned char addr, unsigned char data ){ Extend_Index_Reg = addr ; Extend_Date_Reg = data ;}//*****************************************************************************static unsigned char Ext_Funct_Reg_R( unsigned char addr ){ unsigned char d ; Extend_Index_Reg = addr ; d = Extend_Date_Reg ; return ( d ) ;}//*****************************************************************************void ByteMove(unsigned char *distance, unsigned char *source, int length){ int i; for(i=0; i<length; i++){ * distance = *source; distance ++; source ++; }}/******************************************************************************W83977 PPT Initial******************************************************************************/void W83977_PPT_Init(void){ printf( "\n\nW83977 PPT Initial\n" ); Extend_Index_Reg = 0x87 ; Extend_Index_Reg = 0x87 ; Delay(50000); //delay 100 ms //Enter the extended function mode ,interruptible double-write //Configurate logical device Ext_Funct_Reg_W( W83977_CR07, 0x01 ) ; //select logical device 1 printf( "CR07 = 0x%2x\n", Ext_Funct_Reg_R( W83977_CR07 ) ); Ext_Funct_Reg_W( PPT_CR30, 0x01 ) ; //Logical device 1 is active printf( "CR30 = 0x%2x\n", Ext_Funct_Reg_R( PPT_CR30 ) ); Ext_Funct_Reg_W( PPT_CR60, PPT_BaseH ) ; //Parallel Port I/O base address Ext_Funct_Reg_W( PPT_CR61, PPT_BaseL ) ; Ext_Funct_Reg_W( PPT_CR70, 0x07 ) ; //IIRQ resource Ext_Funct_Reg_W( PPT_CR74, 0x04 ) ; //No DMA Ext_Funct_Reg_W( PPT_CRf0, 0x04 ) ; //Printer mode //Configurate logical device display printf( "CR60 = 0x%2x\n", Ext_Funct_Reg_R( PPT_CR60 ) ); printf( "CR61 = 0x%2x\n", Ext_Funct_Reg_R( PPT_CR61 ) ); printf( "CR70 = 0x%2x\n", Ext_Funct_Reg_R( PPT_CR70 ) ); printf( "CR74 = 0x%2x\n", Ext_Funct_Reg_R( PPT_CR74 ) ); printf( "CRf0 = 0x%2x\n", Ext_Funct_Reg_R( PPT_CRf0 ) ); Extend_Index_Reg = 0xaa ; //Exit extended function mode //************************************************************************* //printer control PPT_LATCH = (3<<6)|(0<<5)|(0<<4)|(1<<3)|(0<<2)|(0<<1)|(1) ; //disnable nACK interrupt, select printer, start printer, no feed, storbe pulled High printf( "Control register : PPT_SWAP = 0x%2x\n", PPT_SWAP ); printf( "Status register : PPT_STATUS = 0x%2x\n", PPT_STATUS ); Delay(1000);}/********************************************************* Function: Getting the status of printer port return value: status = 0 means succeed, status != 0 means failure*********************************************************/unsigned char LptStatus(void){ unsigned char temp; temp = PPT_STATUS; return temp;}/********************************************************* Function: Transmitting a Byte data to printer port parameters: Data: datas be transmitted, Length: datas length return value: status = 0 means succeed, status != 0 means failure*********************************************************/int LptSend(unsigned char *Data, int Length){ int count, time; for(count=0; count<Length; count++){ time =10000; //waiting for printer's nBUSY message while(!(LptStatus() & PPT_nBUSY)){ if(!(time --)){ printf("Transmit printer data ERROR!\n"); return 1; }; Delay(100); } PPT_data = Data[count]; Delay(10); PPT_LATCH &= ~0x01; Delay(10);// printf("Transmit data = %2x\n", Data[count]); PPT_LATCH |= 0x01; Delay(10); } return 0;}/*========================================================== Function: the printer Initilization return value: status = 0 means succeed, status != 0 means failure===========================================================*/int InitPrinter(void){ int err; unsigned char InitPrinterCommand[] = {0x1b,'@',0x0d}; W83977_PPT_Init(); err = LptSend(InitPrinterCommand, 3); Delay(100); return err;}/*========================================================== Function: print space lines return value: status = 0 means succeed, status != 0 means failure===========================================================*/int SpaceLine(unsigned int line){ unsigned char SpaceLineCommand[] = { 0x1b,0x4a,1 };/* int err; unsigned char SpaceLineCommand[] = { 0x1b,0x4a,0 }; ByteMove(&SpaceLineCommand[2], &line, 1); //SpaceLineCommand[2] = line; err = LptSend(SpaceLineCommand, 3); Delay(100); //PrintAction(); return err;*/ int i; unsigned char *ImageDotLineData; // allocate memory for string ImageDotLineData = (unsigned char *)malloc(48); memset(ImageDotLineData,0,48); for(i=0;i<line;i++) PrintDotLine(ImageDotLineData,48,1); //if(line==1) // LptSend(SpaceLineCommand, 3); PrintAction(); // free memory free(ImageDotLineData); LptSend(SpaceLineCommand, 3); PrintAction(); usleep(200000); //延时等待打印机动作 return 0;}/********************************************************* Print a dot line parameters: Data: a dot line array datas, length: the datas length, line_count: print times NOTE: the datas <= 48Bytes return value: status=0 means succeed, status!=0 means failure*********************************************************/int PrintDotLine(unsigned char *Data, int length, unsigned char line_count){ int err,time,i; //the printer: a dot line = 384 = 48* 8 dots unsigned char Print_DotLine_Buf[51] = {0x1b, 0x59, 1, 0}; time = 100000; while(!(LptStatus() & PPT_nBUSY)){ if(!(time --)){ printf("The printer is NOT prepared!\n"); return 1; }; Delay(100); } Print_DotLine_Buf[2] = line_count; ByteMove(&Print_DotLine_Buf[3], Data, length); for(i=(3+length); i<51; i++) Print_DotLine_Buf[i] = 0x00; err = LptSend(Print_DotLine_Buf, 51); Delay(100); return err;}/*========================================================== Function: print proccess action return value: status = 0 means succeed, status != 0 means failure===========================================================*/static void PrintAction(void){ int err; unsigned char action_cmd[]={ 0x1b, 0x5e, 0x1b, 0x5e }; err = LptSend(action_cmd, 2); Delay(1000); return ; //err;}/*************************************************************** Get data of image which be printed**************************************************************/void getImage(unsigned char *ImageData){ int curpos,i,j; FILE *ImageFile; ImageFile = fopen(ImageFileName,"rb"); if(ImageFile == NULL) return; for(i=119;i>=0;i--) { curpos = 0x3e + 48*i; fseek(ImageFile, curpos, SEEK_SET); for(j=0;j<48;j++) ImageData[(119-i)*48+j] = fgetc(ImageFile); } fclose(ImageFile); // close font file}/*======================================================= Print image=======================================================*///功能: 打印图标void PrintImage( void ){ int i; unsigned char *ImageDotLineData; // allocate memory for string if ((ImageDotLineData = (unsigned char *) malloc(6000)) == NULL) printf("ERR!!! Memory Apply Lost!!!"); memset(ImageDotLineData,0x0,6000); getImage(ImageDotLineData); for(i=0;i<120;i++) PrintDotLine(&ImageDotLineData[i*48],48,1); PrintAction(); free(ImageDotLineData); SpaceLine( 1 ); usleep(600000); //延时等待打印机动作 200,000 Yes||100,000 No }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -