Arduino的条形码阅读器识别char

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Arduino的条形码阅读器识别char相关的知识,希望对你有一定的参考价值。

我有一个条形码代码,但需要它识别条形码,以便它可以打印到串行监视器。我已经搜索过我需要将它从char转换为int但是如果答案是这样的话还没有找到办法。

例如,我扫描条形码,如果是某个数字,则串行监视器将打印它。

#include <hid.h>
#include <hiduniversal.h>
#include <usbhub.h>
#include <Usb.h>
#include <hidboot.h>
#include <avr/pgmspace.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Bridge.h>
#include <FileIO.h>
#include <TM1637Display.h>

#define DEBUG true

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

const byte BarcodeLength = 11;
const int buzzer = 2;


const int CLK = 9; //Set the CLK pin connection to the display
const int DIO = 8; //Set the DIO pin connection to the display
const int DIO2 = 7; // Set 2nd DIO pin to second connection to display
const int CLK2 = 6; // Set the 2nd CLK pin to the second connection to display. 
const int procTime = 5; // this is the time it takes for the process to finish countdown


int NumStep = 0;  //Variable to interate

TM1637Display countdownDisplay(CLK, DIO);  //set up the 4-Digit Display.
TM1637Display DelayedDisplay(CLK2, DIO2);  //set up the 4-Digit Display.


class BarcodeReader : public KeyboardReportParser
{
    USB Usb;
    USBHub Hub;
    HIDUniversal Hid;
    HIDBoot<HID_PROTOCOL_KEYBOARD> Keyboard;

    uint8_t buffer[15];
    byte length;
    bool valid;

    void addCharacter(uint8_t c);

  public:
    BarcodeReader();
    void begin();
    void task();
    const uint8_t* barcode();
    void reset();

  protected:
    virtual void OnKeyDown(uint8_t mod, uint8_t key);
};

BarcodeReader::BarcodeReader()
  : Usb()
  , Hub(&Usb)
  , Hid(&Usb)
  , Keyboard(&Usb)
  , length(0)
  , valid(false)
{
}

void BarcodeReader::begin()
{
  if (Usb.Init() == -1) {
    Serial.println("OSC did not start.");
  }
   else 
 Serial.println("Barcode Ready");

  Hid.SetReportParser(1, this);
}

void BarcodeReader::task()
{
  Usb.Task();
}

void BarcodeReader::addCharacter(uint8_t c)
{
  if (valid) {
    // already have a barcode, new one will overwrite it
    reset();
  }

  // could check for 
 or 
 if necessary, or even implement a timing
  // mechanism for detecting end of barcode input, but for now we just
  // assume fixed width field with no output separators

  buffer[length++] = c;
  //buffer[length] = '';

  if (buffer[length] == '') 
  {
    valid = true;
  }
};

// return value no longer valid after another call to task()
const uint8_t* BarcodeReader::barcode()
{
  return (valid) ? buffer : 0;
}

void BarcodeReader::reset()
{

  length = 0;
  valid = false;
}

void BarcodeReader::OnKeyDown(uint8_t mod, uint8_t key) {
  uint8_t c = OemToAscii(mod, key);
  if (c) addCharacter(c);
}

BarcodeReader barcodeReader;

void setup() {

  countdownDisplay.showNumberDec(0);
  DelayedDisplay.showNumberDec(0); 

 Bridge.begin();
  Console.begin();
  FileSystem.begin();
  Serial.begin(9600);
  if (DEBUG) Console.println("Done setup");



  lcd.begin(16, 2);
  pinMode(buzzer,OUTPUT);
  //Serial.begin(115200); 

   lcd.print("Please Scan Your ID: ");
  barcodeReader.begin();


  countdownDisplay.setBrightness(0x0a);  //set the diplay to maximum brightness
  DelayedDisplay.setBrightness(0x0a);
 }

void loop() {
  //char *goodCards = { "1234567" };
 //String dataString = "Testing, 1, 2, 3...";


  File dataFile = FileSystem.open("/mnt/sda1/datalog.txt", FILE_APPEND);
  // If the file opens successfully, write the string to it, close the file,
  // and print the information to the Serial monitor.


  barcodeReader.task();
  const char* barcode = barcodeReader.barcode();
  if  (barcode || dataFile) {  

        lcd.print(barcode);
         dataFile.print(barcode);
        Serial.print(barcode);  
        barcodeReader.reset();
         dataFile.close(); 
//If the barcode is the word 'Setup' then print setup to the screen
//search array of char conver strcmp(). You are sending 0101 not the actual value of 1234567
// collect all the input, convert it to an int
          if(barcode == "0001") {
             {

    Serial.println(": SETUP");

  }
    }   

  }


//timer and barcode


//
// if(barcode == "1234567") {
//    Serial.print(barcode);
//    Serial.println(": SETUP");
// 
////     analogWrite(A1,0);
////    analogWrite(A2,0);
//  }
//   if(barcode == "KAYAMAD") {
//    Serial.print(barcode);
//    Serial.println(": RUN");
//    
////     analogWrite(A0,0);
////    analogWrite(A2,0);
//       countdownDisplay.showNumberDec(0);
//  DelayedDisplay.showNumberDec(0);
//      for(int NumStep = procTime; NumStep > 0 ; NumStep--)  //Interrate NumStep
//  {
//    countdownDisplay.showNumberDec(NumStep); //Display the Variable value;
//    //DelayedDisplay.showNumberDec(NumStep); //Display the Variable value;
//    delay(1000);  //A second delay between steps.
//  }
//  
//      for (int NumStep = 0; NumStep < 5 ; NumStep++)  //Interrate NumStep
//  {
//    //countdownDisplay.showNumberDec(NumStep); //Display the Variable value;
//    DelayedDisplay.showNumberDec(NumStep); //Display the Variable value;
//    countdownDisplay.showNumberDec(0);
//    delay(1000);  //A second delay between steps.
//    
//  }
//    
//  }
//  
//  else if (barcode == "2234663") {
//    Serial.print(barcode);
//    Serial.println(": DELAY");
//   
////      analogWrite(A0,0);
////    analogWrite(A1,0);
//     countdownDisplay.showNumberDec(0);
//  DelayedDisplay.showNumberDec(0);
//  
//
//      for (int NumStep = 0; NumStep < 5 ; NumStep++)  //Interrate NumStep
//  {
//    //countdownDisplay.showNumberDec(NumStep); //Display the Variable value;
//    DelayedDisplay.showNumberDec(NumStep); //Display the Variable value;
//    countdownDisplay.showNumberDec(0);
//    delay(1000);  //A second delay between steps.
//    
//  }
//
//
//  }
//  

  // If there is a problem opening the file, send an error to the Serial monitor
  else {
    if (DEBUG) Console.println("Error opening datalog.txt");
  }

}
答案

你得到的“数字”计算机看作文本,而不是数字。我们确信条形码始终是数字。使用atoi函数将conver char转换为int。这是一个例子:

const char *bar = "123";
int bc = atoi(bar);

以上是关于Arduino的条形码阅读器识别char的主要内容,如果未能解决你的问题,请参考以下文章

水果图像识别:基于 Arduino 和 TensorFlow Lite Micro

arduino String转char *

VB 识别条形码

如何将串行输入存储在char数组arduino中?

请教各位大神语音识别模块LD3320怎么与arduino连

java怎么条形码识别,识别图片中的条形码。