I`m about to develop a card reader system, thar sends the ID of a card to a web-server.
The web-server part is working properly, but I have a problem (I think it`s electrical problem) connecting the card reader to the Arduino.
First i tried just to connect the serial pins 2,3 and +5V and GND pins to the bord, and also sendt power to the card reader on the DTR pin. This work fine if i connect to a computer.
When connecting to an Arduino, the data i recieve on the serial port is garbage, but I sometimes can see part of the expected data comes through. I have configured the correct baud rate.
I noticed that when I`m turning on and of an nearby lamp, the Arduino recieves data !! And the Serial.available is of course trigged.
The CardReader is connected to the Arduino throug a 232 shifter from Sparkfun.
You will probably have to examine the data to see what you are receiving when you get valid data and invalid data and filter the data in software.
Change: Serial.print(inByte, BYTE) to:
Serial.print(inByte, HEX); Serial.print(' ');
That way you can see the hex character codes. Scan a few cards and look for a pattern in the values, for example they might always start and/or end with a low value like 1, A, or D. Let us know what you find.
Edit: One source says the data output format is:
%trackOne?;trackTwo?;trackThree?
The display they show, however, shows the format is more like:
%trackOne?;trackTwo?+trackThree?
So, to get the particular track you want select the data between '%' and '?', ';' and '?', or '+' and '?'.
Edit: Oops. I like a hex display for this stuff, so I changed it.
What platform are you using to program you Arduino? Windows?
And the grounds are connected between the two devices, correct?
Aruduino now sends alot of FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF and dosnt stop.
Added a delay to see more, and this is happening when card is swiped:
Good idea on the delay. Now it will print 10 times a second, and each on its own line.
Mine did the same thing yours did. For some reason, it thinks char is a 32 bit data type. It was returning FFFFFFFF.
Try this. I commented out the Serial1.available() calls to test it.
It is not enough data. And the data is corrupt. I dont think I have a software issue. It seems more like that I have troble with baud rate, or have connected the reader wrong.
is wrong, as the card could also send an 0xFF !! which you will miss.
The solution should be in the proper use of Available(),
furthermore recall that Serial.Read() returns an int not a byte ...
The sparkfun level shifter doesn't seem to support the DTR or RTS signals from which the reader is expecting to get its power supply. Could that be the problem?
If removing the Serial1.available() call and filtering 0xFF characters did not correct the problem, then you cannot assume the Serial1.available() routine is malfunctioning. There may be characters in the receive buffer, and something in your code is interpreting them as 0xFF.
I would now check the baud rates carefully. Insure the Serial1.begin() matches the magnetic reader baud, and Serial.begin() matches the baud rate in the lower right corner of the serial monitor window.
@robtillaart: Thanks for the info on the data size. I did not see the - int at the end of the returns section. If I would have looked at the example, I would have seen it is int. Now it makes sense. 0xFF is not really -1. It is 255. 0xFFFF is an int -1.
My new test code would be:
int inByte;
inByte = Serial1.read();
if(inByte != -1) Serial.write(inByte);
However, in this case I no longer believe it is Serial1.available() that is malfunctioning. It must be something in the serial port settings.