SoftwareSerial available() return always 0

Hi

I am making an automatic weight logger so i need communication from weighting scale to arduino using rs232 to ttl converter

My weight scale delivers 8 bits data at 9600 baud rate with no parity and "1" stop bit

But It is SoftwareSerial available () returned always 0 I try to connect a lot of times So can somebody help me?
Thank you

My arduino code is :

#include <Arduino.h>
#include <AltSoftSerial.h>


#define RXPIN 8
#define TXPIN 9


AltSoftSerial mySerial(RXPIN, TXPIN);


void setup() {
 Serial.begin(9600);


 pinMode(LED_BUILTIN, OUTPUT);


 while(!Serial){
   digitalWrite(LED_BUILTIN, HIGH);
   ;
 }
 pinMode(RXPIN, INPUT);
 pinMode(TXPIN, OUTPUT);


 mySerial.begin(9600);
}


void loop() {
 while(mySerial.available()){
   int c = mySerial.read();
   Serial.println(c);
 }
}

and here is my wiring

Is the scale sending data by itself ? Or should you send a command to the scale first ?

Some serial converters have the right 'RX' and 'TX' labels, some have them wrong. Try exchanging the RX and TX connections.
If that helps, put is as comment in your sketch.

There are a few improvements that can be made to your sketch. Can you do that and show the sketch ?

  • Remove #include <Arduino.h>, that is not needed for a sketch.
  • You don't have to wait for Serial with an Arduino Uno in setup(). You can remove the while(!Serial)...
  • You don't have to set the RX pin to input and the TX pin to output. The AltSoftSerial library takes care of that.
  • In setup(), you could send a message to the serial monitor ("hello", or "sketch has started" or "the scale sketch", so you know that the sketch has started and the serial communication is working.
  • You have a while-statement in the loop(). That is okay, but you can also put a if-statement there and handle only one byte.
  • When the Serial.println() prints a integer, it prints a number. I think you need Serial.println( (char) c);

Koepel:
Is the scale sending data by itself ? Or should you send a command to the scale first ?

Some serial converters have the right 'RX' and 'TX' labels, some have them wrong. Try exchanging the RX and TX connections.
If that helps, put is as comment in your sketch.

There are a few improvements that can be made to your sketch. Can you do that and show the sketch ?

  • Remove #include <Arduino.h>, that is not needed for a sketch.
  • You don't have to wait for Serial with an Arduino Uno in setup(). You can remove the while(!Serial)...
  • You don't have to set the RX pin to input and the TX pin to output. The AltSoftSerial library takes care of that.
  • In setup(), you could send a message to the serial monitor ("hello", or "sketch has started" or "the scale sketch", so you know that the sketch has started and the serial communication is working.
  • You have a while-statement in the loop(). That is okay, but you can also put a if-statement there and handle only one byte.
  • When the Serial.println() prints a integer, it prints a number. I think you need Serial.println( (char) c);

The weight scale is sending weight data always when the power is on And I tried to exchange rx pin and tx pin
And I changed my code according to your quote but mySerial.available() returned 0 so if statement does not working and Serial.println("Test begin") is working now
Thank you for your reply!
My code is :

#include <AltSoftSerial.h>
#define RXPIN 10
#define TXPIN 11
AltSoftSerial mySerial(RXPIN, TXPIN);
void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);
  Serial.println("Test begin");
}
void loop() {
  char c;
  if(mySerial.available()){
    c = mySerial.read();
    Serial.print(c);
  }
}

A common problem is that the Rx and Tx lines from the scale to the RS232-to-TTL converter are reversed. Is there any way you can swap pins 2 & 3 from the cable to the RS232-to-TTL converter?

david_2018:
A common problem is that the Rx and Tx lines from the scale to the RS232-to-TTL converter are reversed. Is there any way you can swap pins 2 & 3 from the cable to the RS232-to-TTL converter?

your meaning is to swap RS232 pins 2&3 from weighting scale to RS TTL converter
Right?
Right?

david_2018 is right, sometimes a RS-232 is not as expected. Sometimes the signal is inverted, sometimes Rx and Tx are swapped, sometimes the voltage levels are too low, and so on.
Let's assume the scale serial RS-232 is not so bad. Keep that as it is.

The bug is in the sketch, AltSoftSerial can not set pin numbers.

Start by reading this: AltSoftSerial Library, for an extra serial port.
I like the AltSoftSerial, but it has fixed pin numbers. Pin 9 is always TX and pin 10 is always RX.

The library itself is here: GitHub - PaulStoffregen/AltSoftSerial: Software emulated serial using hardware timers for improved compatibility.
The best way to install it is with the Library Manager of the Arduino IDE.

Did you install the latest version of the AltSoftSerial with the Library Manager ?

Replace this:

#define RXPIN 10
#define TXPIN 11
AltSoftSerial mySerial(RXPIN, TXPIN);

With this:

// The AltSoftSerial uses a hardware timer and has fixed pins for RX and TX.
// For an Arduino Uno: RX is pin 8, TX is pin 9
AltSoftSerial mySerial;

Then connect it like this:

RS-232-to-ttl-module TX <-> Arduino RX pin 8
RS-232-to-ttl-module RX <-> Arduino TX pin 9

If that does not work, then swap RX and TX.

P.S.: This is just a test to check if you put the comment lines in your sketch as well :wink:

pretzelell:
The weight scale is sending weight data always when the power is on

Did you verify this, and if so, how? What was the data you received that way?

wvmarle:
Did you verify this, and if so, how? What was the data you received that way?

I have checked data in futty from rs232 to usb
In Futty sent right data

Here is data format

Hi,
Can you please try this edited version of your code please.

#include <AltSoftSerial.h>
#define RXPIN 10
#define TXPIN 11
AltSoftSerial mySerial(RXPIN, TXPIN);
void setup()
{
  Serial.begin(9600);
  mySerial.begin(9600);
  Serial.println("Test begin");
}
void loop()
{
  char c;
  if (mySerial.available())
  {
    Serial.print("Data Recieved  ");
    c = mySerial.read();
    Serial.println(c);
  }
}

Thanks.. Tom... :slight_smile:

TomGeorge:
Hi,
Can you please try this edited version of your code please.

#include <AltSoftSerial.h>

#define RXPIN 10
#define TXPIN 11
AltSoftSerial mySerial(RXPIN, TXPIN);
void setup()
{
 Serial.begin(9600);
 mySerial.begin(9600);
 Serial.println("Test begin");
}
void loop()
{
 char c;
 if (mySerial.available())
 {
   Serial.print("Data Recieved  ");
   c = mySerial.read();
   Serial.println(c);
 }
}





Thanks.. Tom... :)

Thank you tom
if statement(mySerial.available()) is not working
so i was printing mySerial.available() but the value is always 0

I'm sure the statement as such is working perfectly fine, in effect telling you that you are not receiving any data on your mySerial connection. That's where your attention should be: get that connection to work.

That's also why my question in #6. It appears the RS232 part is operating, so the issue is in the RS232 to TTL conversion, the transmission of the TTL signals from converter to the Arduino (i.e. your wiring), or the receipt of the TTL signals by the Arduino.

Hi,
The OPs code does not use println when it prints out the serial read, it may be receiving "spaces" that are streaming across the monitor screen.
My edit adds println and text that will see if any thing is being received.
This code will scroll if anything is being printed.

Tom... :slight_smile:

How about fixing the bug, see reply #5.

Hi,

if statement(mySerial.available()) is not working
so i was printing mySerial.available() but the value is always 0

Not sure what you mean..
If the "if statement(mySerial.available())" is not working, then you should not be printing anything to the monitor screen in the loop.

So what does my code in post#8 display on the monitor?

Tom... :slight_smile:

@TomGeorge, the bug !

TomGeorge:
Hi,
Not sure what you mean..
If the "if statement(mySerial.available())" is not working, then you should not be printing anything to the monitor screen in the loop.

So what does my code in post#8 display on the monitor?

Tom... :slight_smile:

Yes the moniter have not display anything
I think the if statement keeps in the loop
My english is slang I am sorry

Hi,
Does it display "Test Begin"?
If not have you got the baud rate on the IDE monitor set for 9600?

Thanks.. Tom... :slight_smile:

I am going to bail out of this, because this is getting too weird for me.

@TomGeorge, can you test your own sketch with two Arduino boards ? I think you you owe pretzelell an apology.

@pretzelell, please go to your first post of this thread, and select More/Modify in the right-bottom corner and edit the topic title. Replace "SoftwareSerial" by "AltSoftSerial".
I'm sorry that I did not spot the bug right away. I use AltSoftSerial myself, so I should have noticed it sooner.
Try to see what happens when you read this whole thread once again while ignoring everything that TomGeorge wrote.

Koepel:
I am going to bail out of this, because this is getting too weird for me.

@TomGeorge, can you test your own sketch with two Arduino boards ? I think you you owe pretzelell an apology.

@pretzelell, please go to your first post of this thread, and select More/Modify in the right-bottom corner and edit the topic title. Replace "SoftwareSerial" by "AltSoftSerial".
I'm sorry that I did not spot the bug right away. I use AltSoftSerial myself, so I should have noticed it sooner.
Try to see what happens when you read this whole thread once again while ignoring everything that TomGeorge wrote.

Why, I was using his code, and trying to at least see if the IDE monitor was working first
Still don't know if "Test Begin" is being displayed from the void setup().
If not then more than just wrong library being used.
Tom... :slight_smile:

TomGeorge:
Why, I was using his code, and trying to at least see if the IDE monitor was working first
Still don't know if "Test Begin" is being displayed from the void setup().
If not then more than just wrong library being used.
Tom... :slight_smile:

Oh bro I said that The Serial moniter have displayed "Test Begin" from void setup() I mean that mySerial.available() always 0 haha Don't be sorry It is my explaination fault haha