Jun 302017

This small thermal printer is very basic and uses 1980s style ESC/POS control codes (like most similar printers of this type that are used in shops for receipt printing). Sending UTF-8 text directly to it (such as any accented characters) doesn’t work well, as the accented characters have different codes (this printer can use a variety of old DOS code pages but defaults to 437).

Luckily Python has all these text conversion functions built in (you don’t even need to roll your own lookup tables!)

# # # convert UTF-8 to codepage for thermal printer # # # this is UTF-8 text with German characters # import pprint import serial # # USB converter device name for thermal printer # ttyname = 'COM2' text1 = [ 'Sie kam als Kind\nwohl am Sonntag an,\n', 'denn sie hieß,\nseit ich denken kann,\n', 'immer nur Sunday Girl.\n', 'Sie war nie Schuld,\nganz egal woran,\n', 'ihr hat nie etwas weh getan,\n', 'denn sie war Sunday Girl.\n\n', 'Kalt wie Eis und genau so süß\n', 'so war sie und auch manchmal\nganz schön mies.\n', 'Für sie war es ein Kinderspiel,\n', 'pech für den, dem sie gefiel.\n', ] # # convert output to list of bytes with codepage # using printers default codepage 437 # opt =[] for i in range(len(text1)): byt = str.encode(text1[i],'cp437') opt.append(byt) # # open the printer port and send data # s=serial.Serial(ttyname,19200) # # centre the title text and bold it # s.write(b'\x1ba1\x1bE1') s.write(b'Text conversion test\n\n') s.write(b'\x1ba0\x1bE0') # print the converted text for i in range(len(opt)): s.write(opt[i]) # # done, print complete message # s.write(b'\x1ba1\x1bE1') s.write(b'\n\nComplete...\n\n\n') s.write(b'\x1ba0\x1bE0')

Dec 172016

code below

C Code to test the thermal printer and serial comms

/*=========================================================================== * Serial Port Programming in C (Serial Port Write) * Non Cannonical mode * Program writes a character to the serial port at 19200 bps 8N1 format *--------------------------------------------------------------------------- * original Programmer : Rahul.S * Date : 21-December-2014 *============================================================================ * altered by : Alex the Engineer * rtn VFRmedia the Rat's nest * Ipswich GB * to work with x86 netbook connected to Sparkfun thermal printer * via USB-TTL serial converter * Date : 2016-05-29 * 2016-05-30 : code recompiled to ZX-SL91 * (old Tosh amd64 laptop with busted keyboard repurposed as mini * server * ---------------------------------------------------------------------------- * 2016-06-10 : sourcecode formatting cleaned up a bit *============================================================================== /*-------------------------------------------------------------*/ /* termios structure - /usr/include/asm-generic/termbits.h */ /* use "man termios" to get more info about termios structure */ /*-------------------------------------------------------------*/ #include <stdio.h> #include <fcntl.h> /* File Control Definitions */ #include <string.h> /* strings */ #include <termios.h> /* POSIX Terminal Control Definitions */ #include <unistd.h> /* UNIX Standard Definitions */ #include <errno.h> /* ERROR Number Definitions */ /* global vars - probably shouldn't be hardcoded like this */ /* and some should be #defines to save memory */ /* but better than sticking them into the sourcecode */ int bytes_written = 0; /* Buffer containing characters to write into port */ char *write_buffer = "\x1b@\n\n\n" "\x1b\x0ertn\n" "telecommunicatie\x1b\x14\n" "printertest ZX-SL91\n" "\nAchtung! Achtung!\n" "The Goose is Loose!\n" "\x1d\x48\x03\n"; char *msg2= "\x1b@" "rtn :miauw naar de maan!\n\n\n\n\n"; char *barcode128n= "\x1d\x6bI"; char *item1= "2815 doedelzak"; char *item2= "9502 asbak "; char *item3= "7281 snakeoil "; char *lin_adv= "\n"; int main(void) { int fd; /*File Descriptor*/ printf("\n +----------------------------------+"); printf("\n | Serial Port Write |"); printf("\n +----------------------------------+"); /*------------------------------- Opening the Serial Port ------*/ /* Change /dev/ttyUSB0 to the one corresponding to your system */ fd = open("/dev/ttyUSB0",O_RDWR | O_NOCTTY | O_NDELAY); /* ttyUSB0 is the FT232 based USB2SERIAL Converter */ /* O_RDWR Read/Write access to serial port */ /* O_NOCTTY - No terminal will control the process */ /* O_NDELAY -Non Blocking Mode,Does not care about- */ /* -the status of DCD line,Open() returns immediatly */ if(fd == -1) { /* Error Checking */ perror("\n Error! in Opening ttyUSB0 "); return -1; } else perror("\n ttyUSB0 Opened Successfully "); /* set tty attrs 19200 8N1 */ struct termios SerialPortSettings; tcgetattr(fd, &SerialPortSettings); cfsetispeed(&SerialPortSettings,B19200); cfsetospeed(&SerialPortSettings,B19200); SerialPortSettings.c_cflag &= ~PARENB; SerialPortSettings.c_cflag &= ~CSTOPB; SerialPortSettings.c_cflag &= ~CSIZE; SerialPortSettings.c_cflag |= CS8; SerialPortSettings.c_cflag &= ~CRTSCTS; SerialPortSettings.c_cflag |= CREAD | CLOCAL; SerialPortSettings.c_iflag &= ~(IXON | IXOFF | IXANY); SerialPortSettings.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG); SerialPortSettings.c_oflag &= ~OPOST;/*No Output Processing*/ /* Set the attributes to the termios structure*/ if((tcsetattr(fd,TCSANOW,&SerialPortSettings)) != 0) { perror ("\n ERROR! in set attrs"); return -1; } else printf("\n ttyUSB0 set to 19200-8N1\n"); bytes_written = str_send(fd,write_buffer); printf("\n Sending these 3 barcode:\n%s\n%s\n%s\n",item1,item2,item3); bytes_written += bc128_send(fd,item1); bytes_written += bc128_send(fd,item2); bytes_written += bc128_send(fd,item3); bytes_written += str_send(fd,msg2); printf("\n %d Bytes written to ttyUSB0", bytes_written); printf("\n +----------------------------------+\n\n"); close(fd);/* Close the Serial port */ } /* str_send : write string of bytes to the serial port */ /* specified by fd */ /* returns number of bytes written */ int str_send (int fd, char *msg) { return write(fd,msg,strlen(msg)); } /* bc128_send : send code128 barcode from string */ /* create code 128 barcode - Achtung! uses fixed length sequence */ /* with hardcoded length of 14 (0x0e) sent to printer */ /* which must be specified as a string even though it is only 1 byte */ /* or wrong data is sent down the line */ /* and the barcodes do not print */ int bc128_send (int fd, char *barcode) { int bcount = 0; bcount += write(fd,barcode128n,3); bcount += write(fd,"\x0e",1); bcount += write(fd,barcode,14); return bcount; }
Aug 132016

The CCTV at work was upgraded;  I got to recover the old DVR and have repurposed it alongside a cheap camera mounted at low level to watch cats and other creatures in my garden. It is better than normal British TV, like having your own personal nature channel 🙂

all creatures great and small :)

The statue of the Blessed Virgin Mary also reminds me to take care with the metal box as its a 230V to 12V psu where I mislaid the case screws, there is 230V mains on terminals inside it)

catcam monitor

This cat is the ringleader, but he shares the space with around 5 others and seems to even encourage them to visit my garden…


One bonus of this arrangement is if I see a cat I can sometimes then photograph it with a better camera (the kitties are often surprised that I seem to know where they are 🙂 )

moar kittehs


Kätzin, interrupted....

Interestingly they seem to behave better since the CCTV was installed; they do not fight each other and make noise with miauwing at night nor foul this side of the garden.

It is not just cats either I see on the camera…

Hedgehog seen on the catcam!

Apologies for poor quality but that is a hedgehog. Apparently these are getting rare in some bits of England so I reported the sighting to Suffolk Wildlife Trust who put it on their map. A lot of folk seem to feed hedgehogs with cat food, I wonder if they are actually following the cats around (they are too big and well protected for lazy town cats to bother with them as prey)…