SoftwareSerial and rs232 shield

Hi again,

for a project I'm working on I need to controll a motorcontroller via RS232. I'm using a rs232 shield and the sofwareserial library. I send 8 bytes and I recieve 66 bytes so I expanded the buffer in the library to 128, but when I check for mySerial.overflow() it always returns true.... I send and read after eachother so the buffer should always be empty after reading.

Is there a thing I missed? I followed this page

Thanks!

What's the point of checking the overflow?

Presumably you know how many bytes you are expecting to receive back.

.

I know how many bytes: 66. The reason I checked the overflow (just for debugging) is that I get a repeat of the first bytes. Every return message starts with [! 65 S] and then a bunch of databytes. I get the first 63 bytes of the message and then it repeats itself. I have no idea what causes it so I was thinking in the direction of overflow and maybe that I forgot something with the buffer resizing.

Hope this makes sense?

Just a quick thought.

Did you download the SoftwareSerial library manually and add it to your arduino libraries?

I had a similar situation where I had the pre installed version of a library (i.e. the library came with the arduino ide setup) and another version that I manually installed. I was editing one version and the compiler was using the other!

I edited the library in the install folder and I have never installed one myself. It must be using the right one.

Please, post a connection diagram and the associated sketches of the two devices that are exchanging data using UART Ports.

A connection diagram would not help, I can communicate with it so the wiring is correct. The shield is using pins tx 6 and rx 7.

I uploaded the sketch I'm using minus all the CAN communication which added 200 lines. The motorcontroller is suppied by a company so I don't have the code running that thing, I uploaded the datasheet instead.

Maybe a bit more info on why I bother trying to fix this because the last 3 bytes don't contain any data. For my bachelorthesis I have to make cruise controls for a boat using this controller. Due to the covid outbreak and lockdown we can't test anything on the water so I can't make a speed (gps) cruise control. So I'm focusing on making a rpm PID controller using a potentiometer as a setpoint and the returned RPM as a proces value. The problem is that sometimes I get a wrong reading, like 10000 rpm which is impossible. So I figured I would implement a CRC check before doing any calculations but the last three bytes, including the CRC, are missing.

I don't have any other way to check the messages btw.

Hope that clears things up.

Arduino_forum.ino (8.94 KB)

SLS-serial-protocol.pdf (220 KB)

You may be blocking on serial printing. The low baud rate isn't helping there. Just read everything into your buffer first, then worry about printing it. At that point, you may well not need the extended buffer you put in softwareSerial any more.

Well I tried reading everything without serial printing and without that microseconds delay but then I have faulty readings al over the shop. That's why I read them first in a temporary buffer and then check if the full message is there before copying it to the calculations.

Moshtaraq:
Well I tried reading everything without serial printing and without that microseconds delay but then I have faulty readings al over the shop. That's why I read them first in a temporary buffer and then check if the full message is there before copying it to the calculations.

That's because your code assumes that there will always be something to read and gives up the first time there isn't. Serial data isn't like that, it's asynchronous obviously.

Try reading until you have enough characters. Drop the delay too. Once you have it working you'll likely want to add a time limit where you accept that no more data is coming after a second or so.

Wildbill, can you explain some more how I should read it then? Note: I have to cyclically send the command to the controller every 300ms or else it wil go into safety. Every time I send the command the controller responds with the 66 bytes.

I'm suggesting that you send the six bytes and read what comes back into your buffer until you have all sixty six. Don't stop looking just because serial.available says that there's nothing there - there will be.

When that's working, add a timeout so that if you don't get them all after say 150mS, you send your command again and reset the buffer.

I wrote this function, I think It's what you said(?) but anyway it works great thanks! I've been stuck on this for 4 months now...

void read_rs232() {
  int i = 0;
  start_time_reading = millis();
  while (i < l_feedback && (millis() - start_time_reading) < timeout) { //while message not complete, keep reading until timeout
    if (mySerial.available()) {
      temp_buffer[i] = mySerial.read();
      i++;
    }
  }
  i = 0;
  while (i < l_feedback) {
    Serial.print(temp_buffer[i]);
    Serial.print(", ");
    i++;
  }
  Serial.println(" ");
}

That looks like what I had in mind. Glad it's working.