arduarn:
Just for LOLs, here is a possible way you could structure your sketch:
#include <hid.h>
#include <hiduniversal.h>
#include <usbhub.h>
#include <Usb.h>
#include <hidboot.h>
#include <avr/pgmspace.h>
#include <SoftwareSerial.h>
const byte BarcodeLength = 14;
const byte WeightLength = 14;
const byte WeightRxPin = 8;
const byte WeightTxPin = 9;
//
class BarcodeReader : public KeyboardReportParser
{
USB Usb;
USBHub Hub;
HIDUniversal Hid;
HIDBoot<HID_PROTOCOL_KEYBOARD> Keyboard;
uint8_t buffer[BarcodeLength + 1];
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.");
}
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 \n or \r 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] = '\0';
if (length == BarcodeLength) 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);
}
//
class WeightReader
{
SoftwareSerial ss;
uint8_t buffer[WeightLength + 1];
byte length;
bool valid;
public:
WeightReader();
void begin();
void task();
const uint8_t* weight();
void reset();
};
WeightReader::WeightReader()
: ss(WeightRxPin, WeightTxPin)
, length(0)
, valid(false)
{
}
void WeightReader::begin()
{
pinMode(WeightRxPin, INPUT);
pinMode(WeightTxPin, OUTPUT);
ss.begin(9600);
}
void WeightReader::task()
{
if (ss.available()) {
uint8_t c = ss.read();
if (valid) {
// already have a weight, new one will overwrite it
reset();
}
// could check for \n or \r if necessary, but for now we just
// assume fixed width field with no output separators
buffer[length++] = c;
buffer[length] = '\0';
if (length == WeightLength) valid = true;
}
}
// return value no longer valid after another call to task()
const uint8_t* WeightReader::weight()
{
return (valid) ? buffer : 0;
}
void WeightReader::reset()
{
length = 0;
valid = false;
}
//
BarcodeReader barcodeReader;
WeightReader weightReader;
void setup()
{
Serial.begin( 9600 );
Serial.println("Start");
barcodeReader.begin();
weightReader.begin();
}
void loop()
{
barcodeReader.task();
weightReader.task();
const char* barcode = barcodeReader.barcode();
const char* weight = weightReader.weight();
if (barcode && weight) {
Serial.print(barcode);
Serial.print(" ");
Serial.println(weight);
barcodeReader.reset();
weightReader.reset();
}
}
Completely untested, but you should get the idea anyway.
halo i just edit your code, the output from barcode and rs232 appear, but the output from rs232 is very annoying. This is the code.
#include <hid.h>
#include <hiduniversal.h>
#include <usbhub.h>
#include <Usb.h>
#include <hidboot.h>
#include <avr/pgmspace.h>
#include <SoftwareSerial.h>
const byte BarcodeLength = 14;
const byte WeightLength = 17;
const byte WeightRxPin = 2;
const byte WeightTxPin = 3;
//
class BarcodeReader : public KeyboardReportParser
{
USB Usb;
USBHub Hub;
HIDUniversal Hid;
HIDBoot<HID_PROTOCOL_KEYBOARD> Keyboard;
uint8_t buffer[BarcodeLength + 1];
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.");
}
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 \n or \r 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] = '\0';
if (length == BarcodeLength) 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);
}
//
class WeightReader
{
SoftwareSerial ss;
uint8_t buffer[WeightLength + 1];
byte length;
bool valid;
public:
WeightReader();
void begin();
void task();
const uint8_t* weight();
void reset();
};
WeightReader::WeightReader()
: ss(WeightRxPin, WeightTxPin)
, length(0)
, valid(false)
{
}
void WeightReader::begin()
{
pinMode(WeightRxPin, INPUT);
pinMode(WeightTxPin, OUTPUT);
ss.begin(9600);
}
void WeightReader::task()
{
if (ss.available()) {
uint8_t c = ss.read();
if (valid) {
// already have a weight, new one will overwrite it
reset();
}
// could check for \n or \r if necessary, but for now we just
// assume fixed width field with no output separators
buffer[length++] = c;
buffer[length] = '\0';
if (length == WeightLength) valid = true;
}
}
// return value no longer valid after another call to task()
const uint8_t* WeightReader::weight()
{
return (valid) ? buffer : 0;
}
void WeightReader::reset()
{
length = 0;
valid = false;
}
//
BarcodeReader barcodeReader;
WeightReader weightReader;
void setup()
{
Serial.begin( 9600 );
Serial.println("Start");
barcodeReader.begin();
weightReader.begin();
}
void loop()
{
barcodeReader.task();
weightReader.task();
const char* barcode = barcodeReader.barcode();
const char* weight = weightReader.weight();
if (barcode && weight) {
Serial.print(barcode);
Serial.print(" ");
Serial.println(weight);
barcodeReader.reset();
weightReader.reset();
}
}
, everytime i open serial monitor its is changing (i open serial monitor >> scan the barcode >> weighing the something>> the output is changing (you can see the display of serial monitor 1 & 2 in below here
this is serial monitor 1
Start
4547648730754 0074.25 g
ST,+0
4548583108066 0074.25 g
ST,+0
4547648730754 0074.25 g
ST,+0
4548583108066 0074.25 g
ST,+0
and this is serial monitor 2
Start
4548583108066 00074.25 g
ST,+
4547648730754 00074.25 g
ST,+
4548583108066 00074.25 g
ST,+
4547648730754 00074.25 g
ST,+
4548583108066 00074.25 g
ST,+
4547648730754 00074.25 g
ST,+
can you explain me all about the barcode scanner code? because it's very confused to understand your code.
after i understand the code of barcode scanner, my planning is edit the code of barcode scanner for make the code of weighing scale.
thank you....