Skip to content

Instantly share code, notes, and snippets.

@houmei
Created September 10, 2012 05:07
Show Gist options
  • Save houmei/3688972 to your computer and use it in GitHub Desktop.
Save houmei/3688972 to your computer and use it in GitHub Desktop.
A2 Thermal Printer bitmap test
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "delay.cpp"
#include "openserial.cpp"
int openFlags ;
const char *path = "/dev/tty.usbserial-AE01APTO" ;
// const char *path = "/dev/tty.usbserial" ;
#define A2EMPHASIZE 0x08
#define A2DOUBLEHEIGHT 0x10
#define A2DOUBLEWIDTH 0x20
#define A2DELETELINE 0x40
#define A2UNDERLINE 0x80
char one_line[48]; // 12dot x 32character
char image[48*48]; // 12dot x 32character x 48line
int bitmap(int fd,char *line){
char *p;
write(fd,"\x12\x56\x30\x00",4) ; // print bitmap
for(int y=0;y<48;y++){
p=line+y*48;
for(int x=0;x<48;x++){
write(fd,p++,1);
}
}
}
main(){
int fd, cflag ;
struct termios termattr ;
char r[100]; // read buffer
const char *p="----+----+----+----+----+----+--" ;
const char *q="01234567890" ;
// abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
int t;
fd = openPort( path,B19200,CS8,0,1,( O_RDWR | O_NOCTTY | O_NDELAY) ) ;
if ( fd<0 ) {
printf("open error\n") ;
exit(fd);
}
printf("filehandler=%d\n",fd);
// --- A2Thermal Initialize
write(fd,"\x1b\x37\x14\xff\xff",5); // Printer Setting HeatingDots,HeatTime,HeatInterval@9V
write(fd,"\x12\x23\x8e",3); // Print Density 4<<5 | 14
// write(fd,"\x12\x54",2); // printing test page
write(fd,"\x1d\x61\x44",3); // ASB,RTS
for(int t=1;t<=5; t++) {
write(fd,"\x0a\x0a",1);
for(int y=0;y<48;y++){
for(int x=0;x<48;x++) image[x+y*48]=0x70+y;
}
bitmap(fd,image);
write(fd,"\x0a\x0a",1);
bitmap(fd,image);
write(fd,"\x0a\x0a",1);
bitmap(fd,image);
write(fd,"\x0a\x0a",1);
delay(1000);
write(fd,"\x0a\x0a",2);
delay(100);
write(fd,"\x0a\x0a",2);
write(fd,"\x0a\x0a",2);
delay(100);
write(fd,"\x0a\x0a",2);
delay(1000);
}
close(fd);
}
#include <time.h>
int delay(long t) {
clock_t st,fin;
st=clock();
do {
fin=clock();
if (fin==(clock_t)-1) return 1;
} while (fin-st<CLOCKS_PER_SEC/1000.0*t);
return 0;
}
// see ref. http://linuxjf.sourceforge.jp/JFdocs/Serial-Programming-HOWTO-3.html
//
// int openFlags ;
// const char *path = "/dev/tty.usbserial-AE01APTO" ;
// const char *path = "/dev/tty.usbserial" ;
// common function to open port and set up serial port parameters
int openPort( const char *path, int speed, int bits, int parity, int stops, int openFlags)
{
int fd, cflag ;
struct termios termattr ;
fd = open( path, openFlags ) ;
if ( fd < 0 ) return -1 ;
// build other flags
cflag = 0 ;
cflag |= ( bits == 7 ) ? CS7 : CS8 ; // bits
if ( parity != 0 ) {
cflag |= PARENB ; // parity
if ( parity == 1 ) cflag |= PARODD ;
}
if ( stops > 1 ) cflag |= CSTOPB ;
// merge flags into termios attributes
tcgetattr( fd, &termattr ) ;
termattr.c_cflag &= ~( CSIZE | PARENB | PARODD | CSTOPB ) ; // clear all bits and merge in our selection
termattr.c_cflag |= cflag | CLOCAL ; //
// set speed, split speed not support on Mac OS X?
cfsetispeed( &termattr, speed ) ;
cfsetospeed( &termattr, speed ) ;
// set termios
tcflush(fd,TCIFLUSH); //
tcsetattr( fd, TCSANOW, &termattr ) ;
return fd ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment