J'ai un scanner barcode USB détecté en tant que clavier sur mon PC.
J'aimerai faire en sorte que ma carte Arduino récupère les caractères du scanner pour les mettre dans une variable, mais je n'ai absolument aucune idée de comment faire.
Pourriez vous m'aider à me diriger sur le programme à faire?
attention tous les scanner n'exportent pas un profil HID "propre" et donc il se peut que le votre fonctionne sur PC mais pas avec la classe par défaut du shield USB
Merci J-M-L,
j'ai utilisé ton code dans le topic anglais(et conservé les include comme dit pour le BCS) et il fonctionne.. enfin presque.
Quand je démarre le moniteur de série il apparait le caractèe "â" ou "ç"
Et quand je scan le code EAN13 "8058333490632" il apparait "ÿúýýêÌÞ’ÿt”ÿÿ”ÿý”ðâ"
Qu'est ce que ça veut dire? Une solution?
#include <avr/pgmspace.h>
#include <Usb.h>
#include <usbhub.h>
#include <avr/pgmspace.h>
#include <hidboot.h>
#include <SPI.h>
#include <hiduniversal.h>
#define MAXQRCODE 50 // NEEDS TO BE LONG ENOUGH TO FIT YOUR LONGEST BARCODE SCAN
class KbdRptParser : public KeyboardReportParser
{
public:
KbdRptParser();
void resetString();
bool newCar;
bool newString;
uint8_t currentCursor;
char QRCodeBuffer[MAXQRCODE + 1];
protected:
virtual void OnKeyDown (uint8_t mod, uint8_t key);
virtual void OnKeyPressed(uint8_t key);
};
KbdRptParser::KbdRptParser(): KeyboardReportParser()
{
resetString();
}
void KbdRptParser::resetString()
{
newCar = false;
newString = false;
currentCursor = 0;
QRCodeBuffer[0] = '\0';
}
void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)
{
uint8_t c = OemToAscii(mod, key);
if (c) OnKeyPressed(c);
}
/* what to do when symbol arrives */
void KbdRptParser::OnKeyPressed(uint8_t key)
{
QRCodeBuffer[currentCursor++] = key;
QRCodeBuffer[currentCursor] = '\0';
if (currentCursor >= MAXQRCODE) currentCursor = MAXQRCODE - 1; // not enough space, next character will overwrite last one
newCar = true;
newString = true;
}
USB Usb;
USBHub Hub(&Usb);
HIDUniversal Hid(&Usb);
KbdRptParser Prs;
const unsigned long timeOutDuration = 200ul;
void setup()
{
Serial.begin(115200);
Serial.println("Start");
if (Usb.Init() == -1) {
Serial.println("OSC did not start. Stop here.");
while (1); // No need to continue.
}
Hid.SetReportParser(0, (HIDReportParser*)&Prs);
}
void loop()
{
static uint32_t lastKeyTime = 0; //watchdog
static bool timedOut = false;
Usb.Task(); // this gives a chance to read from the barcode scanner
if (Prs.newCar) { // we just got a new char; you'll see the string building up
Serial.print("["); Serial.print(Prs.QRCodeBuffer); Serial.println("]");
Prs.newCar = false; // new char has been handled
lastKeyTime = millis(); // memorize the last char entry time
}
// here we check if there has been no activity from the barcode reader for a while
if (Prs.newString && (millis() - lastKeyTime >= timeOutDuration)) { //timeout, we just finsihed entering a new string
Serial.print("Final string = ["); Serial.print(Prs.QRCodeBuffer); Serial.println("]");
Prs.resetString(); // tell the Prs instance that the new string has been handled.
}
}
vous avez vu que mon code contient Serial.print("Final string = ["); Serial.print(Prs.QRCodeBuffer); Serial.println("]");
ça devrait vous donner une idée d'où se trouve le chaîne lue, non?
ensuite pour comparer deux chaînes (tableaux de caractères terminés par '\0') il suffit d'utiliser les fonctions des librairies standard (par exemple [url=http://www.cplusplus.com/reference/cstring/strcmp/]strcmp()[/url] ) que vous trouvez dans stdlib.h et string.h
if (strcmp (barcode,QRCodeBuffer) {
Serial.print("Code bon");
On me donne cette erreur :
BCS.ino: In function 'void loop()':
BCS:98: error: 'QRCodeBuffer' was not declared in this scope
BCS:98: error: expected ')' before '{' token
'QRCodeBuffer' was not declared in this scope