Unable to use values received at rx tx

Hello! I'm trying to get values from my AB Micrologix 1400 PLC to control some LEDs connected to the Arduino. The data flow can be given as PLC (RS232 Port) ==> RS232 to TTL converter ==> Arduino Uno (Rx(0)-Tx(1) pins).

I'm still in work in progress phase, so don't have a complete program to show. For now I am testing with this code:

// Example 1 - Receiving single characters

char receivedChar;
boolean newData = false;

void setup() {
    Serial.begin(9600);
    Serial.println("<Arduino is ready>");
}

void loop() {
    recvOneChar();
    showNewData();
}

void recvOneChar() {
    if (Serial.available() > 0) {
        receivedChar = Serial.read();
        newData = true;
    }
}

void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChar);
        newData = false;
    }
}

This code is taken from Example 1 - Receiving single characters

The problem is, when i send a character from my PLC and open up the serial monitor, I see only setup print statements. And when i reset the arduino, first i see the number that was received in the buffer and then the setup statements. Something As follows:

<Arduino is ready>
1<Arduino is ready>
15<Arduino is ready>
6<Arduino is ready>

Now I've tried SoftwareSerial, different Arduino modules, different TTL converters and a lot of codes. But it seems that at any point I can't use that value in buffer, because I'm resetting the arduino.

Can you tell me what's wrong here?

SoftwareSerial is best at 9600 baud.

If you need faster, then I would get a Mega.

You obviously didn't get the memo that when the serial port to the Arduino is opened, the Arduino is expecting you to upload code. If it doesn't get the code, then it starts running the code that was previously uploaded.
.

Software serial, for all its faults, is quite OK at 38400, but the code you posted isn't using it anyway. So, no problem there, but perhaps you should be using it, and what you really need to know is the baud rate of the peripheral.

The code you show is irrelevant but not useless. It apparently is just something to receive data from serial monitor and regurgitate it straight back. It may be modified to receive the same data from a peripheral; and pass it on to the monitor, which I believe is what you want.

Assuming

  1. you need to use the serial monitor,
  2. You know what you are doing with the PLC, and
  3. You know the peripheral gear communicates at 9600

I suggest you cut to the chase and decide to use Software Serial on pins 7,8, or anything but pins 0,1, and 9600 baud. Call it serial1. Try modifying the rcvonechar subroutine by changing both instances of serial to serial1, and start software serial in the usual manner. Also you have no proper timing in the loop, which could cause you grief.

I found another post with the same problem, which again ends with the use of software serial library.
Serial communication using Rx/Tx pins

I told, I have tried using the software serial library, but it was a failed attempt
Below was the code I was using:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2,3);

char receivedChar;

void setup()
{
    Serial.begin(9600);
    mySerial.begin(9600);
    Serial.println("Setup Done");
}
void loop()
{
    if(mySerial.available>0)
    {
        receivedChar=mySerial.read();
        Serial.println(receivedChar);
    }
}

I connected Rx to 2 and TX to 3, and connected the USB cable as usual.
This time, even if I reset the Arduino, no input showed up on the serial monitor, except the setup print statement.

Also let me tell you, the PLC baud rate also is 9600. I need the same on Arduino too.

Nick_Pyner:
Also you have no proper timing in the loop, which could cause you grief.

Nick_Pyner can you please tell me, what kind of timing would you suggest?

If I use SoftwareSerial correctly what kind I output should I expect?
Will it be showing the data without resetting the Arduino?
Will I be able to use the character stored in receivedChar for comparing and turning some LEDs ON or OFF?

For now, I'll try more to get my SoftwareSerial started.

I connected Rx to 2 and TX to 3,

I don't use software serial, but I'm sure the library call is Rx,Tx and you therefore have the wiring the wrong way round. Check that.

I would ignore the thread you quote, he didn't know what he was doing either, and I think the result was inconclusive.

Will a sample rate of 1Hz do? If so put a delay(1000); at the end of the loop, or adjust accordingly.

If you use software serial, the output will be the same as any other serial. You should be able to include code to signal a LED in accordance with data received. This is normal practice.

Still not working, I tried using the software serial same as before with changing the connection. Nothing on the serial monitor. I tried reseting the arduino too.

I also tried the connections you recommend (i.e. on 7,8 pins). Output: Nothing!
Then I removed the serial monitor from the equation, powering arduino from external power supply 7.4v. Removed all serial statements, except those of softwareserial. And connected an led to be turned on or off according to the data received at the rx pin. It's simply not working.

Have you checked the Wiring Rx>TX, Tx>Rx as noted in reply#4?
Note that there is nothing magic about pins 7,8, indeed I only mentioned them because they are very un-magic.

Nick_Pyner:
Have you checked the Wiring Rx>TX, Tx>Rx as noted in reply#4?

Yes I did.
Here I think pin 2,3 can take interrupt on uno, so they might be ideal for the communication

What does a Serial.event() do? And how is it called from the loop?

Sounds like your RS232 to TTL converter is not working. You posted no details, so we can only guess.

Did you connect the grounds?

Actually in the first post I told that whatever I send from plc, shows up in my serial monitor, but it's after I reset the arduino, an then the setup statements appear. So I guess no problem with Rs232 to ttl converter.

Visual_Kol:
Here I think pin 2,3 can take interrupt on uno, so they might be ideal for the communication

No, and that is why I said Pins 7,8. Pins 2,3 will work OK for comms but there is no need to start bad habits you may regret later.

Guys, something much bizarre has happened today. of course i did not get the output as i wanted but just have a look.
I've attached a copy of the test codes I was conducting today. Please find it attached. I again tested with the Software Serial monitor, it seems to be unable to read any data. So it failed again.
But while I was experimenting with simple serial monitor code, I accidentally deleted the code for printing the received Character. So i further experimented with it. And finally uploaded this code.

void setup()
{
    Serial.begin();
    Serial.println("Serial Setup Complete");
}
void loop()
{
    
}

So, as you see I haven't read the data arriving in the serial buffer or printed it.
Now I send number '2' from the plc. I didn't get anything as expected. But when i reset the arduino It was as follows.

Serial Setup Complete
2Serial Setup Complete
2Serial Setup Complete
2Serial Setup Complete

So it means that the conception that the conflict between Serial monitor and tx rx working at the same time might be the reason to get this effect. But why is this value getting printed? that too before even setup begins the serial communication. and again how to store this value in a variable so that it can be used further.

Serial_Communication_Testing.ino (9.74 KB)

How do you have the PLC wired to the Arduino?

It looks like you are still using the serial monitor RX and TX (pins 0 and 1 on an Uno) connections for both, which is a very bad idea.

jremington:
How do you have the PLC wired to the Arduino?

AB Micrologix 1400 PLC has a DB9 port which supports RS232 protocol.
So the Connections are PLC <---> RS232 Cable <---> RS232 to TTL converter <---> Arduino (0,1)(Rx,Tx)

jremington:
It looks like you are still using the serial monitor RX and TX (0 and 1 on an Uno) connections for both, which is not allowed.

Yes but it's because my SoftwareSerial doesn't work!
Same as earlier, it doesn't take anything into the buffer. But for some reason hardware rx tx does (after reset). That's why I was trying to use rx tx and serial monitor parallelly.

Yes but it's because my SoftwareSerial doesn't work!

You are doing something wrong, but have not provided enough information for us to figure out what it is.

Software serial WORKS, at least at 9600 Baud. Continuing as you are will get you nowhere.

Post a very clear, hand drawn wiring diagram, showing all connections, with pins and parts carefully labeled.

The schematic is pretty simple. I've attached it, along with the code. And my apologise for not posting this earlier.

Please can you have a look at this
Do you think I should try using this DF1 library, while knowing that there is no standard DF1 library for arduino?

re reply#11, there is no such thing as a software serial monitor, the serial monitor is on hardware serial - no exceptions, and, if you have any peripheral connected to 0,1, you won't be able to upload..

Visual_Kol:
AB Micrologix 1400 PLC has a DB9 port which supports RS232 protocol.
So the Connections are PLC <---> RS232 Cable <---> RS232 to TTL converter <---> Arduino (0,1)(Rx,Tx)Yes but it's because my SoftwareSerial doesn't work!

You are connecting the peripheral to the hardware serial pins, 0,1 which, with Software Serial, will never work. If you insist on using Software serial, put it on pins 7,8 and use software serial there, then it might work. Indeed anything will do - except pins 0,1.

Your hand drawing does not agree with the pic in the link. You just have to make up your mind which serial method you want and code accordingly, and stop fartarsing about with both.

If AB say hardware, you should understand that is OK, and I assume the serial monitor is not part of the game. Just remember to disconnect the MAX232 when uploading the programme.

If you want to use software serial, it will be OK at 9600.

Get rid of the useless delays in the program shown in the .jpg appended to reply #15.

Then bidirectional character transmission should work, but if not, try swapping the wires connected to pins 2 & 3.

However, if you don't use the expected protocol, you will not be able to communicate properly with the box, and it may not respond AT ALL.

Nick_Pyner:
re reply#11, there is no such thing as a software serial monitor.

Sorry to confuse you, but when I was saying about using Rx Tx and Serial Monitor, I was trying to say that my SoftwareSerial method doesn't work, so I have gone back to Hardware Serial, removing SoftwareSerial from the picture.

Nick_Pyner:
Your hand drawing does not agree with the pic in the link.

Actually I was asking you, whether I should go with method that they have used in that link, so my drawing is not built according to that.

jremington:
Get rid of the useless delays in the program shown in the .jpg appended to reply #15.

The delay(50) is because PLC is sending data at every 50 milliseconds. And I send just ONE Character from PLC so ARDUINO should perfectly match with that.
The delay(1000) is because I was getting print statements in loop to quick to understand. So for now it is definitely useless. And I will get rid of them.

jremington:
Then bidirectional character transmission should work, but if not, try swapping the wires connected to pins 2 & 3.

I did try swapping the pins. I think I'm frustrating you guys by complicating my replies. I'll try to keep it simple onwards.
And I will further be uploading the code after disconnecting the RS232 - TTL converter from Arduino.