Go Down

Topic: Unable to use values received at rx tx (Read 3778 times) previous topic - next topic

Visual_Kol

Mar 08, 2020, 02:33 am Last Edit: Mar 08, 2020, 08:07 am by Visual_Kol
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:
Code: [Select]

// 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:
Code: [Select]
<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?

ieee488

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

Nick_Pyner

#2
Mar 08, 2020, 05:49 am Last Edit: Mar 08, 2020, 07:07 am by Nick_Pyner
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.


Visual_Kol

#3
Mar 08, 2020, 08:19 am Last Edit: Mar 08, 2020, 10:22 am by Visual_Kol
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:
Code: [Select]
#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.

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.

Nick_Pyner

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

Visual_Kol

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.

Nick_Pyner

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.

Visual_Kol

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?

jremington

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

Did you connect the grounds?

Visual_Kol

#9
Mar 10, 2020, 04:05 am Last Edit: Mar 10, 2020, 04:15 am by Visual_Kol
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.

Nick_Pyner

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.

Visual_Kol

#11
Mar 11, 2020, 04:27 pm Last Edit: Mar 11, 2020, 06:48 pm by Visual_Kol
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.
Code: [Select]
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.
Code: [Select]
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.

jremington

#12
Mar 11, 2020, 06:32 pm Last Edit: Mar 11, 2020, 06:33 pm by jremington
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.

Visual_Kol

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

jremington

#14
Mar 11, 2020, 08:17 pm Last Edit: Mar 11, 2020, 08:19 pm by jremington
Quote
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.

Go Up