Reading RFID using C

This is a discussion on Reading RFID using C within the C Programming forums, part of the General Programming Boards category; Yes, <stdio.h> for start....

  1. #31
    Registered User
    Join Date
    Mar 2010
    Posts
    90
    Yes, <stdio.h> for start.

  2. #32
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    hi , good news here.i opened the COM port already.
    The card reader requires a request command to return the card number, i am trying to do that through writefile function.

    That' sounds a quite good start, but after i unplug the card reader, the program still shows the state is connecting, what's go on?

  3. #33
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Post your code... We're not mind readers...

  4. #34
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    Quote Originally Posted by CommonTater View Post
    Post your code... We're not mind readers...
    Ok, but i have to go now, i will post it ASAP,thanks

  5. #35
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    hi, i found some included APIs, i tried use it ,no compile errors and run time errors, but finally the console just crashed.
    Even if i compile without those API, the writfile and readfile function is still unfunctional..
    Code:
    #include <stdio.h>
    #include <windows.h>
    void main()
    {    
    	  
    	   DCB dcb = {0};
    	unsigned char str[32]={0};
    	DWORD wCount;
    	BOOL bReadStat;
    
    	DWORD dwNumBytesWritten;
    	BOOL bResult;
    	char *inbuf[10] = {"0x55","0x00","0x01","0x00","0x02","0x00","0x10","0x00","0x13","0xAA"};
          
    
    	HANDLE hOpenFile = (HANDLE)CreateFile("\\\\.\\COM1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,0, NULL);
           if (hOpenFile == INVALID_HANDLE_VALUE)
    	{	printf("Can not open the port\n");
               hOpenFile = NULL;
             
          }
    	   else if(hOpenFile != INVALID_HANDLE_VALUE)
    	   {
    		   printf("The port has been opened\n");
    	    
    		}
    	   PurgeComm(hOpenFile,PURGE_RXCLEAR|PURGE_TXCLEAR|PURGE_TXABORT|PURGE_RXABORT);
    
    	   
    	   if (!BuildCommDCB("9600,n,8,1", &dcb)) { 
    		   printf("Setting error\n");}
    	   else
    			printf("Setting ok\n");
    
    	bResult = WriteFile(hOpenFile,inbuf,10, &dwNumBytesWritten,NULL);
    		if(!bResult || dwNumBytesWritten == 0)
    			printf("Writing fail\n");
    		else
    			printf("Writing sucessful\n");
    		
        
    	bReadStat=ReadFile(hOpenFile,str,32,&wCount,NULL);
    		if(!bReadStat||wCount== 0)
    		{      
    			printf("Reading fail!");
    			PurgeComm(hOpenFile,PURGE_RXCLEAR|PURGE_TXCLEAR|PURGE_TXABORT|PURGE_RXABORT);
    		}
    		else 
    		{
    		printf("Reading sucessful");
    	}
    
    		CloseHandle(hOpenFile);
    
    
    }
    Last edited by anthonyung; 03-08-2011 at 09:14 AM.

  6. #36
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Where is it failing?

    Is the port being opened by CreateFile?
    1) There's no need to typecast the return of CreateFile. (Probably won't change anything, just unnecessary.)
    2) When you get an invalid handle your program should call exit(GetLastError()) and shut down.
    3) The else if condition is unnecessary... if it gets past your first error trap just assume the port is open...

    Have you tried setting the Baud, parity, bits and stops in Device Manager instead of using the apis you found?

    Your definition of inbuf should probably be simply char inbuf[] = instead of defining it as a char pointer. Sizing it as 10 with 10 hex bytes does not null terminate the resulting string... let the compiler handle it for you.

  7. #37
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    Quote Originally Posted by CommonTater View Post
    Where is it failing?

    Is the port being opened by CreateFile?
    1) There's no need to typecast the return of CreateFile. (Probably won't change anything, just unnecessary.)
    2) When you get an invalid handle your program should call exit(GetLastError()) and shut down.
    3) The else if condition is unnecessary... if it gets past your first error trap just assume the port is open...

    Have you tried setting the Baud, parity, bits and stops in Device Manager instead of using the apis you found?

    Your definition of inbuf should probably be simply char inbuf[] = instead of defining it as a char pointer. Sizing it as 10 with 10 hex bytes does not null terminate the resulting string... let the compiler handle it for you.
    i set that using "BuildCommDCB",is it ok?

  8. #38
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anthonyung View Post
    i set that using "BuildCommDCB",is it ok?
    That's why I suggested you try doing it manually ... you don't know if it's ok or not, do you?

  9. #39
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    Quote Originally Posted by CommonTater View Post
    That's why I suggested you try doing it manually ... you don't know if it's ok or not, do you?
    How can i set the Baud using DeviceIOcontrol?
    Code:
    BOOL WINAPI DeviceIoControl(
      __in         HANDLE hDevice,
      __in         DWORD dwIoControlCode,
      __in_opt     LPVOID lpInBuffer,
      __in         DWORD nInBufferSize,
      __out_opt    LPVOID lpOutBuffer,
      __in         DWORD nOutBufferSize,
      __out_opt    LPDWORD lpBytesReturned,
      __inout_opt  LPOVERLAPPED lpOverlapped
    );
    which part is for the Baud parameter?

  10. #40
    Registered User
    Join Date
    May 2010
    Posts
    2,698
    You might want to take a look at this link and scroll down to the section titled "Serial Settings". This section shows how to set the serial port settings using the DCB structure. You probably should also set the port for no Flow Control.


    Jim

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 06:27 AM
  2. Resetting a ifstream object after reading the whole file
    By Zeeshan in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2008, 08:03 AM
  3. Replies: 2
    Last Post: 01-28-2008, 02:07 AM
  4. help with reading from stream
    By movl0x1 in forum C Programming
    Replies: 7
    Last Post: 05-31-2007, 10:36 PM
  5. problems reading data into an array and printing output
    By serino78 in forum C Programming
    Replies: 4
    Last Post: 04-28-2003, 08:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21