element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Sci Fi Your Pi
  • Challenges & Projects
  • Design Challenges
  • Sci Fi Your Pi
  • More
  • Cancel
Sci Fi Your Pi
Blog Meditech: Serial over Bluetooth printer setting on Raspberry PI
  • Blog
  • Forum
  • Documents
  • Files
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: balearicdynamics
  • Date Created: 5 May 2015 8:57 PM Date Created
  • Views 2242 views
  • Likes 2 likes
  • Comments 1 comment
  • bluetooth
  • meditech_project
  • python
  • scifi_your_pi
  • setup
  • raspberry-pi
  • setting
  • serial
  • printing
  • wireless
  • installation
Related
Recommended

Meditech: Serial over Bluetooth printer setting on Raspberry PI

balearicdynamics
balearicdynamics
5 May 2015

    • Introduction
    • The setup procedure
      • Installing the components
      • Pairing and stable connection with the printer
    • Python printing example
Introduction

The need is to create a reliable and fast, simply to modify command set (as a matter of fact should become a Python library) to control the printer features. The small thermal ESC/POS printer, supports a well known protocol able to generate all the needed printout we need.

Adopting a Bluetooth enabled printer has the great advantage that the same peripheral can be used to print from inside the Meditech device and from the Mobile Display Unit without changing almost nothing.

 

Searching on the available documentation in Internet it seems that the most reliable way to manage the bluetooth hardware layers with python is using the lightblue cross-platform. I have tried it and it really support any kind of Bluetooth layer. There are some issues that can be solved but as the final result I have excluded it. The reason is that the architecture should be as simple as possible. In our case we don't need the full availability of the Bluetooth features but only a serial printer enabled and ready to work wireless, available at short and medium range.

 

The most simple choice revealed to be setup the serial layer over the Bluetooth interface. With the advantage that by the point of view of the entire system the printer remain a serial device as it should be.

 

On the RPI side I have used a common bluetooth dongle. As for what I see, the same procedure will work with almost any other USB Bluetooth dongle.

 

The setup procedure

The entire setup procedure is almost simple and can be replicated in minutes on any Raspberry PI running raspian. I am working with python 3.2 so should be took in account that some instructions in the standard libraries - i.e. the serial library - manages data in a slight different way than the previous python versions. I suggest anyway to adopt this version because the printing data are sent through the serial as bytes(...), including the encoding format. Specifying UTF-8 we are sure that every character is sent as an 8 bit packet to the peripheral. This aspect is important because of the printing protocol to avoid unexpected results when sending data including control characters.

 

Installing the components

If not yet present it is necessary to install the components as shown below:

 

sudo apt-get install bluetooth bluez-utils blueman
sudo apt-get install python-serial

 

After this operation the linux Bluetooth components and the serial library for python are installed on the system and we can proceed.

 

Pairing and stable connection with the printer

The next steps are shown as single command lines, but it is not complex to create a bash script to make all together in a single shot.

 

hciconfig

 

Use this command to see  how the Bluetooth device is recognised by the system. In my case it is handled as hci0

 

Now the printer should be powered and visible by the RPI Bluetooth then launch the command

 

hcitool scan

 

After waiting a while also the printer will appear in the identified devices. The result is something like


xx:xx:xx:xx:xx:xx    PRINTER_NAME

 

that are the device Bluetooth address and the Bluetooth printer name. At this point we should pair the printer with the RPI using the command

 

sudo bluez-simple-agent hci0 xx:xx:xx:xx:xx:xx

 

After few seconds the command ask to introduce the printer Bluetooth pin then the device is paired with the RPI.

 

At this point we should make the binding between the two bluetooth stable also after the reboot. We can do it editing the rfcomm.conf file

 

sudo leafpad /etc/bluetooth/rfcomm.conf

 

When the file opens in the editor (you can use the vi editor is there is not any graphic desktop running) add - or uncomment and change if already present - the following lines:

 

# Change rfcomm0 accordingly with your Bluetooth dongle setting
rfcomm0 {
    bind yes;
     # Replace xx:xx:xx ... with the printer Bluetooth address
    device xx:xx:xx:xx:xx:xx;
    channel 1;
    comment "Serial Bluetooth printer";
}

 

Save the file and exit. Now the last command is to enable the binding with the peripheral immediately with the command

 

sudo rfcomm bind all

 

Now the printer is connected and ready to receive data from the applications.

 

Python printing example

Open the Python 3 Ide and from the editor insert the following test script

 

#! /usr/bin/python

import serial
from time import sleep

bluetoothSerial = serial.Serial("/dev/rfcomm0", baudrate=9600)

testS = "Raspberry-PI Project \nMeditech Printer Test"
testV = input()

bluetoothSerial.write(bytes(testS, 'UTF-8'))
bluetoothSerial.write(bytes(testV, 'UTF-8'))

 

Run the script and it should print a test.

  • Sign in to reply
  • ifasanelli
    ifasanelli over 7 years ago

    Hi there, thanks for the tutorial.

    I'm starting coding and I find this project really interesting.

    I would like to know if it is possible to format the text printed(like change the font type or size) or even to add some pictures on it, like emoji for example. There is a way to do it adding some features to this print code?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube