Hardware Serial – wrong fist byte on read

I’m trying to read data from a smart card in passive mode using an arduino Leonardo.
The setup is very basic for now. I sniff the traffic connecting I/O Pin of the smart card to the rx pin 0 on Serial1, then I send it to Serial. My code is like this:

int incomingByte = 0;

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ;
    }
  Serial.println("Ready");
  Serial1.begin(10752,SERIAL_8N2);
}

void loop() {
  if (Serial1.available() > 0) {
      incomingByte = Serial1.read();
      Serial.println(incomingByte, HEX);
   }
}

The 10752 band is 4000000/372 (4Mhz crystal by 1 ETU). Almost everything is working ok, except for the first byte. Suppose I send the following command:

00 00 00 00 00, I receive 00 00 00 00 00 69 85. 
But if I send 01 00 00 00 00, I receive 00 00 00 00 00 69 85.

Further test indicates that bits 7,6,1 and 0 are always 0 in the first byte received.
I’ve installed IDE 1.5.6-r2 (major changes on hardwareserial) but the problem remains.
Any thoughts why is this happening only on the first byte?
Newby here.

Start with providing us a link to the hardware you use. Then post a photo or schematics of your setup (how you wired it).

Thanks Pylon,

It's very simple. Just connecting all the pins on the smart card and sniffing the I/O line.
On the drawing, arduino uno is used, but it's a Leonardo that I'm testing since it as a Serial1 port.

http://www.parallax.com/product/32320

Why are you connecting that device to the serial port? Those smart cards use I2C.
http://www.parallax.com/product/32323

I'm almost sure that the ISO 7816 specs state an asynchronous protocol, with 8 bits, even parity and 2 stop bits.
It's the same as SPI?

http://www.cardwerk.com/smartcards/smartcard_standard_ISO7816-3.aspx

If i send a lot of APDU commands, I get the expected values, except for the 1st byte.

"8 bits, even parity and 2 stop bits"
Looks like description of serial to me. Not SPI, which is synchronous (clock + data).

This is one of the Parallax smart cards for that reader. This is from the Overview section of that page.

The IS24C02A Smart Card provides 256 bytes of serial EEPROM accessible by Parallax’s Smart Card Reader (#32320). The pins of the IS24C02A are accessible through the gold plated contact pads embedded in the card.

This is from the Details section of that page:

Communication: I2C (CMOS)

That is why there is a data and clock line on that card reader.

edit: I read the protocol data sheet on the Smart Card you posted, but the Parallax page mentions nothing of an optional serial interface.

Right.

But that's the smart card internal protocol. I'm not connected to him.
I'm just sniffing the traffic between two smart readers, and that is an asynchronous protocol, except if the T1 is specified witch is not the case.

After a few more test I think the problem is on the guard time between bytes.

Have you tried a pullup resistor on the data line? I2C circuits usually require one, and the protocol sheet even mentions it. I did not see one on your schematic. Just a thought...

Thanks SurferTim.

Yes, I've already try it.

vjsmoreira:
I'm almost sure that the ISO 7816 specs state an asynchronous protocol, with 8 bits, even parity and 2 stop bits.
It's the same as SPI?

http://www.cardwerk.com/smartcards/smartcard_standard_ISO7816-3.aspx

If i send a lot of APDU commands, I get the expected values, except for the 1st byte.

The smart card reader you linked to uses I2C. What's the device on the left side of the picture?
It's possible that you just sniff the I2C data line with your UART and sometimes you get the data correctly because the SCK signal is timed exactly enough to be able to read the data by an UART but that doesn't mean that this is reliable in any way. If you connect the reader directly to your Arduino (the two extra pins labeled SDA and SCK) and use the Wire library to read it you probably get much better results as SurferTim already told you.

I concur with Surfer Tim and pylon that the protocol is I2C so you should also be reading the clock to get valid data. It should be possible to write code to do this without much problem.

I'm curious what the project your doing is as the equipment your reading and it's mode (passive) points towards potential card skimming.

I'll definitely try to use the I2C protocol and see the results.
I've read this (not in detail) http://www.nxp.com/documents/other/UM10204_v5.pdf and since there are a Clock line (4Mhz) and a I/O line, we can find some similarities.

The device on the left is just a dummy card connected to another smart reader. And no, this is not for card skimming or anything else you might imagine. Just a project to familiarize my self with the arduino platform.

vjsmoreira:
I'll definitely try to use the I2C protocol and see the results.
I've read this (not in detail) http://www.nxp.com/documents/other/UM10204_v5.pdf and since there are a Clock line (4Mhz) and a I/O line, we can find some similarities.

A quick skim (no pun intended) and it seems pretty comprehensive. May I suggest a more cut down version first.

Just a project to familiarize my self with the arduino platform.

Am I allowed to suggest an easier project to get familiar with the platform?

The device on the left is just a dummy card connected to another smart reader.

I'm sorry, but I don't believe that this is a dummy card because it manages an I2C bus, else you wouldn't be able to sniff the data. Can you describe in more detail what kind of device (a link to it's datasheet would be best) this is? Maybe we can give you a hint how to get to the data you need by an easier way.

Am I allowed to suggest an easier project to get familiar with the platform?

Please do. I'm also trying to build a Frequency reader and a Logic Analyzer with a beraduino kit.

I'm sorry, but I don't believe that this is a dummy card because it manages an I2C bus, else you wouldn't be able to sniff the data. Can you describe in more detail what kind of device (a link to it's datasheet would be best) this is? Maybe we can give you a hint how to get to the data you need by an easier way.

I don't expect you to believe. See the pic attach.

Ok.

Tried with (same code because they all inherit the same core libs):

TWI (I2C)          -> Nothing on pin2
SoftSerial         -> Change values to the custom baud rate, worst results
AltSoftSerial      -> Same results as hardware serial, wrong fist byte

For I2C you need to use the correct hardware pins (unless using a software wire lib). For the UNO that's pins A4 & A5

I've manage to found the problem. Actually it's not a problem is what data arduino leonardo is receiving.

I've get an oscilloscope from a friend (Velleman PCSU1000, not the best one to measure logic or at least as far as I know), and capture at 0,5ms. Each bit takes 0,09ms.
Sent -> 12 13 14 15 00
Receive -> 69 85

On the oscilloscope:
Sent -> 10 13 14 15 00
Receive -> not enough measure time.

But the point is: the first byte is different (probably the application). I'm using JSmartCard Explorer 1.03. I'll try with a little python script.

Anyway. Thanks for your support.

I just posted today about the same problem with TRUE serial to serial coms with a leonardo. I am interfacing with the serial port of an actual serial device and when I send a string the first part of the string ALWAYS comes back wrong :frowning:

http://forum.arduino.cc/index.php?topic=224148.msg1624114#msg1624114