UART seriel question

Can anyone write me a line of code, that sends the command "G10000" via the rx1 and tx1 line and is in compliance with the following:

The ideal way to use the UART interface of the RMCS-220x is with a terminal software like hyper-terminal, putty, etc. The UART interface works at a fixed baud rate of 9600bps. The UART signals must be TTL logic compatible.

The UART interface on the RMCS-220x prompts the user for a command variable and decimal value string. To set a value of a variable the user must provide an integer decimal value following the command code. To read/display the value of a variable the user must give the command character and immediately follow it by line feed and carriage return. The UART command processor will return the value of the variable. The command list and value range are as follows.

Command Description Value Minimum Value Maximum ‘S’ Read/Write Motor Speed and Direction -255 +255 ‘M’ Read/Write Motor Max Speed 0 255 ‘D’ Read/Write Speed Damping 0 255 ‘E’ Read/Write I2C address 0 127 ‘Y’ Load Default Values of all settings and gains - - ‘P’ Read/Write Encoder Position -2147483648 2147483647 ‘G’ Read/Write Go to Position Command -2147483648 2147483647 ‘R’ Write Relative Go to Position Command -2147483648 2147483647 ‘A’ Read/Write Speed-Feedback Gain term 0 32767 ‘B’ Read/Write P-Gain term 0 32767 ‘C’ Read/Write I-Gain term 0 32767 ‘X’ Auto-calibrate Speed-Feedback Gain term - -

I have tried this code, in various combination with write and print and shifting the \r and \n, but i can't get the motor moving, thus i think i am doing something wrong with my code.

void setup() {

  Serial1.begin(9600);
}

void loop() { 

  Serial1.write("G1000\r\n");

  delay(5000);

}

More information can be found here: http://www.robokits.co.in/documentation/RMCS220x_DCServo+Driver.pdf

(I made a thread were i was trying to write to it via the serial prompt, but i made so many mistakes in my code, so now i have shaved it down to the basic... i just wan't the motor to spin, then i will figure out the rest)

I have tried this code, in various combination with write and print and shifting the \r and \n, but i can't get the motor moving, thus i think i am doing something wrong with my code.

Serial.println("G10000");

would seem to be what you need. But, perhaps you need to define other stuff before telling the motor to move, like speed and direction. Maybe it IS moving, but at zero speed. That will require a lot of patience to see results.

This assumes, of course, that you have the motor controller (and motor and power supply) all wired to the Arduino correctly - an assumption I'm not willing to make without seeing anything about the controller, the motor, the power supply, etc.

That was also my first try, but since that did not work i moved on to the code above.

I re-ran my wiring and i was precisely as stated in the manual (page 4). But then i came to think, that since it was an indian produced motor and the build quality seemed a little rough, they might have switched the RX and TX line. So i switched mine and now it is spinning and i seem to have some sort of control with it.

I will return with my findings on this motor :)

Oh and thank you for your help Paul. I am pretty ok with wiring, but coding are not my strongest point. When you confirmed my coding i knew that it must be a hardware issue :)

The meaning of RX and TX depends on your perspective. Is the RX pin one you receive data on, or is it the one you send data to for the other end to receive? It would be nice to have a standard that everyone followed.

I'm glad you got it figured out.

So far it seems like a good little engine. I especially like the fact that it runs on 12v, thus eliminating the need for a higher voltage seperate PSU, normally needed for stepper motors. I run this of an old PC PSU.

One thing though: I thought that it was gonna send its position back as soon as it arrived on target, but it just returns the given position immediately. The good news are that you can send the P command and it will tell you the actual position, although this means that you will have to use processing power to ask the motor where it is. It will as an example return “P1021”, so one will have to convert this to a int (position 1021) to make numerical analyses (i guess…ideas are welcome here).

Another thing is that i tends to overshoot its target by 4 steps (0.2 deg pr step). This might be tunable, but if not its not a no go for me… but that means that i can’t use the equal sign to check on it position because i don’t what to wait for it to settle.

Anyway here goes my first simple code, that works by taking commands from the serial monitor. Suggestions are welcome :slight_smile:

/* 

 The servo i bought had switched TX and RX terminal, so if the motor does not respond, try switching them.
 
 */
void setup() {

  Serial.begin(9600);
  Serial1.begin(9600);
  Serial.println("Serial conection started, waiting for instructions...");

}

void loop() {

  String serDataStr0;
  String serDataStr1;
  
  
 // Reads the input from the serial prompt and gives it to the motor
  while(Serial.available()) {   // Check to see if there are any availeble communication
    char recievedChar0 = Serial.read();
    serDataStr0 += recievedChar0;  // Puttes the serial communication together to one string.
    delay(5);    // Neccesary on fast boards as chipkit, else it will put one serial com into two.
  }

  if (serDataStr0 != "") {  // If the datastring are not empty, then proceed.
    Serial1.print(serDataStr0);
    serDataStr0 = "";    //Empty the data string from the Serial
  }  
  
  // Reads the input from the motor and return it to the serial prompt  
  while(Serial1.available()) {   // Check to see if there are any availeble communication
    char recievedChar1 = Serial1.read();
    serDataStr1 += recievedChar1;  // Puttes the serial communication together to one string.
    delay(5);    // Neccesary on fast boards as chipkit, else it will put one serial com into two.
  }

  if (serDataStr1 != "") {  // If the datastring are not empty, then proceed.
    Serial.print(serDataStr1);
    serDataStr0 = "";    //Empty the data string from the Serial
  }    
}

Suggestions are welcome

Do NOT use Strings. Put on your big boy pants and learn to use strings.

    delay(5);    // Neccesary on fast boards as chipkit, else it will put one serial com into two.

Complete and utter nonsense. It is "necessary" only because the code is not separating the collection of data as it arrives (slowly) until a delimiter arrives (saying "Hey, we're done") from the use of the collected data.

As i said coding is not my strongest asset. When i asked it to send it to the prompt, it arrived in two packages, thus my comment :~

Whats the difference between string and String?

What would i have to do to avoid the delay?

PaulS: The meaning of RX and TX depends on your perspective. Is the RX pin one you receive data on, or is it the one you send data to for the other end to receive? It would be nice to have a standard that everyone followed.

I'm glad you got it figured out.

There is a standard. It's just that no one (myself included) can remember what DCE and DTE mean... :~

Whats the difference between string and String?

A string is simple a NULL-terminated array of chars. A String, on the other hand, is a class that wraps the NULL-terminated array of chars, doing dynamic memory allocation each time a char is added to the array. The dynamic memory manipulation on a limited memory Arduino is not a good fit.

What would i have to do to avoid the delay?

You don't read a letter, close your eyes, open them, read a letter, etc. hoping not to try to read a letter before it's been typed, do you? No, you wait until I add the end-of-packet marker (by hitting the Post button). Then, you are freed to read my reply as fast as possible.

If your computer is really slow (or it's network connection is), it will display part of a page, while it loads more. You can read all the data that has been displayed so far, and then do something else while you wait for the rest of the page to be posted.

The key, though, is that there needs to be something that says that the sender is done sending a packet. In the case of the Serial Monitor, you can have it add a carriage return, a line feed, both, or neither to what you enter (as an end-of-packet marker).

I don't know how the motor controller does it. There may be something it adds, like a carriage return or line feed.