How to display output barcode + output data from rs232 device

hai guys, I have new question again and again.

i want to connect barcode scanner to arduino, and rs232 device (weighing scale) to arduino) and display data to serial monitor like this(4547648730101 ST,+00000.00 g) (4547648730101 from barcode scanner and ST,+00000.00 g from weighing scale).

this is my code for barcode scanner, i use usb host shield library 2.0 version 1.1.1...

#include <hid.h>                           //Add to Oleg Mazurov code to Bar Code Scanner
#include <hiduniversal.h>                  //Add to Oleg Mazurov code to Bar Code Scanner
#include <usbhub.h>

#include <avr/pgmspace.h>
#include <Usb.h>
#include <usbhub.h>
#include <avr/pgmspace.h>
#include <hidboot.h>
#define DISPLAY_WIDTH 16
 
USB     Usb;
USBHub     Hub(&Usb);                                          //I enable this line
HIDUniversal      Hid(&Usb);                                  //Add this line so that the barcode scanner will be recognized, I use "Hid" below 
HIDBoot<HID_PROTOCOL_KEYBOARD>    Keyboard(&Usb);
 
class KbdRptParser : public KeyboardReportParser
{
        void PrintKey(uint8_t mod, uint8_t key);             // Add this line to print character in ASCII
protected:
  virtual void OnKeyDown  (uint8_t mod, uint8_t key);
};
 
void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)  
{
    uint8_t c = OemToAscii(mod, key);
    if (c)
        Serial.print((char)c);
}

KbdRptParser Prs;
 
void setup()
{
    Serial.begin( 2400 );
    Serial.println("Start");
 
    if (Usb.Init() == -1) {
        Serial.println("OSC did not start.");
    }
    Hid.SetReportParser(0, (HIDReportParser*)&Prs);        //Here I change  "Keyboard" for "Hid"

}
 
void loop()
{
  Usb.Task();
}

and for rs232 device the code is...

#include <SoftwareSerial.h>

#define rxPin 8
#define txPin 9

// set up a new serial port
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
int i=1;
int x;
char c;
String Berat;

void setup()  {

  
  // define pin modes for tx, rx:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  // set the data rate for the SoftwareSerial port
  Serial.begin(9600);
  mySerial.begin(9600);
}

void loop() 
{
 for (i=0;i < 17;i++){
  if (mySerial.available()) {
  c = mySerial.read();
  Berat = String(Berat +c);
 if (Berat.length()>=17){;
 Serial.print(Berat);

 Berat ="";
 }
 
 }}

}

and my question is, how to join two code here to display data to serial monitor like this(4547648730101 ST,+00000.00 g), its very difficult to me because i newbie in arduino device, thank you

Can you explain a little more about what your overall project is so we can get a better idea of what it encompasses?

arduarn:
Can you explain a little more about what your overall project is so we can get a better idea of what it encompasses?

i want to display a data from barcode scanner and data from rs232 device (weighing scale).
example of data from barcode scanner is 8994096215986
example of data from rs232 device (weighing scale) is ST,+00000.00 g
so the output is (89940962151986 ST,+00000.00 g)

so how my project work is, if i scan the barcode and i weighing something in my weighing scale, the display output is example (89940962151986 ST,+00000.00 g)
and if i scan again the barcode and i weighing something in my weighing scale, the display output is example (89940962151986 ST,+00000.17 g)
the barcode and the weight is fluctuative, depend of the user

i have two code:

  1. the barcode scanner code
#include <hid.h>                           //Add to Oleg Mazurov code to Bar Code Scanner
#include <hiduniversal.h>                  //Add to Oleg Mazurov code to Bar Code Scanner
#include <usbhub.h>

#include <avr/pgmspace.h>
#include <Usb.h>
#include <usbhub.h>
#include <avr/pgmspace.h>
#include <hidboot.h>
#define DISPLAY_WIDTH 16

int i=1;
char c;
String Barcode;
  
USB     Usb;
USBHub     Hub(&Usb);                                          //I enable this line
HIDUniversal      Hid(&Usb);                                  //Add this line so that the barcode scanner will be recognized, I use "Hid" below 
HIDBoot<HID_PROTOCOL_KEYBOARD>    Keyboard(&Usb);
 
class KbdRptParser : public KeyboardReportParser
{
        void PrintKey(uint8_t mod, uint8_t key);             // Add this line to print character in ASCII
protected:
  virtual void OnKeyDown  (uint8_t mod, uint8_t key);
  virtual void OnKeyPressed(uint8_t key);
};
 
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)  
{
  for (i=0;i < 1;i++){
  c = ((char)key);
  Barcode = String(Barcode +c);
 if (Barcode.length()>=14){;
 Serial.print(Barcode);
 Barcode="";

}}};
 
KbdRptParser Prs;
 
void setup()
{
    Serial.begin( 9600 );
    Serial.println("Start");
 
    if (Usb.Init() == -1) {
        Serial.println("OSC did not start.");
    }
 
    Hid.SetReportParser(1, (HIDReportParser*)&Prs);        //Here I change  "Keyboard" for "Hid"

}
 
void loop()
{
  Usb.Task();
}
  1. rs232 code
#include <SoftwareSerial.h>

#define rxPin 8
#define txPin 9

// set up a new serial port
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
int i=1;
int x;
char c;
String Berat;

void setup()  {

  
  // define pin modes for tx, rx:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  // set the data rate for the SoftwareSerial port
  Serial.begin(9600);
  mySerial.begin(9600);
}

void loop() 
{
 for (i=0;i < 17;i++){
  if (mySerial.available()) {
    //Serial.println("SSS");
  c = mySerial.read();
  Berat = String(Berat +c);
 if (Berat.length()>=17){;
 Serial.print(Berat);

 Berat ="";
 }
 
 }}

}

i want to connect barcode scanner to arduino, and rs232 device

The Arduino inputs are TTL level (5V) devices but TTL uses 12V. Some will work and not cause a problem or damage but to make sure you should use a level shifter between the Arduino and the RS232 device.

UKHeliBob:
The Arduino inputs are TTL level (5V) devices but TTL uses 12V. Some will work and not cause a problem or damage but to make sure you should use a level shifter between the Arduino and the RS232 device.

i use rs232 to TTL, actually i dont have problem with hardware because barcode scanner to arduino and rs232 device to arduino is working.
i have a problem with the code of arduino connect to barcode scanner and rs232 device.

That sounds OK.

Have you tried to combine the programs ?

UKHeliBob:
That sounds OK.

Have you tried to combine the programs ?

yeah i try this code, and the result just a data from barcode scanner, not a data from barcode scanner and data from rs 232 device

#include <hid.h>                           //Add to Oleg Mazurov code to Bar Code Scanner
#include <hiduniversal.h>                  //Add to Oleg Mazurov code to Bar Code Scanner
#include <usbhub.h>

#include <avr/pgmspace.h>
#include <Usb.h>
#include <usbhub.h>
#include <avr/pgmspace.h>
#include <hidboot.h>
#define DISPLAY_WIDTH 16

#include <SoftwareSerial.h>

#define rxPin 8
#define txPin 9

// set up a new serial port
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);

int i=1;
char c;
String Barcode;


int j=1;
char d;
String Berat;

  
USB     Usb;
USBHub     Hub(&Usb);                                          //I enable this line
HIDUniversal      Hid(&Usb);                                  //Add this line so that the barcode scanner will be recognized, I use "Hid" below 
HIDBoot<HID_PROTOCOL_KEYBOARD>    Keyboard(&Usb);
 
class KbdRptParser : public KeyboardReportParser
{
        void PrintKey(uint8_t mod, uint8_t key);             // Add this line to print character in ASCII
protected:
  virtual void OnKeyDown  (uint8_t mod, uint8_t key);
  virtual void OnKeyPressed(uint8_t key);
};
 
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)  
{
  for (i=0;i < 1;i++){
  c = ((char)key);
  Barcode = String(Barcode +c);
 if (Barcode.length()>=14){;
 Serial.print(Barcode);
 Barcode="";}}
 //Serial.print( (char)key );      //Add char to print correct number in ASCII
  for (j=0;j < 17;j++){
  if (mySerial.available()) {
    //Serial.println("SSS");
  d = mySerial.read();
  Berat = String(Berat +d);
 if (Berat.length()>=17){;
 Serial.print(Berat);
 Berat ="";
 }
 }}
};
 
KbdRptParser Prs;
 
void setup()
{
   // define pin modes for tx, rx:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  mySerial.begin(9600);
    Serial.begin( 9600 );
    Serial.println("Start");
 
    if (Usb.Init() == -1) {
        Serial.println("OSC did not start.");
    }
 
    Hid.SetReportParser(1, (HIDReportParser*)&Prs);        //Here I change  "Keyboard" for "Hid"

}
 
void loop()
{

     
  Usb.Task();
  

}

the result just a data from barcode scanner, not a data from barcode scanner and data from rs 232 device

void loop()
{
  Usb.Task();
}

I am not clear how in the program you read from the SoftSerial input and why reading from it is not in its own function.

UKHeliBob:

void loop()

{
  Usb.Task();
}



I am not clear how in the program you read from the SoftSerial input and why reading from it is not in its own function.

nahhh, i really confused about where i place the code of rs232 in my code of barcode scanner, so i place there.
and my question is how to combined this program(my barcode code and my rs232 code?

i really confused about where i place the code of rs232 in my code of barcode scanner,

Don't put it in your code for the barcode scanner, keep it separate.

Have one function read the scanner into variables
Have a second function read the RS232 data into variables
Have a third function display the variables

void loop()
{
  readScanner();
  readScales();
  displayResults();
}

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.

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.

your code can't work, but i edit your code and the output just a barcode cant display the weighing scale.
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 = 14;
const byte WeightRxPin = 8;
const byte WeightTxPin = 9;

int i=1;
char c,d;
String Barcode,Berat;
//

class BarcodeReader : public KeyboardReportParser
{
    USB     Usb;
    USBHub     Hub;                                          //I enable this line
    HIDUniversal      Hid;                                  //Add this line so that the barcode scanner will be recognized, I use "Hid" below 
    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);
    virtual void OnKeyPressed(uint8_t key);
};

BarcodeReader::BarcodeReader()
  : Usb()
  , Hub(&Usb)
  , Hid(&Usb)
  , Keyboard(&Usb)
  , 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);
}

void BarcodeReader::OnKeyPressed(uint8_t key)  
{
  for (i=0;i < 1;i++){
  c = ((char)key);
  Barcode = String(Barcode +c);
 if (Barcode.length()>=14){;
 Serial.print(Barcode);
 Barcode="";
 }}}
//

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 d = ss.read();
    Berat = String(Berat +d);
 if (Berat.length()>=17){;
 Serial.print(Berat);
 }}
 
    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();
  }
}

Have you got code that reads the scanner data into variables ? Yes
Put it into a function

Have you got code that reads the scale data into variables ? Yes
Put it into a function

Create a function to display both sets of data

Then follow the advice in reply #9

UKHeliBob:
Have you got code that reads the scanner data into variables ? Yes
Put it into a function

Have you got code that reads the scale data into variables ? Yes
Put it into a function

Create a function to display both sets of data

Then follow the advice in reply #9

#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;

#define rxPin 8
#define txPin 9

// set up a new serial port
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);

int i,j=1;
char c,d;
String Barcode,Berat;
//

class BarcodeReader : public KeyboardReportParser
{
    USB     Usb;
    USBHub     Hub;                                          //I enable this line
    HIDUniversal      Hid;                                  //Add this line so that the barcode scanner will be recognized, I use "Hid" below 
    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);
    virtual void OnKeyPressed(uint8_t key);
};

BarcodeReader::BarcodeReader()
  : Usb()
  , Hub(&Usb)
  , Hid(&Usb)
  , Keyboard(&Usb)
  , 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);
}

void BarcodeReader::OnKeyPressed(uint8_t key)  
{
  for (i=0;i < 1;i++){
  c = ((char)key);
  Barcode = String(Barcode +c);
 if (Barcode.length()>=14){;
 Serial.print(Barcode);
 Barcode="";
 }}}

BarcodeReader barcodeReader;


void setup()
{
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);

  mySerial.begin(9600);
  Serial.begin( 9600 );
  Serial.println("Start");

  barcodeReader.begin();
}

void timbangan()
{
  for (j=0;j < 17;j++){
  if (mySerial.available()) {
  d = mySerial.read();
  Berat = String(Berat +d);
 if (Berat.length()>=17){;
      Serial.println(Berat);
      }
  }
}

}


void loop()
{
  barcodeReader.task();
  
  const char* barcode = barcodeReader.barcode();
  if (barcode){
    Serial.print(barcode);
    Serial.print(" ");
      timbangan ();
    barcodeReader.reset();
  }
Berat="";
  
  }

this is my new code for your advice, and my question is why the arduino cant get data from rs232.
arduino uno can connect both of rs232 and usb host module? i think its my problem but i not sure.

thanks

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....

riandanualdy:
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.

Glad you took another look at the code. Your post #11 stating that it "can't work" but giving no reason or explanation why was rather unhelpful.

riandanualdy:
this is serial monitor 1

Start

4547648730754 0074.25  g
ST,+0
4548583108066 0074.25  g
...




and this is serial monitor 2


Start
4548583108066 00074.25  g
...

Ok, good; you basically have the code working. I assume you really are scanning two different barcodes, so that is not an error?
As it turns out, the scales are outputting ST,+00000.00 g but with a carriage return line feed \r\n at the end. These special characters are your record separators.
At the moment, you just begin reading weights at some random point in the serial data stream; that's why the weight display looks offset.
What you need to do is use the record separator(s) to determine the beginning or end of each record:

Set WeightLength to 15, so that you are just expecting and storing the 15 visible characters and spaces.

Now in WeightReader::task() you want to check each character coming in:

  • if it is a \r then simply ignore it.
  • if it is a newline \n then you want to check if you have read in 15 characters already, and if so, set valid to true; if you haven't read 15 characters, then you must have missed the start of the weight, so discard the buffer contents.
  • if it is any other character, and you still have space in the buffer, you want to add it to the buffer.

Try to implement this yourself first. I'll post how I might do it in my next post.

riandanualdy:
can you explain me all about the barcode scanner code? because it's very confused to understand your code.

Sure. Quick and dirty comments inline:

#include <usbhid.h>
#include <hiduniversal.h>
#include <usbhub.h>
#include <Usb.h>
#include <hidboot.h>
#include <avr/pgmspace.h>

const byte BarcodeLength = 14;


//

// try http://www.penguinprogrammer.co.uk/c-beginners-tutorial/classes/ as an intro to classes
// but there are loads of other websites that can explain it in other words
class BarcodeReader : public KeyboardReportParser
{
    USB Usb;     // since this USB stuff is only used by the barcode reader, I moved
    USBHub Hub;  // it inside of the BarcodeReader class for neatness
    HIDUniversal Hid;
    HIDBoot<USB_HID_PROTOCOL_KEYBOARD> Keyboard;

    uint8_t buffer[BarcodeLength + 1]; // avoid the String class, this is a character array for a C style string; the +1 is for the terminating null \0
    byte length; // used to keep track of how many characters have been read already
    bool valid; // set to true when buffer contains a complete barcode

    void addCharacter(uint8_t c);

  public: // the stuff above is automatically "private" and could have been expicitly labelled so
    BarcodeReader(); // constructor
    void begin();
    void task();
    const uint8_t* barcode();
    void reset();

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

BarcodeReader::BarcodeReader()
  : Usb()       // this is how class object variables are initialised
  , Hub(&Usb)   // etc ...
  , 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();
}

// each time this function is called, it adds one character to the buffer
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; // this line is equivalent to:
  // buffer[length] = c;
  // length = length + 1;
  buffer[length] = '\0'; // add the terminating null character which makes the char array a C style string

  if (length == BarcodeLength) valid = true;
};

// return value no longer valid after another call to task()
const uint8_t* BarcodeReader::barcode()
{
  // return a null pointer (=0) if no barcode read, and a pointer to the barcode if barcode read
  return (valid) ? buffer : 0; // ternary operator, is the same as:
  // if (valid == true) return buffer;
  // else return 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()
{
  Serial.begin( 9600 );
  Serial.println("Start");

  barcodeReader.begin();
}

void loop()
{
  barcodeReader.task(); // check for keypresses

  const char* barcode = barcodeReader.barcode(); // is barcode ready?

  if (barcode) { // yes (any non zero value evaluates to true)
    Serial.println(barcode); // print it
    barcodeReader.reset(); // delete it
  }
}

Untested:

const byte WeightLength = 15;

void WeightReader::task()
{
  if (ss.available()) {
    uint8_t c = ss.read();
    if (valid) {
      // already have a weight, new one will overwrite it
      reset();
    }

    switch (c) {
      case '\r': // discard carriage return
        break;
      case '\n': // end of weight
        if (length == WeightLength) {
          valid = true;
        } else {
          // missed the beginning of the weight
          reset();
        }
        break;
      default: // store weight characters
        if (length < WeightLength) {
          buffer[length++] = c;
          buffer[length] = '\0';
        } else {
          // unexpected output from scales; should never happen
          reset();
        }
    }
  }
}

arduarn:
Untested:

const byte WeightLength = 15;

void WeightReader::task()
{
  if (ss.available()) {
    uint8_t c = ss.read();
    if (valid) {
      // already have a weight, new one will overwrite it
      reset();
    }

switch (c) {
      case '\r': // discard carriage return
        break;
      case '\n': // end of weight
        if (length == WeightLength) {
          valid = true;
        } else {
          // missed the beginning of the weight
          reset();
        }
        break;
      default: // store weight characters
        if (length < WeightLength) {
          buffer[length++] = c;
          buffer[length] = '\0';
        } else {
          // unexpected output from scales; should never happen
          reset();
        }
    }
  }
}

sorry i forget to tell you the weighing data is "|S|T|,|+|0|0|0|0|0|.|0|0| | |g|Cr|Lf|", its mean CR is Carriage return and Lf is Line feed ?

Wow, i edit my code and copy your code to my code and its work. can you explain your last code for weighing ?

and you tell me about "What you need to do is use the record separator(s) to determine the beginning or end of each record:", in this code you share to me, what is the record separator for determine the beginning/end of each record?

thank you

Re-read my previous post #15 a few times. Your questions are already answered there.
Also work your way through the code until you understand it before doing anything else.