-
June 29th, 2006, 01:46 PM
#1
Serial Communication help for capturing data
Hi,
I need to capture data from a card reader using serial communication...
The card stripe data should be returned to the calling program .... The card data is going to be around 500 - 1000 characters max.
I want to write this in C , or VC++. I have .Net 2003 installed and eventually I need to convert this in to a DLL so that it can be called from anywhere.
Can anyone please provide me with some sample examples on how this can be done .
Thank you .
-
June 29th, 2006, 02:11 PM
#2
Re: Serial Communication help for capturing data
Typically each packet will be seperated with a character 13.
http://www.daniweb.com/techtalkforum...ead.php?t=8020
HTH,
ahoodin
To keep the plot moving, that's why.
-
June 29th, 2006, 03:03 PM
#3
Re: Serial Communication help for capturing data
ahoodin,
thanks for ur link .
I am looking to write a very simple program.
Open Com1 and read the card that is swiped from the reader.
The link that you gave me has too much detail and I am not able to get most of it since I a beginner.
Can you give me some simple program just like a basic client program in sockets ?.
Thanks
-
June 29th, 2006, 04:02 PM
#4
Re: Serial Communication help for capturing data
These functions will allow you to open a COM port and read data. I've copied them from my comport functions cpp that I use every day.
Code:
const int COM_SUCCESS = 0;
const int COM_FAILURE = 1;
const int COM_TIMEOUT = 2;
const int COM_CHAR_READY = 3;
const int COM_EMPTY = 4;
/////////////////////////////////////////////////
// OpenComPort()
// Returns 0 for fail, 1 for pass
//
int OpenComPort(HANDLE *hcom, // handle to com port
char port_no[], // "COM1" etc as string
int baud, // 9600, etc
int parity, // NONE, EVEN, ODD
int bitlength, // number of bits
int stopbits) //1 or 2
{
DCB dcb; // device control block
BOOL fSuccess;
*hcom = CreateFile( port_no,
GENERIC_READ | GENERIC_WRITE,
0, // comm devices must be opened w/exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // comm devices must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);
//check that comm port opened correctly
if (*hcom == INVALID_HANDLE_VALUE)
{
return (0);
}
//get comm port state
fSuccess = GetCommState(*hcom, &dcb);
//check for error getting comm state
if (!fSuccess)
{
return(0);
}
// Fill in the DCB:
// baud = 9,600
// 8 data bits, no parity, and 1 stop bit.
dcb.BaudRate = baud; // set the baud rate
dcb.ByteSize = bitlength; // data size, xmit, and rcv
dcb.Parity = parity; // no parity bit
dcb.StopBits = stopbits; // one stop bit
//set comm state
fSuccess = SetCommState(*hcom, &dcb);
//check for error setting comm state
if (!fSuccess)
{
DWORD err = GetLastError(); // debugging
return (0);
}
return fSuccess;
}
//////////////////////////////////////////////////////////////////
// read string from comport
// bytecount contains bytes read
//
unsigned char read_comport_str(HANDLE hCom,
char *str, int* bytecount)
{
unsigned long bytesread = 0;
int tries = 0;
unsigned long dErrorFlags; //error variable from ClearCommError
COMSTAT ComStat; //ComStat structure
do{
//Get communication port status structure to see if data is available
ClearCommError(hCom,&dErrorFlags, &ComStat);
//Read data from the com port if it's there
if (ComStat.cbInQue > 0)
{
//read the characters in the input buffer
ReadFile(hCom, str, ComStat.cbInQue, &bytesread, NULL);
}
Sleep(20); // 20 msec X 1000 = 20 sec timeout
tries++;
}while((bytesread == 0) && (tries < 1000));
if(tries == 1000)
return COM_TIMEOUT;
else if(bytesread == 0)
return COM_FAILURE;
else
{
*bytecount = (int)bytesread;
return COM_SUCCESS;
}
}
You create a HANDLE object when you open the port, and use the handle when calling the read function. Don't forget to close the handle when you're done, to free up the port.
Code:
HANDLE hcom;
int ret, count;
char buffer[255];
ret = OpenComPort(&hcom,"COM1",57600,EVENPARITY,8,2); // open the com port
if(ret)
{
ret = read_comport_str(hcom, buffer, &count);
}
else
{
// error
}
CloseHandle(hcom);
buffer contains your data.
-
April 1st, 2012, 11:19 AM
#5
Re: Serial Communication help for capturing data
Thanks for this, This has helped!
The Data that is coming in is all scrambled thought
I have a Teensy 2.0 that is Sending data to COM8 like this:
!ANG: 0.22, 0.33, 0.12
!ANG: 0.22, 0.33, 0.12
!ANG: 0.22, 0.33, 0.12
!ANG: 0.22, 0.33, 0.12
Etc... etc...
But What the buffer contains is weird ascii characters - please help!
-
April 2nd, 2012, 03:14 AM
#6
Re: Serial Communication help for capturing data
Originally Posted by ChadReitsma
I have a Teensy 2.0 that is Sending data to COM8 like this:
!ANG: 0.22, 0.33, 0.12
!ANG: 0.22, 0.33, 0.12
!ANG: 0.22, 0.33, 0.12
!ANG: 0.22, 0.33, 0.12
...
But What the buffer contains is weird ascii characters - please help!
Define "weird ascii characters".
Victor Nijegorodov
-
April 2nd, 2012, 01:52 PM
#7
Re: Serial Communication help for capturing data
The thread is nearly six years old. Perhaps related to ChadReitsma's recent thread at http://forums.codeguru.com/showthread.php?t=522597
Mike
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|