将数据发送到一个酒吧code扫描仪通过RS232串口 [英] Send data to a barcode scanner over RS232 serial port

查看:452
本文介绍了将数据发送到一个酒吧code扫描仪通过RS232串口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是通过RS232串行口连接一个酒吧,code扫描仪。我需要编写一个程序,该设备与PC连接并传输数据。我已经写了一些基本的方法来初始化通信链路,并试图通过发送BEEP命令的设备进行测试,但预期它不发出蜂鸣声。所以我觉得有什么不对我的源$ C ​​$ C。请别人帮我完成源$ C ​​$ C。下面是编写的源$ C ​​$ C

01)DeviceRS232.h

 的#ifndef DEVICERS232_H
#定义DEVICERS232_H为externC
{
#包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&termios.h GT;
#包括LT&; SYS / ioctl.h>
#包括LT&;&unistd.h中GT;
#包括LT&;&fcntl.h GT;
#包括LT&; SYS / types.h中>
#包括LT&; SYS / stat.h>
#包括LT&;&limits.h中GT;
}#包括LT&;串GT;#定义MAX_SERIAL_PORT_NO 30类DeviceRS232
{
    上市:
        DeviceRS232();
        虚拟〜DeviceRS232();        INT fdRS232; //串行端口文件描述符        无效setSerialPort(标准::字符串SP);
        无效setBaudRate(长波特);
        无效setDataBits(INT数据位);
        无效setStopBits(INT停止位);
        无效setNumberOfParityBits(INT nparityBits);
        无效setDefaultAttributes();
        长getBaudRate();
        标准::字符串getSerialPort();
        INT openSerialPort();
        INT readUserConfiguration();
        INT sendDataBuffer(const的无符号字符* DataBuffer中,为size_t缓冲区大小);
        无效closeSerialPort();
    保护:
        标准::字符串的SerialPort; //串口如/ dev / ttyS0来
        长波特率; //扫描仪波特率
        诠释数据位; //数据位
        INT停止位; //停止位
        INT numberOfParityBits; //奇偶校验位的数量
        termios的oldSerialPortSetting; termios结构为为/ dev / ttyS0来//当前值
        termios的newSerialPortSetting; //新的termios属性为/ dev / ttyS0来
    私人的:
};#ENDIF // DEVICERS232_H

2)DeviceRS232.cpp

 的#includeDeviceRS232.hDeviceRS232 :: DeviceRS232()
{
    //构造函数
}DeviceRS232 ::〜DeviceRS232()
{
    //析构函数
}无效DeviceRS232 :: setSerialPort(标准::字符串SP)
{
    的SerialPort = SP;
}无效DeviceRS232 :: setBaudRate(长波特)
{
    波特率=波特;
}无效DeviceRS232 :: setDataBits(INT数据位)
{
    数据位=数据位;
}无效DeviceRS232 :: setStopBits(INT停止位)
{
    STOPBITS = STOPBIT;
}无效DeviceRS232 :: setNumberOfParityBits(INT nparityBits)
{
    numberOfParityBits = nparityBits;
}无效DeviceRS232 :: setDefaultAttributes()
{
    标准::字符串SP =为/ dev / ttyS0来;
    长波特率= 9600;
    INT数据位= 1;
    INT停止位= 1;
    INT nparityBits = 0;    setSerialPort(SP);
    setBaudRate(波特);
    setDataBits(数据位);
    setStopBits(停止位);
    setNumberOfParityBits(nparityBits);
}长DeviceRS232 :: getBaudRate()
{
    返回波特率;
}标准::字符串DeviceRS232 :: getSerialPort()
{
    返回的SerialPort;
}INT DeviceRS232 :: openSerialPort()
{
    INT FD,baudr,地位,portStatus;
    setDefaultAttributes();    开关(getBaudRate())
    {
        案例50:baudr = B50;
                       打破;
        案例75:baudr = B75;
                       打破;
        案例110:baudr = B110;
                       打破;
        案例134:baudr = B134;
                       打破;
        案例150:baudr = B150;
                       打破;
        案例200:baudr = B200;
                       打破;
        案例300:baudr = B300;
                       打破;
        案例600:baudr = B600;
                       打破;
        案例1200:baudr = B1200;
                       打破;
        案例1800:baudr = B1800;
                       打破;
        案例2400:baudr = B2400;
                       打破;
        案例4800:baudr = B4800;
                       打破;
        案例9600:baudr = B9600;
                       打破;
        案例19200:baudr = B19200;
                       打破;
        案例38400:baudr = B38400;
                       打破;
        案例57600:baudr = B57600;
                       打破;
        案例115200:baudr = B115200;
                       打破;
        案例230400:baudr = B230400;
                       打破;
        460800案例:baudr = B460800;
                       打破;
        案例500000︰baudr = B500000;
                       打破;
        案件576000:baudr = B576000;
                       打破;
        案件921600:baudr = B921600;
                       打破;
        案件的1000000:baudr = B1000000;
                       打破;
        默认:输出(无效的波特率\\ n);
                       返回(1);
                       打破;
    }    //打开串口
    FD =开(getSerialPort()c_str(),O_RDWR | O_NOCTTY | O_NDELAY);
    如果(FD == -1)
    {
        的printf(无法打开串口... \\ n);
        返回1;
    }    fdRS232 = FD;
    状态= tcgetattr(fdRS232,&安培; oldSerialPortSetting);
    如果(状态== -1)
    {
        关闭(fdRS232);
        的printf(无法获得串行端口属性... \\ n);
        返回1;
    }    memset的(安培; newSerialPortSetting,0,sizeof的(newSerialPortSetting));
    newSerialPortSetting.c_cflag = baudr | CS8 | CLOCAL | CREAD; //
    newSerialPortSetting.c_iflag = IGNPAR;
    newSerialPortSetting.c_oflag = 0;
    newSerialPortSetting.c_lflag = 0;
    newSerialPortSetting.c_cc [VMIN] = 0;
    newSerialPortSetting.c_cc [VTIME] = 0;    状态= tcsetattr(fdRS232,TCSANOW,&安培; newSerialPortSetting);
    如果(状态== - 1)
    {
        关闭(fdRS232);
        PERROR(无法调整portsettings);
        返回1;
    }    //获取打开串口的状态
    如果(的ioctl(fdRS232,TIOCMGET,&安培; portStatus)== -1)
    {
        PERROR(无法获取端口状态);
        返回1;
    }    //燕鸥的DTR和RTS
    portStatus | = TIOCM_DTR;
    portStatus | = TIOCM_RTS;    //设置新的DTR,RTS值端口的状态
    如果(的ioctl(fdRS232,TIOCMSET,&安培; portStatus)== -1)
    {
        PERROR(无法设置端口状态...);
        返回1;
    }  返回0;
}INT DeviceRS232 :: sendDataBuffer(const的无符号字符* DataBuffer中,为size_t缓冲区大小)
{
    回写(fdRS232,DataBuffer中,缓冲区大小);
}无效DeviceRS232 :: closeSerialPort()
{
    INT portStatus;    如果(的ioctl(fdRS232,TIOCMGET,&安培; portStatus)== -1)
    {
        PERROR(无法获取端口状态);
    }    //关燕鸥DTR和RTS
    portStatus&安培; =〜TIOCM_DTR;
    portStatus&安培; =〜TIOCM_RTS;    //设置新的DTR,RTS值端口的状态
    如果(的ioctl(fdRS232,TIOCMSET,&安培; portStatus)== -1)
    {
        PERROR(无法设置端口状态...);
    }    关闭(fdRS232);
}

3)的main.cpp

 的#include<&iostream的GT;
为externC
{
    #包括LT&;&stdlib.h中GT;
    #包括LT&;&stdio.h中GT;
    #包括LT&;&unistd.h中GT;
}
#包括DeviceRS232.h使用命名空间std;诠释的main()
{    //字符sendBuffer [4096] =;
    unsigned char型sendBeep [] = {0×05,0xE6,0x04的0x00时,0X0D,0×00,0×00};
    unsigned char型LEDON [] = {0×05,0xE7,0x04的0x00时,0X0D,0×00};
    unsigned char型VAL [7];    COUT<< ********************** RS232 - SSI ​​*********************&LT ;< ENDL<< ENDL;
    DeviceRS232 dev_rs232;
    dev_rs232.setDefaultAttributes();
    dev_rs232.openSerialPort();    // ------------------------------------------------ ----
    //对于(中间体X = 0; X小于10; X ++)
    // {
    // dev_rs232.sendDataBuffer(sendBeep,sizeof的(sendBeep));
    //}
    // ------------------------------------------------ ----    INT sizeSent = dev_rs232.sendDataBuffer(sendBeep,sizeof的(sendBeep));
    如果(sizeSent大于0)
    {
        的printf(数据传送:%d个... \\ n,sizeSent);
    }    睡眠(10);    dev_rs232.closeSerialPort();
    COUT<< ******************* ********<< ENDL;
    返回0;
}

unsigned char型 sendBeep [] = {0×05,0xE6,0x04的0x00时,0X0D,0×00,0×00}; 写入考虑酒吧设备的串口通讯协议规范。

(编辑补充输出)
OUT输出:

  ********************** RS232  -  SSI ​​************ *********发送的数据:7 ...
************************************************** *******流程返回0(为0x0)执行时间:10.006小号
preSS ENTER继续。

所有帮助和建议,欢迎。谢谢你。


解决方案

 的#include<&iostream的GT;
为externC
{
    #包括LT&;&stdlib.h中GT;
    #包括LT&;&stdio.h中GT;
    #包括LT&;&unistd.h中GT;
}
#包括DeviceRS232.h使用命名空间std;诠释的main()
{    unsigned char型sendBuffer [4096] =测试测试测试...;
    unsigned char型sendBeep [] = {0×05,0xE6,0x04的0x00时,为0x11,将0x46,0×00};
    unsigned char型LEDON [] = {0×05,0xE7,0x04的0x00时,0X0D,0×00};
    unsigned char型VAL [7];    // ------------------------------------------------ -------------------------
    unsigned char型commonBuffer [257];    INT iChecksum;
    INT I;
    commonBuffer [1] =(unsigned char型)0xC6;
    commonBuffer [2] =(unsigned char型)0×04;
    commonBuffer [3] =(unsigned char型)0x08的; //您永久Chnage
    commonBuffer [4] =(unsigned char型)为0x11; //设置后发出蜂鸣声。 FF为没有提示
    commonBuffer [5] =(unsigned char型)0xEE; // Decorder参数设置(238)
    commonBuffer [6] =(unsigned char型)为0x01; //值来设置    commonBuffer [0] =(unsigned char型)值为0x07; // 长度    iChecksum = 0;
    对于(I = 0; I&下; 7;我+ +)
    {
        iChecksum + = commonBuffer [I]
    }    commonBuffer [我++] =(char)的((( - iChecksum)GT;> 8)及0xFF的); //添加校验到命令
    commonBuffer [我++] =(char)的(( - iChecksum)及0xFF的);
    // ------------------------------------------------ -------------------------
    COUT<< ********************** RS232 - SSI ​​*********************&LT ;< ENDL<< ENDL;
    DeviceRS232 dev_rs232;
    dev_rs232.setDefaultAttributes();
    dev_rs232.openSerialPort();    // ------------------------------------------------ ----
    //对于(中间体X = 0; X小于10; X ++)
    // {
    // dev_rs232.sendDataBuffer(sendBeep,sizeof的(sendBeep));
    //}
    // ------------------------------------------------ ----    INT sizeSent = dev_rs232.sendDataBuffer(commonBuffer,sizeof的(commonBuffer));
    如果(sizeSent大于0)
    {
        的printf(数据传送:%d个... \\ n,sizeSent);
    }    睡眠(1);    dev_rs232.closeSerialPort();
    COUT<< ******************* ********<< ENDL;
    返回0;
}

I have a bar-code scanner which is connecting through RS232 serial port. I need to write a program to connect this device with the PC and transmit data. I already wrote some basic methods to initialize a communication link and tried to test by sending a BEEP command to the device but it is not beeping as expected. So I think there is something wrong with my source code. Please somebody help me complete the source code. Below is the written source code

01)DeviceRS232.h

#ifndef DEVICERS232_H
#define DEVICERS232_H

extern "C"
{
#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
}

#include <string>

#define MAX_SERIAL_PORT_NO  30



class DeviceRS232
{
    public:
        DeviceRS232();
        virtual ~DeviceRS232();

        int fdRS232;                    //  file descriptor for the serial port

        void setSerialPort(std::string sp);
        void setBaudRate(long baud);
        void setDataBits(int dataBit);
        void setStopBits(int stopBit);
        void setNumberOfParityBits(int nparityBits);
        void setDefaultAttributes();
        long getBaudRate();
        std::string getSerialPort();
        int openSerialPort();
        int readUserConfiguration();
        int sendDataBuffer(const unsigned char *dataBuffer, size_t bufferSize);
        void closeSerialPort();


    protected:
        std::string serialPort;         //  Serial port like /dev/ttyS0
        long baudRate;                  //  Scanner baud rate
        int dataBits;                   //  data bits
        int stopBits;                   //  stop bits
        int numberOfParityBits;         //  number of parity bits
        termios oldSerialPortSetting;   //  Current values of termios structure for /dev/ttyS0
        termios newSerialPortSetting;   //  new termios attributes for /dev/ttyS0


    private:
};

#endif // DEVICERS232_H

2) DeviceRS232.cpp

#include "DeviceRS232.h"

DeviceRS232::DeviceRS232()
{
    //ctor
}

DeviceRS232::~DeviceRS232()
{
    //dtor
}

void DeviceRS232::setSerialPort(std::string sp)
{
    serialPort = sp;
}

void DeviceRS232::setBaudRate(long baud)
{
    baudRate = baud;
}

void DeviceRS232::setDataBits(int dataBit)
{
    dataBits = dataBit;
}

void DeviceRS232::setStopBits(int stopBit)
{
    stopBits = stopBit;
}

void DeviceRS232::setNumberOfParityBits(int nparityBits)
{
    numberOfParityBits = nparityBits;
}

void DeviceRS232::setDefaultAttributes()
{
    std::string sp = "/dev/ttyS0";
    long baud = 9600;
    int dataBit = 1;
    int stopBit = 1;
    int nparityBits = 0;

    setSerialPort(sp);
    setBaudRate(baud);
    setDataBits(dataBit);
    setStopBits(stopBit);
    setNumberOfParityBits(nparityBits);
}

long DeviceRS232::getBaudRate()
{
    return baudRate;
}

std::string DeviceRS232::getSerialPort()
{
    return serialPort;
}

int DeviceRS232::openSerialPort()
{
    int fd, baudr, status, portStatus;
    setDefaultAttributes();

    switch(getBaudRate())
    {
        case      50 : baudr = B50;
                       break;
        case      75 : baudr = B75;
                       break;
        case     110 : baudr = B110;
                       break;
        case     134 : baudr = B134;
                       break;
        case     150 : baudr = B150;
                       break;
        case     200 : baudr = B200;
                       break;
        case     300 : baudr = B300;
                       break;
        case     600 : baudr = B600;
                       break;
        case    1200 : baudr = B1200;
                       break;
        case    1800 : baudr = B1800;
                       break;
        case    2400 : baudr = B2400;
                       break;
        case    4800 : baudr = B4800;
                       break;
        case    9600 : baudr = B9600;
                       break;
        case   19200 : baudr = B19200;
                       break;
        case   38400 : baudr = B38400;
                       break;
        case   57600 : baudr = B57600;
                       break;
        case  115200 : baudr = B115200;
                       break;
        case  230400 : baudr = B230400;
                       break;
        case  460800 : baudr = B460800;
                       break;
        case  500000 : baudr = B500000;
                       break;
        case  576000 : baudr = B576000;
                       break;
        case  921600 : baudr = B921600;
                       break;
        case 1000000 : baudr = B1000000;
                       break;
        default      : printf("invalid baudrate\n");
                       return(1);
                       break;
    }

    //  Open serial port
    fd = open(getSerialPort().c_str(),  O_RDWR | O_NOCTTY | O_NDELAY);
    if(fd == -1)
    {
        printf("Unable to open serial port...\n");
        return 1;
    }

    fdRS232 = fd;
    status = tcgetattr(fdRS232, &oldSerialPortSetting);
    if(status == -1)
    {
        close(fdRS232);
        printf("Unable to get serial port attributes...\n");
        return 1;
    }

    memset(&newSerialPortSetting, 0, sizeof(newSerialPortSetting));
    newSerialPortSetting.c_cflag = baudr | CS8 | CLOCAL | CREAD; //
    newSerialPortSetting.c_iflag = IGNPAR;
    newSerialPortSetting.c_oflag = 0;
    newSerialPortSetting.c_lflag = 0;
    newSerialPortSetting.c_cc[VMIN] = 0;
    newSerialPortSetting.c_cc[VTIME] = 0;

    status = tcsetattr(fdRS232, TCSANOW, &newSerialPortSetting);
    if(status==-1)
    {
        close(fdRS232);
        perror("unable to adjust portsettings ");
        return 1;
    }

    //  Get the status of opened serial port
    if(ioctl(fdRS232, TIOCMGET, &portStatus) == -1)
    {
        perror("Unable to get port status");
        return 1;
    }

    //  Tern on DTR and RTS
    portStatus |= TIOCM_DTR;
    portStatus |= TIOCM_RTS;

    //  Set the status of the port with new DTR, RTS values
    if(ioctl(fdRS232, TIOCMSET, &portStatus) == -1)
    {
        perror("Unable to set port status...");
        return 1;
    }

  return 0;
}

int DeviceRS232::sendDataBuffer(const unsigned char *dataBuffer, size_t bufferSize)
{
    return write(fdRS232, dataBuffer, bufferSize);
}

void DeviceRS232::closeSerialPort()
{
    int portStatus;

    if(ioctl(fdRS232, TIOCMGET, &portStatus) == -1)
    {
        perror("Unable to get the port status");
    }

    //  Tern off DTR and RTS
    portStatus &= ~TIOCM_DTR;
    portStatus &= ~TIOCM_RTS;

    //  Set the status of the port with new DTR, RTS values
    if(ioctl(fdRS232, TIOCMSET, &portStatus) == -1)
    {
        perror("Unable to set port status...");
    }

    close(fdRS232);
}

3) main.cpp

#include <iostream>
extern "C"
{
    #include <stdlib.h>
    #include <stdio.h>
    #include <unistd.h>
}
#include "DeviceRS232.h"

using namespace std;

int main()
{

    //char sendBuffer[4096] = "                        ";
    unsigned char sendBeep[] = {0x05, 0xE6, 0x04, 0x00, 0x0D, 0x00, 0x00};
    unsigned char ledOn[] = {0x05, 0xE7, 0x04, 0x00, 0x0D, 0x00};
    unsigned char val[7];

    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(sendBeep, sizeof(sendBeep));
    if( sizeSent > 0)
    {
        printf("Data sent: %d...\n", sizeSent);
    }

    sleep(10);

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


    return 0;
}

unsigned char sendBeep[] = {0x05, 0xE6, 0x04, 0x00, 0x0D, 0x00, 0x00}; is written considering the bar-device's serial port communication protocol specification.

(Edited to add the output) Out put:

********************** RS232 - SSI **********************

Data sent: 7...
*********************************************************

Process returned 0 (0x0)   execution time : 10.006 s
Press ENTER to continue.

All helps and suggestions are welcome. Thanks.

解决方案

#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;
}

这篇关于将数据发送到一个酒吧code扫描仪通过RS232串口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆