Hey guys. I'm currently working on control Mdrive motor with a Arduino R3 with a RS422/485 shield attached to it.
The problem is that I can't send the correct signal to the motor though I can get the signal from the motor.
Code
#include<SoftwareSerial.h>
int DE=2; //output pin
//char b[]="SL 51200^M";//command need to input to the mdrive
SoftwareSerial mySerial(3,4);
String comdata="";//input command in the serial monitor
void setup() {
mySerial.begin(9600);
Serial.begin(9600);
pinMode(DE, OUTPUT);
}
void loop() {
mySerial.flush();
// read sign-in message
while(mySerial.available()>0){
//mySerial.println("test for command mySerial 3");
//Serial.println("test for command Serial 4");
char c=mySerial.read();
Serial.write(c);
//mySerial.flush();
delay(1000);
}
//code for input command in the serial monitor
while(Serial.available()>0){
comdata += char(Serial.read());
mySerial.println(comdata);
delay(1000);
}
if (comdata.length() > 0)
{
Serial.write(mySerial.read());//Serial.println(comdata);(show on the serial monitor)
comdata = "";
}
}
When I power up the motor it'll show the sigh-in message but if I send "SL 51200^M"(make the motor rotate the "^M" is carriage return) it will only return ÿ...
It's strange that I have to add delay(1000) if the delay time is lower than that it will show garbled letters instead of the sign-in message on the serial monitor when I power up the motor.
The connection and the pin-out is right. The problem might be the input command isn't right or the program isn't right but I not really sure.
Any suggestions and ideas is appreciate :)! The RS422/485 shield's datasheet is shown below.
No, it's not. When you send data to a serial port, the data is buffered, and shifted out later. You are expecting a response a few nanoseconds after writing the data to the buffer. The data hasn't even been sent, yet, lot alone acted on and a response sent.
No, it's not. When you send data to a serial port, the data is buffered, and shifted out later.
While your comment is correct for the hardware serial interface it is not for the SoftwareSerial emulation. That does not buffer outgoing data but sends it immediately blocking the complete processor until it's done.
The rest of your answer is correct, the opposite side has to get some time to answer the message so a short wait is necessary. It's better not to do that with a delay() call but waiting actively until the receive buffer got enough characters.
PaulS:
No, it's not. When you send data to a serial port, the data is buffered, and shifted out later. You are expecting a response a few nanoseconds after writing the data to the buffer. The data hasn't even been sent, yet, lot alone acted on and a response sent.
Thank you for your response. I've manage to clear out the garbled letters by changing the motor mode to echo mode 2. However the machine still don't response to my command. Is that possible I have to send the signal by 'char' instead of 'string'? That motor understand ASCII command.
PaulS:
Can you tell me which ONE key you are going to press to define the char to send
Thanks for replied. The key command is "^M", which means carriage return. In the data sheet it is in HEX format "0D". In the mdrive SEM terminal "^M" could be recognized and transfer as a carriage return instead of showing on the terminal screen.
The technical support told me to add ^M and it wont be a problem. I've try all the combination but it didn't workout.
I just use logic to watch the PMW signal between the shield and the motor. Turn out it didn't send at all...What do you think is the possible reason causing that problem?