Software Serial problem

I am trying to read a barcode reader but I am getting weird characters rather than the text expected.

The barcode reader is returning the correct text when connected to a Putty terminal session via USB/serial convertor.

When I connect the output to my Arduino, I get non text characters on my LCD and Arduino terminal.

The reader outputs at 9600 baud. I only use the Tx output connected to D2 input.

#include <Wire.h>  
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address

char readChr;
char pageBarCode[40]  ;          // an array to hold publication barcode
int charCounter = 0;             //count position in array
int searchChrNo = 22;             //char in string to search for = 23rd char
int pageColour;                  //read from position in barcode string
boolean newBarCode = false;      //Flag to show new code ready to process

SoftwareSerial mySerial(2, 3);   // RX, TX

void setup()  
{
  Serial.begin(9600);           // Used to type in characters from terminal
  mySerial.begin(9600);
  lcd.begin(16,2);              // initialize the lcd for 16 chars 2 lines, turn on backlight 
}

void loop()   
{
  // when characters arrive over the serial port...
  if (mySerial.available()) 
  { // wait a bit for the entire message to arrive
    delay(100);
    newBarCode = true;                  //there is a new message to process
  }

  if ( newBarCode == true) {
    lcd.clear();           
    lcd.setCursor(0,0);     
 
    while (mySerial.available() > 0 )    // read all the available characters
    {
      readChr = (mySerial.read() );
      lcd.write (readChr);
      Serial.print (readChr);
    } 
    
    newBarCode = false;                //end of processing, reset flag
  }
}

I would appreciate any hints.

Weedpharma

  if (mySerial.available())
  { // wait a bit for the entire message to arrive
    delay(100);
    newBarCode = true;                  //there is a new message to process
  }

What is this rubbish? The delay() will NOT assure that the complete message has arrived.

      lcd.write (readChr);

Why would you use lcd.write() to print a character to the LCD?

    newBarCode = false;                //end of processing, reset flag

Because you've read some of it? Well, OK.

So, why do you suspect SoftwareSerial?

As usual PaulS, straight to the point!!

This code was originated by others and I grabbed bits and pieces from several places while I work out what is needed.

LCD.write was originally a print but did not give the correct result so I just tried something else.

I do not necessarily suspect software serial. Just that is what I am using and it is not doing what I need.

As you can see under my user name, I am a newbie with programming in C.

So I am still in need of any help I can get.

Weedpharma

The barcode reader is returning the correct text when connected to a Putty terminal session via USB/serial convertor.

Perhaps a link to the device would be useful.
Perhaps some details about this USB/serial converter would be useful.
Perhaps a schematic showing how it is wired to the Arduino would be useful.

What I'm thinking now is that the device is a RS232 device, not a TTL device. You might need a MAX232 chip between it and the Arduino.

The reader link is in the OP.

USB/serial converter is a generic with no manufacturer info.

The output is RS232. The reader is powered from 5v. I have assumed the output be 5v so may be something to look at.

I have used the reader Tx and connected it to Arduino D2 as the software serial Rx. Gnd is connected.

The device has a 15 pin connecter and I have taken gnd and Tx off to a DB9.

Manual Page A5

Appendix B — Electrical Specifications
Maximum Operating Power: 2W
Power Input: 5VDC ±5%, 200mV p-p max. ripple, 260 mA @ 5VDC (typical)
Trigger, Input 1, New Master: 5 to 28 VDC rated (optoisolated)
Outputs (1,2,3): 1 to 28VDC rated (optoisolated) (ICE <100mA @24VDC, current limited by user)
Pin Host RS-232 Host & Aux. RS-232 Host RS-422/485 In/Out
1 Power +5VDC In
2 TxD TxD TxD (-) Out
3 RxD RxD RxD (-) In
4 Power/Signal Ground
5 NC
6 RTS Aux. TxD TxD (+) Out
7 Output 1 TTL (can sink 10mA and source 2mA) Out
8 Default Configuration (NPN) In
9 Trigger (NPN) In
10 CTS Aux. RxD RxD (+) In
11 Output 3 TTL (can sink 10mA and source 2mA) Out
12 New Master Pin (NPN) In
13 Chassis Ground
14 Output 2 TTL (can sink 10mA and source 2mA) Out
15 NC

WeedPharma

RS232 levels and TTL levels are inverted. You don't mention a chip like the MAX232, so I guess that that will be the reason.

0V ttl <-> +12V rs232
5V ttl <-> -12V rs232

Thank you both. Looks like I go shopping next week.

Weedpharma

Maybe try the examples in Serial Input Basics - simple reliable ways to receive data.

...R

Robin2:
Maybe try the examples in Serial Input Basics - simple reliable ways to receive data.

...R

weedpharma first needs to get the hardware layer right :wink:

Thanks to the help, I now have the hardware working. As suggested, it needed a Max323.

Now working on the finer points of displaying on the LCD in a meaningful manner and operating an output depending on the letter in a particular position. The parsing and out part I have working.

Thanks again.

WeedPharma