Antarmuka Port Serial dengan C++

Gunakan Class Serial berikut:

//File: Serial.h

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

class Serial
        //Serial comm handler
        HANDLE hSerial;
        //Connection status
        bool connected;
        //Get various information about the connection
        COMSTAT status;
        //Keep track of last error
        DWORD errors;

        //Initialize Serial communication with the given COM port
        Serial(char *portName);
        //Close the connection
        //Read data in a buffer, if nbChar is greater than the
        //maximum number of bytes available, it will return only the
        //bytes available. The function return -1 when nothing could
        //be read, the number of bytes actually read.
        int ReadData(char *buffer, unsigned int nbChar);
        //Writes data from a buffer through the Serial connection
        //return true on success.
        bool WriteData(char *buffer, unsigned int nbChar);
        //Check if we are actually connected
        bool IsConnected();



//File: Serial.cpp
#include "Serial.h"

Serial::Serial(char *portName)
    //We're not yet connected
    this->connected = false;

    //Try to connect to the given port throuh CreateFile
    const size_t cSize = strlen(portName) + 1;
    wchar_t* wc = new wchar_t[cSize];
    mbstowcs_s(NULL, wc, cSize, portName, cSize);
    this->hSerial = CreateFile(wc,
    //Try to connect to the given port throuh CreateFile
    this->hSerial = CreateFile(portName,

    //Check if the connection was successfull
        //If not success full display an Error

            //Print Error if neccessary
            printf("ERROR: Handle was not attached. Reason: %s not available.\n", portName);

        //If connected we try to set the comm parameters
        DCB dcbSerialParams = {0};

        //Try to get the current
        if (!GetCommState(this->hSerial, &dcbSerialParams))
            //If impossible, show an error
            printf("failed to get current serial parameters!");
            //Define serial connection parameters for the arduino board
            //Setting the DTR to Control_Enable ensures that the Arduino is properly
            //reset upon establishing a connection
            dcbSerialParams.fDtrControl = DTR_CONTROL_ENABLE;

             //Set the parameters and check for their proper application
             if(!SetCommState(hSerial, &dcbSerialParams))
                printf("ALERT: Could not set Serial Port parameters");
                 //If everything went fine we're connected
                 this->connected = true;
                 //Flush any remaining characters in the buffers
                 PurgeComm(this->hSerial, PURGE_RXCLEAR | PURGE_TXCLEAR);
                 //We wait 2s as the arduino board will be reseting


    //Check if we are connected before trying to disconnect
        //We're no longer connected
        this->connected = false;
        //Close the serial handler

int Serial::ReadData(char *buffer, unsigned int nbChar)
    //Number of bytes we'll have read
    DWORD bytesRead;
    //Number of bytes we'll really ask to read
    unsigned int toRead;

    //Use the ClearCommError function to get status info on the Serial port
    ClearCommError(this->hSerial, &this->errors, &this->status);

    //Check if there is something to read
        //If there is we check if there is enough data to read the required number
        //of characters, if not we'll read only the available characters to prevent
        //locking of the application.
            toRead = nbChar;
            toRead = this->status.cbInQue;

        //Try to read the require number of chars, and return the number of read bytes on success
        if(ReadFile(this->hSerial, buffer, toRead, &bytesRead, NULL) && bytesRead != 0)
            return bytesRead;


    //If nothing has been read, or that an error was detected return -1
    return -1;


bool Serial::WriteData(char *buffer, unsigned int nbChar)
    DWORD bytesSend;

    //Try to write the buffer on the Serial port
    if(!WriteFile(this->hSerial, (void *)buffer, nbChar, &bytesSend, 0))
        //In case it don't work get comm error and return false
        ClearCommError(this->hSerial, &this->errors, &this->status);

        return false;
        return true;

bool Serial::IsConnected()
    //Simply return the connection status
    return this->connected;

Berikut contoh koneksi pengiriman data

#include <iostream>
#include "windows.h"
#include "Serial.h"

#include <thread>
#include <chrono>

using namespace std;

void delay(int ms){
    std::this_thread::sleep_for (std::chrono::milliseconds(ms));

int main(int argc, char** argv) {

    char alamatPort[] = "\\\\.\\COM7";
    Serial* s = new Serial(alamatPort);

	char dataOn[256] = "on1\n"; //"Nyala!";
	char dataOff[256] = "off0\n"; //"mati!";

	cout<<"isikan angka 1 untuk menyalakan, atau 0 untuk mematikan"<<endl;

	int jawab;

            s->WriteData(dataOff, sizeof(dataOff));
            s->WriteData(dataOn, sizeof(dataOn));
            for(int a=0;a<5;a++){
                s->WriteData(dataOn, sizeof(dataOn));
                s->WriteData(dataOff, sizeof(dataOff));

	return 0;

Berikut contoh untuk mengirim dan menerima data

#include <iostream>
#include <stdio.h>
#include <tchar.h>
#include "Serial.h" // Library described above
#include <string>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
char alamatPort[] = "\\\\.\\COM7";
Serial* SP = new Serial(alamatPort);    // adjust as needed

void* baca(void* data)

    char incomingData[256] = "";            // don't forget to pre-allocate memory
    int dataLength = 256;
    int readResult = 0;

    while (SP->IsConnected())
        readResult = SP->ReadData(incomingData, dataLength);
        if (readResult > 0) {
            printf("%s\n", incomingData);
        //printf("Bytes read: (-1 means no data available) %i\n",readResult);

        //std::string test(incomingData);

        //test = "";

    return NULL;

void* tulis(void* data)
    while (SP->IsConnected())
        char outgoingData[256] = "";
        char keluar[256] = "keluar";
        int dataLength = sizeof(outgoingData);        // don't forget to pre-allocate memory
        scanf_s("%255s", outgoingData, dataLength);

        if (outgoingData != "")
            if (strcmp(outgoingData, "OK") == 0) {
                printf("Pengiriman selesai\n");
                return NULL;

            if (SP->WriteData(outgoingData, dataLength))
                printf("Data send:%s\n", outgoingData);

    // do stuff...
    return NULL;

#include <thread>

int main(int argc, char** argv) {
    printf("Welcome to the serial test app!\n\n");
    if (SP->IsConnected())
        printf("Serial sudah terhubung\n");
    int status;
    std::thread thrd_1(baca,(void*)0);
    std::thread thrd_2(tulis, (void*)0);


// thread example
#include <pthread.h>

int main(int argc, char** argv) {
    printf("Welcome to the serial test app!\n\n");
    if (SP->IsConnected())
        printf("Serial sudah terhubung\n");

    pthread_t thrd_1;
    pthread_t thrd_2;
    int status;

    pthread_create(&thrd_1, NULL, baca, (void*)0);
    pthread_create(&thrd_2, NULL, tulis, (void*)0);

    pthread_join(thrd_1, (void**)&status);
    pthread_join(thrd_2, (void**)&status);

    printf("Terima kasih");

    return 0;

Printer POS USB menggunakan PHP

$tmpdir = sys_get_temp_dir();   # ambil direktori temporary untuk simpan file.
$file =  tempnam($tmpdir, 'ctk');  # nama file temporary yang akan dicetak (Windows, samba)

$handle = fopen($file, 'w');

//Printer parameter (ESC POS Command)
$initialized = chr(27).chr(64);

$justify = Chr(27) . Chr(97);
$left = Chr(0); $center = Chr(1);$right = Chr(2);

$fontwidth = Chr(27).Chr(87);
$doublewidth = Chr(1); $normalwidth = Chr(0);

$LF = Chr(10);

//Start making data
$Data  = $initialized;
$Data .= $fontwidth.$doublewidth;
$Data .= $justify.$center;
$Data .= "NAMA TOKO"."\n";
$Data .= $fontwidth.$normalwidth;
$Data .= "Alamat Toko"."\n";
$Data .= $LF.$LF;

//Write data to temporary file
fwrite($handle, $Data);

//WIN: send temporary file to nerwork shared printer (share your printer)
//LINUX: send data directly to device
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'){ //Win
    copy($file, "//localhost/POS-58");  # Shared Printer as POS-58
}else if (strtoupper(substr(PHP_OS, 0, 3)) === 'LIN'){ //Linux    
    $device = "/dev/usb/lp0";           # Print at lp0, find it using lsusb! Use udev for managing user access
    if (is_writable($device)) {
        $fp =fopen($device, "w");
        if($fp) {
            fwrite($fp, $Data);

//echo $Data;


Cara mencetak ke printer POS menggunakan PHP. Skrip ini mencontohkan jika menggunakan Windows maupun Linux.

Untuk menggunakan Windows maupun Linux, printer dibuat menjadi akses bersama (shared printer) terlebih dahulu lalu beri nama, misalnya: POS-58. Pastikan bisa diakses di //ALAMAT_IP/NAMA_PRINTER. Untuk skrip ini, menggunakan //localhost/POS-58 Untuk di Windows, buka di Control Panel, pastikan printer yang di-share sudah tersedia dan online.

Sedangkan jika menggunakan Linux, cari nama device dengan perintah:

ls /dev/usb

alamat device printer akan diawali dengan lpx, dimana x bisa 0,1,2 atau angka yang lain. Bagaimana bisa memastikan mana alamat device yang benar? gunakan perintah berikut:

echo "Test Printer" >> /dev/usb/lp0 

Jika printer bisa mencetak, berarti alamat printer adalah benar, yaitu /dev/usb/lp0 jika salah ganti angka 0 dengan angka 1 atau 2 atau angka lain hingga printer bisa mencetak. Pada skrip ini, alamat device perinter adalah /dev/usb/lp0

Setting printer USB di Raspberry Pi

sudo chown www-data:www-data etc
sudo chmod 775 etc
sudo chown www-data:www-data /dev/usb/lp0


Setting printer serial di raspberry pi

sudo raspi-config

“Interfacing Options,” “Serial.” Turn OFF: login shell over serial, ENABLE: hardware serial port
“Interfacing Options,” enable SSH

sudo apt-get update
sudo apt-get install git cups wiringpi build-essential libcups2-dev libcupsimage2-dev python-serial python-pil python-unidecode

cd ~
git clone
cd zj-58
sudo ./install

sudo usermod -a -G lp pi
sudo usermod -a -G lp www-data

sudo lpadmin -p ZJ-58 -E -v serial:/dev/serial0?baud=9600 -m zjiang/ZJ-58.ppd
sudo lpoptions -d ZJ-58

git clone
cd Python-Thermal-Printer

sudo usermod -a -G dialout www-data
sudo usermod -a -G dialout pi

Membuat Daemon di Raspberry Pi

Buka terminal lalu ketik:

hcitool scan

Nama perangkat biasanya bernama: hci0
Misalkan hasilnya adalah:

66:12:13:EE:D1:76 Bluetooth Printer

Test printer dengan perintah:

sudo rfcomm bind /dev/rfcomm0 66:12:13:EE:D1:76 1
echo “Test” > /dev/rfcomm0

Jika printer berhasil mencetak maka koneksi sudah berhasil

Selanjutnya tambahkan file untuk daemon service

sudo leafpad /etc/systemd/system/rfcomm.service

Ketik kode berikut dan simpan:

Description=RFCOMM service

ExecStart=/usr/bin/rfcomm bind hci0 66:12:13:EE:D1:76 1


Ganti nama hci0 dengan nama hasil hciconfig dan ganti 66:12:13:EE:D1:76 dengan hasil hcitool scan

Aktifkan daemon, ubah preferensi printer dan booting

sudo systemctl enable rfcomm
sudo systemctl start rfcomm
Sudo reboot

Menambahkan RTC pada Raspberry Pi 3

Setting antarmuka I2C

sudo apt-get install -y python-smbus
sudo apt-get install -y i2c-tools
sudo raspi-config

Pilih Interfacing Option > I2C > Enable lanjutkan dengan booting. Selanjutnya test koneksi dengan perintah i2cdetect untuk antaruka I2C 1. (Cek juga antarmuka ke 0 jika perlu)

sudo i2cdetect -y 1 #isikan angka 1 bila mengunakan I2C1, isikan 0 bila menggunakan I2C 0

Setting RTC

sudo modprobe rtc-ds1307 #load rtc ke kernel
sudo bash #masuk ke super user
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device #tambahkan device ds1307 di alamat 0x68 ke I2C1
exit #keluar dari super user
sudo hwclock -r #baca waktu dari RTC
sudo hwclock -w #tulis RTC berdasarkan sistem

Set agar waktu yang digunakan menggunakan RTC

sudo nano /etc/modules #agar modul rtc-ds1307 diload
#tambahkan baris berikut:

sudo nano sudo nano /etc/rc.local #agar dijalankan saat booting
#tambahkan baris berikut sebelum exit 0:
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device #tambahkan device ds1307  di alamat 0x68 ke I2C1
sudo hwclock -s #set waktu yang digunkan adalah RTC
date #baca waktu saat ini dari RTC


Mengubah script python menjadi executable

Menggunakan Linux

Gunakan pyinstaller, pasang dengan:

pip install pyinstaller

Jika gagal, maka:

Download: PyInstaller-3.3.1.tar.gz lalu extract dan masuk kedirektori tersebut.
Eksekusi dengan perintah:

sudo python build 
sudo python install

Selanjutnya ubah script python ke executable dengan

pyinstaller --onefile <script_phyton>.py

Jika dikatakan bootloader belum tersedia, misalnya untuk Linux ARM 32Bbit, maka harus dibuat dahulu. Pada hasil download PyInstaller ada direktori bootloader. Masuk ke direktori tersebut dan jalankan perintah:

sudo python ./waf all
cd ../PyInstaller/bootloader
cp -R Linux-32bit-arm/ /home/pi/.local/lib/python2.7/site-packages/PyInstaller/bootloader/Linux-32bit-arm/


Menerima paket UDP dan meneruskan ke URL atau website

Gunakan script phyton ini:

import socket, urllib2

UDP_IP = ""
UDP_PORT = 50000

sock = socket.socket(socket.AF_INET, # Internet
                     socket.SOCK_DGRAM) # UDP
#sock.bind((UDP_IP, UDP_PORT))
sock.bind(("", UDP_PORT)) #bind, receive from any addresss

while True:
 data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
 print "received message:", data
 stringdata = str(data)
 if stringdata[0] == "+":
 value = stringdata[2]
 karakter = ord(loket)
 karakter = karakter + 1;
 value = chr(karakter)
 url = "http://localhost/getUDP.php?var="+value
 print "url:", url
 response = urllib2.urlopen(url)
 headers =
 data =
 print data

Menambahkan autorun pada linux raspberry

buka direktori ~/config/autostart, buat jika belum ada dengan mkdir

buat file berektensi .desktop, misalnya chrome.desktop

lalu isikan script berikut

[Desktop Entry]
[Desktop Entry]
Exec=chromium-browser --kiosk --app=http://localhost/

Chrome akan berjalan setelah GUI terbuka, fullscreen tanpa notifikasi dengan sempurna. Namun jika ingin autorun saat booting, maka gunakan cara berikut:

sudo leafpad /etc/rc.local

sebelum bagian  exit 0 tambahkan kode berikut:

python /home/pi/ &

Penting: Ampersand (&) dibutuhkan agar proses autorun tidak memblokir proses yang lain, dangan kata lain proses yang lain juga akan terbuka walaupun proses autorun ini belum sepenuhnya selesai. Jika tanpa tanda ampersand, maka proses autorun ini akan ditunggu hingga selesai baru proses lain dibuka. Akan menjadi masalah jika pada proses ini loop, maka tidak akan pernah bisa login.

Untuk menghentikan proses loop pada autorun, gunakan:

sudo ps -ax | grep python
sudo kill <PID>

ganti <PID> dengan nomor proses. Hati hati jangan keliru, komputer bisa hang!