Antarmuka Port Serial dengan C++

Gunakan Class Serial berikut:

//File: Serial.h
#ifndef SERIALCLASS_H_INCLUDED
#define SERIALCLASS_H_INCLUDED

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

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

    public:
        //Initialize Serial communication with the given COM port
        Serial(char *portName);
        //Close the connection
        ~Serial();
        //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();


};

#endif // SERIALCLASS_H_INCLUDED

//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
 #if _MSC_VER && !__INTEL_COMPILER
    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,
        GENERIC_READ | GENERIC_WRITE,
        0,
        NULL,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        NULL);
#else
    //Try to connect to the given port throuh CreateFile
    this->hSerial = CreateFile(portName,
        GENERIC_READ | GENERIC_WRITE,
        0,
        NULL,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        NULL);
#endif

    //Check if the connection was successfull
    if(this->hSerial==INVALID_HANDLE_VALUE)
    {
        //If not success full display an Error
        if(GetLastError()==ERROR_FILE_NOT_FOUND){

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

        }
        else
        {
            printf("ERROR!!!");
        }
    }
    else
    {
        //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!");
        }
        else
        {
            //Define serial connection parameters for the arduino board
            dcbSerialParams.BaudRate=CBR_9600;
            dcbSerialParams.ByteSize=8;
            dcbSerialParams.StopBits=ONESTOPBIT;
            dcbSerialParams.Parity=NOPARITY;
            //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");
             }
             else
             {
                 //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
                 Sleep(2000);
             }
        }
    }

}

Serial::~Serial()
{
    //Check if we are connected before trying to disconnect
    if(this->connected)
    {
        //We're no longer connected
        this->connected = false;
        //Close the serial handler
        CloseHandle(this->hSerial);
    }
}

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(this->status.cbInQue>0)
    {
        //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.
        if(this->status.cbInQue>nbChar)
        {
            toRead = nbChar;
        }
        else
        {
            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;
    }
    else
        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;
	do{
        cin>>jawab;;

        if(jawab==0)
            s->WriteData(dataOff, sizeof(dataOff));
        if(jawab==1)
            s->WriteData(dataOn, sizeof(dataOn));
        if(jawab==2)
            for(int a=0;a<5;a++){
                s->WriteData(dataOn, sizeof(dataOn));
                delay(500);
                s->WriteData(dataOff, sizeof(dataOff));
                delay(500);
            }
	}while(jawab<3);

	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
    //printf("%s\n",incomingData);
    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);

        //printf("%s",incomingData);
        //test = "";

        Sleep(500);
    }
    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);
            }
        }

        Sleep(500);
    }
    // do stuff...
    return NULL;
}

#if _MSC_VER && !__INTEL_COMPILER
#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);

    thrd_1.join();
    thrd_2.join();

}
#else
// 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;
}
#endif

Printer POS USB menggunakan PHP

<?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);
fclose($handle);

//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);
            fclose($fp);
        }
    }
}

unlink($file);
//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

source:
https://www.howtogeek.com/169679/how-to-add-a-printer-to-your-raspberry-pi-or-other-linux-computer/
https://kernelmastery.com/enable-regular-users-to-add-printers-to-cups/
https://maker.pro/raspberry-pi/projects/how-to-turn-a-usb-printer-into-a-wireless-printer-with-raspberry-pi-zero-w

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 https://github.com/adafruit/zj-58
cd zj-58
make
sudo ./install

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

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

git clone https://github.com/adafruit/Python-Thermal-Printer
cd Python-Thermal-Printer
python printertest.py

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

Membuat Daemon di Raspberry Pi

Buka terminal lalu ketik:

hciconfig
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:

[Unit]
Description=RFCOMM service
After=bluetooth.service
Requires=bluetooth.service

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

[Install]
WantedBy=multi-user.target

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:
i2c-bcm2708 
rtc-ds1307

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

Sumber:

Mengubah script python menjadi executable

Menggunakan Linux

Gunakan pyinstaller, pasang dengan:

pip install pyinstaller

Jika gagal, maka:

Buka: https://github.com/pyinstaller/pyinstaller/releases
Download: PyInstaller-3.3.1.tar.gz lalu extract dan masuk kedirektori tersebut.
Eksekusi dengan perintah:

sudo python setup.py build 
sudo python setup.py 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/
 

sumber: https://pyinstaller.readthedocs.io/en/stable/bootloader-building.html

Menerima paket UDP dan meneruskan ke URL atau website

Gunakan script phyton ini:

import socket, urllib2

UDP_IP = "127.0.0.1"
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 = response.info()
 data = response.read()
 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]
Type=Application
Name=Clock
[Desktop Entry]
Type=Application
Name=Clock
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/blink.py &

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!

sumber: https://learn.sparkfun.com/tutorials/how-to-run-a-raspberry-pi-program-on-startup/all