我正在尝试使条形码扫描仪与Arduino Mega一起工作(我正在使用Honeywell Eclipse 5145)尽管我从另外两个问题中获得了帮助,但我似乎仍然无法使其完全正常工作。
我让条形码扫描仪正确地将条形码编号发送到串行监视器中,但是我试图将If语句添加到变量中,以便这样
If ([Placeholder]==(char)ascii {
digitalWrite(22, HIGH);
delay(1000);
digitalWrite(22, LOW);
}
但是这不起作用。有人告诉我函数ascii一次只存储一个字符,因此它将一个接一个地输出字符,而不是一个字符串。我已经尝试解决了一段时间,但似乎无法使其正常工作。我也不知道我应该把代码放在我的项目中。
这是我当前的代码,
#include <usbhid.h>
#include <usbhub.h>
#include <hiduniversal.h>
#include <hidboot.h>
#include <SPI.h>
char scan;
//Barcodes
char Barcode1[] = {'7','6','2','2','2','1','0','4','1','1','0','0','6','\0'};
char Barcode2[] = {"8717644012208 "};
class MyParser : public HIDReportParser {
public:
MyParser();
void Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
protected:
uint8_t KeyToAscii(bool upper, uint8_t mod, uint8_t key);
virtual void OnKeyScanned(bool upper, uint8_t mod, uint8_t key);
virtual void OnScanFinished();
};
MyParser::MyParser() {}
void MyParser::Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) {
if (buf[2] == 1 || buf[2] == 0) return;
for (uint8_t i = 7; i >= 2; i--) {
if (buf[i] == 0) continue;
if (buf[i] == UHS_HID_BOOT_KEY_ENTER) {
OnScanFinished();
}
else {
OnKeyScanned(i > 2, buf, buf[i]);
}
return;
}
}
uint8_t MyParser::KeyToAscii(bool upper, uint8_t mod, uint8_t key) {
if (VALUE_WITHIN(key, 0x04, 0x1d)) {
if (upper) return (key - 4 + 'A');
else return (key - 4 + 'a');
}
else if (VALUE_WITHIN(key, 0x1e, 0x27)) {
return ((key == UHS_HID_BOOT_KEY_ZERO) ? '0' : key - 0x1e + '1');
}
return 0;
}
void MyParser::OnKeyScanned(bool upper, uint8_t mod, uint8_t key) {
uint8_t ascii = KeyToAscii(upper, mod, key);
Serial.print((char)ascii);
scan = (char)ascii;
}
void MyParser::OnScanFinished() {
Serial.println(" - Finished");
}
USB Usb;
USBHub Hub(&Usb);
HIDUniversal Hid(&Usb);
MyParser Parser;
void setup() {
Serial.begin( 115200 );
Serial.println("Start");
if (Usb.Init() == -1) {
Serial.println("OSC did not start.");
}
delay( 200 );
Hid.SetReportParser(0, &Parser);
}
void loop() {
Usb.Task();
}
我想我某种程度上需要一个变量,当从函数ascii]接收到所有字符时,该变量将存储数据,然后在If语句而不是(char)ascii
中使用该变量。如果您有任何疑问,请在下面评论,我会尽力回答(我对ascii
不太熟悉)
如果可能,请提供示例
谢谢,
[我正在尝试使条形码扫描仪与Arduino Mega一起工作(我正在使用Honeywell Eclipse 5145),尽管我从其他两个问题中获得了帮助,但似乎仍然无法正常工作.. 。
class MyParser : public HIDReportParser {
public:
MyParser();
void Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
protected:
char KeyToAscii(bool upper, uint8_t mod, uint8_t key);
virtual void OnKeyScanned(bool upper, uint8_t mod, uint8_t key);
virtual void OnScanFinished();
};
char MyParser::KeyToAscii(bool upper, uint8_t mod, uint8_t key) {
if (VALUE_WITHIN(key, 0x04, 0x1d)) {
if (upper) return (key - 4 + 'A');
else return (key - 4 + 'a');
}
else if (VALUE_WITHIN(key, 0x1e, 0x27)) {
return ((key == UHS_HID_BOOT_KEY_ZERO) ? '0' : key - 0x1e + '1');
}
return 0;
}