ASCII via RS485, command sent correct, but no data comes back

Hi,

I'm working on a project with an Arduino MKR 485 Shield (stacked on a MKR 1010 Wifi) and a PMD401 Motor controller (Manual https://piezomotor.com/wp-content/uploads/2019/03/150025_PMD401_Technical_Manual.pdf).
They comunicate via RS485 and ASCII.

The problem I have is, that I can send a command, the motor runs, but I can't get any data from the controller.
With the command I use, it just repeats the commands I sent before. Normally the "X1E" command should give me the position of the motor like "X1E:20" (see manual page 5, point 7).

How can I fix this?

Here is my code:

#include <ArduinoRS485.h>

void setup() {
  RS485.begin(115200);
  Serial.begin(115200);
  RS485.receive();
}

int counter = 0;
byte RS485_byte = 0;

void loop() {
  Serial.print("Count No: ");
  Serial.println(counter);
  RS485.beginTransmission();
  RS485.write("X1J20,0,150\r"); // run the motor
  RS485.write("X1E\r");         // get the location of the encoder
  Serial.flush();
  delay(20);
  while (RS485.available()) {
    RS485_byte = RS485.read();  // get a byte RS485
    Serial.write(RS485_byte);   // send the byte to the Monitor
  }
  RS485.endTransmission();
  Serial.println(" ");
  delay(10000);
  counter ++;
}

And the terminal output:

Count No: 1
X1J20,0,150X1E
Count No: 2
X1J20,0,150X1E
Count No: 3
X1J20,0,150X1E

It's "XE1", not "X1E".

aarg:
It's "XE1", not "X1E".

Thanks for the answer, but it is "X1E"
The "1" is needed to say, which motor if you stack multiple together.
Anyway I tested the commands before with the terminal program (Terminal)
There it runs and I got the right answer (X1E:20).
That's why I really don't understand the error at all.

Maybe try the passthrough example from the library.

wildbill:
Maybe try the passthrough example from the library.

Same thing. The motor runs but I don't get values back. Only the commands I wrote before... so like an echo

It sounds like your shield has a loopback mode (a guess, I'm not familiar with it) which is echoing whatever you send back to you and as a part of that, ignores anything received from other sources.

wildbill:
It sounds like your shield has a loopback mode (a guess, I'm not familiar with it) which is echoing whatever you send back to you and as a part of that, ignores anything received from other sources.

Sound exactly like what it does. But couldn't find something like that in the datasheet...
I have a second kit of the Arduino MKR at home, will try tomorrow if they can communicate correctly together.

So I tried it, and I think I found the problem.
You have to end the transmission before you can receive something.
So my code now looks like that:

#include <ArduinoRS485.h>

void setup() {
  RS485.begin(115200);
  Serial.begin(115200);
  RS485.receive();
}

int counter = 0;

void loop() {
  Serial.print("Count No: ");
  Serial.println(counter);

  RS485.beginTransmission();
  RS485.write("X1E\r");         // get the location of the encoder
  RS485.endTransmission();

  delay(20);

  while (RS485.available()) {
    Serial.write(RS485.read());   // send the data to the monitor
  }

  RS485.beginTransmission();
  RS485.write("X1J-20,0,150\r"); // run the motor
  RS485.endTransmission();

  Serial.println(" ");
  delay(3000);
  counter ++;
}

But the output is a bit strange as well:

Count No: 1
X1J-20,0,15020,0,150!X1E0
Count No: 2
X1J-20,0,15020,0,150X1E-69 
Count No: 3
X1J-20,0,15020,0,150X1E-139 
Count No: 4
X1J-20,0,15020,0,150X1E-208 
Count No: 5
X1J-20,0,15020,0,150X1E-278 
Count No: 6
X1J-20,0,15020,0,150X1E-347 
Count No: 7
X1J-20,0,15020,0,150X1E-416

So the encoder gives me some reasonable values back :grin:
But it doesn't make the ":" in between. It writes "X1E-347" instant of "X1E:-347" and it lost the "X1J-" of the first one.
Sometimes it also bugs in not readable content...
Why is that so?

#include <ArduinoRS485.h>

void setup() {
  RS485.begin(115200);
  Serial.begin(115200);

So what Serial port do you have the RS485 connected to ?
From github it shows me that normally it is connected to hwSerial, but then you cannot expect to use hwSerial to communicate for anything else and expect it to work without fault.
I can't quite see how to define a different port, other than the library defines Serial1 tx pin if it's available.

@OP
I think that you problem is that your receive buffer is too small to receive all the incoming data,
Try to make your receive buffer bigger. For example:
string inputData =“”;
inputData.reserve(100);

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.