How to get The data from BarCode Reader?
How to get The data from BarCode Reader?
Best answer:
If you know the format of the data from the barcode reader you can write a C/C++ program to read characters from the serial port and decode the information.
// terminal.c – simple terminal program for Dev-C++ and Cygwin
//
// only works for one port at moment
//
#include
#include
#include
HANDLE hCom; //handle for serial port I/O
/*—————————————————————————-*
* Serial port: initialise io_port, set baud rate, set data bits, one stop bit*/
HANDLE rs_initialise (int io_port, const long int BaudRate, const char parity, const char data)
{
BOOL bPortReady;
DCB dcb;
char ComPortName[]=”COM1″; // set up COM port number in COM?
ComPortName[3]=’0′+io_port;
hCom = CreateFile(ComPortName, GENERIC_READ | GENERIC_WRITE,
0, // exclusive access
NULL, // no security
OPEN_EXISTING,
0, // no overlapped I/O
NULL); // null template
if ((int)hCom <= 0) { printf("serial port COM%d connect fail %s error %d\n\r", io_port, ComPortName, GetLastError()); return 0; }
//else printf(" serial port COM%d connect OK \n\r", io_port);
bPortReady = SetupComm(hCom, 2, 128); // set buffer sizes
if (!bPortReady ) { printf("serial port COM%d SetupComm fail %d\n\r", io_port, GetLastError()); return 0; }
//else printf(" serial port COM%d connect OK \n\r", io_port);
bPortReady = GetCommState(hCom, &dcb);
if (!bPortReady ) { printf("serial port COM%d GetCommState fail %d\n\r", io_port, GetLastError()); return 0; }
// else printf(" serial port COM%d connect OK \n\r", io_port);
dcb.BaudRate = BaudRate;
if( data == '7') dcb.ByteSize = 7;
else dcb.ByteSize = 8;
if( parity == 'E') dcb.Parity = EVENPARITY;
if( parity == 'O') dcb.Parity = ODDPARITY;
else dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.fAbortOnError = TRUE;
// set XON/XOFF
dcb.fOutX = FALSE; // XON/XOFF off for transmit
dcb.fInX = FALSE; // XON/XOFF off for receive
// set RTSCTS
dcb.fOutxCtsFlow = FALSE; // turn off CTS flow control
dcb.fRtsControl = FALSE; // RTS_CONTROL_HANDSHAKE; //
// set DSRDTR
dcb.fOutxDsrFlow = FALSE; // turn off DSR flow control
//dcb.fDtrControl = DTR_CONTROL_ENABLE; // DTR handshake
dcb.fDtrControl = DTR_CONTROL_DISABLE; //
// dcb.fDtrControl = DTR_CONTROL_HANDSHAKE; //
bPortReady = SetCommState(hCom, &dcb);
if (!bPortReady ) { printf("serial port COM%d SetCommState fail %d\n\r", io_port, GetLastError()); return 0; }
// Communication timeouts
COMMTIMEOUTS CommTimeouts;
bPortReady = GetCommTimeouts (hCom, &CommTimeouts);
CommTimeouts.ReadIntervalTimeout = 5 ;
CommTimeouts.ReadTotalTimeoutConstant = 5 ;
CommTimeouts.ReadTotalTimeoutMultiplier = 1 ;
CommTimeouts.WriteTotalTimeoutConstant = 5 ;
CommTimeouts.WriteTotalTimeoutMultiplier = 1 ;
bPortReady = SetCommTimeouts (hCom, &CommTimeouts);
if (!bPortReady ) { printf("serial port COM%d SetCommTimeouts fail %d\n\r", io_port, GetLastError()); return 0; }
else printf(" serial port COM%d connect OK \n\r", io_port);
return hCom;
}
/*----------------------------------------------------------------------------*
* Serial port: terminate io_port, sets DTR and RTS to low */
void rs_terminate(const int io_port)
{
// Close(hCom);
}
/*----------------------------------------------------------------------------*
* Serial port: read character from io_port (ignored in this version) */
char rs_getch(const int io_port)
{
char rxchar;
BOOL bReadRC;
static DWORD iBytesRead;
bReadRC = ReadFile(hCom, &rxchar, 1, &iBytesRead, NULL);
if (iBytesRead) return rxchar; else return 0; // return 0 if no character read
}
/*----------------------------------------------------------------------------*
* Serial port: transmit character to io_port */
void rs_putch(const int io_port, const int txchar)
{
BOOL bWriteRC;
static DWORD iBytesWritten;
bWriteRC = WriteFile(hCom, &txchar, 1, &iBytesWritten,NULL);
return;
}
/*----------------------------------------------------------------------------*
* Serial port: transmit a string of characters to io_port */
void rs_putstring(const int io_port, const char *string)
{
while (*string != '')
rs_putch(io_port, *string++);
}
//#include
int main()
{
int port = 1;
if(!rs_initialise(port ,57600, ’8′, ‘N’)) { getch(); exit(1); }
char letter;
while(1)
{
if (kbhit()) rs_putch(port, getche()); // if keyboard hit read character and transmit it
if((letter=rs_getch(port))>0)
{ putchar(letter); if(letter==’\r’) putchar(‘\n’); } // if character received display it
}
getch();
return 0;
}
Tags: Barcode, Data, from, reader
Under Forum

Leave a Comment for How to get The data from BarCode Reader?
You must be logged in to post a comment.
Trackback this post | Subscribe to the comments via RSS Feed