3
votes

I'm working on a printer, trying to read and write a contactless card.

This is the manual of the hardware and software I'm using: manual contactless

The process to ineract with the smart card (read and write in it), if I understood correctly, is:

  1. Establish the Context using SCardEstablishContext()
  2. Connect to the card through the selected reader using SCardConnect()
  3. Talk to the card using SCardTransmit()

Is that correct?

Points 1 and 2 works ok and return 0 (SCARD_S_SUCCESS), but I have problems with SCardTransmit.

  • If I want to read block 5 for example, this is the code I use:

     SCARD_IO_REQUEST pioRecvPci;
    
     DWORD dwActiveProtocol, dwSendLength, dwRecvLength;
     BYTE pbRecvBuffer[16];
     BYTE pbSendBuffer[] = {(BYTE)0xFC, (BYTE)0xB0,(BYTE)0x05, (BYTE)0x00, (BYTE)0x00};
    
     dwSendLength = sizeof(pbSendBuffer);
     dwRecvLength = sizeof(pbRecvBuffer);
    
      SCARD_IO_REQUEST pioSendPci;
    
         pioSendPci.dwProtocol=SCARD_PROTOCOL_T0;
         pioSendPci.cbPciLength= sizeof(pioSendPci);
    
     ret = SCardTransmit(cardHandle,                // SCard API
                            &pioSendPci, 
                            pbSendBuffer, dwSendLength,
              NULL, pbRecvBuffer, &dwRecvLength);
    

    It returns an error code 22.

    If I change SCARD_PROTOCOL_T0 to SCARD_PROTOCOL_T1, it seems to work (I don't know if pbRecvBuffer values are correct): pbRecvBuffer has 2 bytes with values: 6e 00 and ret = 0.

    Have these values sense?

    I read SCardTransmit api (SCardTransmit) and there I understand I have to use protocol T=0, does it mean I have to put SCARD_PROTOCOL_T0 to work? In that case, why I receive an errror 22? What I'm doing wrong?

  • If I want to write in block 5, I follow the same process: SCardEstablishContext, SCardConnect and SCardTransmit. I change pbSendBuffer to: {(BYTE)0xFC, (BYTE)0xD0,(BYTE)0x05, (BYTE)0x00, 0x10};, but I don't understand well where is the data, and what params I should put on SCardTransmit. Could you provide me an example of this?

Thank you very much!

1
What type of card do you use?Michael Roland

1 Answers

-1
votes
`enter code here` we  must  check protocol using switch case and transmit like :


switch ( m_dwAP )
    {
    case SCARD_PROTOCOL_T0:
        dwErrorFlags = SCardTransmit(this->m_hCardHandle,
                        SCARD_PCI_T0,
                        rgbIN,
                        bIN,
                        NULL,
                        rgbOUTTra,
                        &uwBufferLen);
        break;
    case SCARD_PROTOCOL_T1:
        dwErrorFlags = SCardTransmit(this->m_hCardHandle,
                        SCARD_PCI_T1,
                        rgbIN,
                        bIN,
                        NULL,
                        rgbOUTTra,
                        &uwBufferLen);
        break;
}
here m_dwAP  is active protocol.