Send data to a barcode scanner over RS232 serial port

时光怂恿深爱的人放手 提交于 2019-11-29 15:47:12
#include <iostream>
extern "C"
{
    #include <stdlib.h>
    #include <stdio.h>
    #include <unistd.h>
}
#include "DeviceRS232.h"

using namespace std;

int main()
{

    unsigned char sendBuffer[4096] = "Test test test...";
    unsigned char sendBeep[] = {0x05, 0xE6, 0x04, 0x00, 0x11, 0x46, 0x00};
    unsigned char ledOn[] = {0x05, 0xE7, 0x04, 0x00, 0x0D, 0x00};
    unsigned char val[7];

    //-------------------------------------------------------------------------
    unsigned char commonBuffer[257];

    int iChecksum;
    int i;
    commonBuffer[ 1 ] = (unsigned char)0xC6;
    commonBuffer[ 2 ] = (unsigned char)0x04;
    commonBuffer[ 3 ] = (unsigned char)0x08; // Permanant Chnage
    commonBuffer[ 4 ] = (unsigned char)0x11; // Beep after setting. FF for No Beep
    commonBuffer[ 5 ] = (unsigned char)0xEE; // Decorder parameter to set (238)
    commonBuffer[ 6 ] = (unsigned char)0x01; // Value to set

    commonBuffer[ 0 ] = (unsigned char)0x07; // Length

    iChecksum = 0;
    for (i = 0; i < 7; i++)
    {
        iChecksum += commonBuffer[i];
    }

    commonBuffer[i++] = (char)(((-iChecksum) >> 8) & 0xFF); // Add Checksum into the command
    commonBuffer[i++] = (char)((-iChecksum) & 0xFF);
    //-------------------------------------------------------------------------


    cout << "********************** RS232 - SSI **********************" << endl << endl;
    DeviceRS232 dev_rs232;
    dev_rs232.setDefaultAttributes();
    dev_rs232.openSerialPort();

    //----------------------------------------------------
    //for(int x=0; x<10; x++)
    //{
    //    dev_rs232.sendDataBuffer(sendBeep, sizeof(sendBeep));
    //}
    //----------------------------------------------------

    int sizeSent = dev_rs232.sendDataBuffer(commonBuffer, sizeof(commonBuffer));
    if( sizeSent > 0)
    {
        printf("Data sent: %d...\n", sizeSent);
    }

    sleep(1);

    dev_rs232.closeSerialPort();
    cout << "*********************************************************" << endl;


    return 0;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!