1

I have an Arduino Uno with an RS-232 shield, and a servo drive that communicates via CAN or RS-232. I'm trying to communicate with this drive using an RS-232 cable, but I'm getting no response. I'm using the SoftwareSerial library on the Arduino to use digital pins 3 and 2 as rx and tx. Only pins 2 (RxD), 3 (TxD), and 5 (GND) of RS-232 are used to communicate with the drive. 19,200 is the default baud rate for the drive. The drive is powered with a dedicated power supply. The drive is supposed to echo every character received back to the sender.

I've had success using a simple Java program to communicate with the drive using the same kind of cable (although with a USB-to-serial converter). The drive echos all characters back, and the motor responds to the commands sent to the drive. The cable used has a female DB9 connector on one end (to communicate with the desktop) and a custom 3 pin connector on the other (to communicate with the drive).

I've also had success with the same Java program communicating with the Arduino (also with the converter). This cable has a female DB9 connector on one end (to communicate with the desktop) and a male DB9 connector on the other (to communicate with the Arduino).

But I've had no success exchanging data between the Arduino and the drive. There is no response back from the drive nor does the motor respond to any commands. The cable used for this has a male DB9 connector on one end (to communicate with the Arduino) and a custom 3 pin connector on the other (to communicate with the drive).

Each cable has been tested with a voltmeter to confirm that pins on one end are connected to the correct pins on the other end.

I'm at a loss here. Is there something I'm missing?

For your consideration, here is the Arduino code. It is the same whether it is connected to the desktop or the drive.

#include <SoftwareSerial.h>
#include <SPI.h>


const int rx = 3;               // software serial receive pin
const int tx = 2;               // software serial transmit pin
const int baudRate = 19200;     // 19,200 bits per second for read and write serial functions

SoftwareSerial serial(rx, tx);


/*
 * Initial, one-time setup
 */
void setup()
{
    Serial.begin(baudRate);     // open hardware serial communications
    serial.begin(baudRate);     // open software serial communications

    serial.println("MO=1;");      // start motor
    serial.println("JV=30000;");  // set velocity to 30,000 counts/second
    serial.println("BG;");        // begin movement
}

/*
 * Executes every tick
 */
void loop()
{
    if (serial.available())
    {
        Serial.write(serial.read());
    }

    if (Serial.available())
    {
        serial.write(Serial.read());
    }
}

Also, here is the Java code.

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


public class TwoWaySerialComm
{
    public TwoWaySerialComm()
    {
        super();
    }

    void connect(String portName) throws Exception
    {
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
        if (portIdentifier.isCurrentlyOwned())
        {
            System.out.println("Error: Port is currently in use");
        }
        else
        {
            CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);

            if (commPort instanceof SerialPort)
            {
                SerialPort serialPort = (SerialPort)commPort;
                serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

                InputStream in = serialPort.getInputStream();
                OutputStream out = serialPort.getOutputStream();

                (new Thread(new SerialReader(in))).start();
                (new Thread(new SerialWriter(out))).start();

            }
            else
            {
                System.out.println("Error: Only serial ports are handled by this example.");
            }
        }     
    }

    /** */
    public static class SerialReader implements Runnable 
    {
        InputStream in;

        public SerialReader(InputStream in)
        {
            this.in = in;
        }

        public void run()
        {
            byte[] buffer = new byte[1024];
            int len = -1;
            try
            {
                while ((len = this.in.read(buffer)) > -1)
                {
                    System.out.print(new String(buffer, 0, len));
                }
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }            
        }
    }

    /** */
    public static class SerialWriter implements Runnable
    {
        OutputStream out;

        public SerialWriter(OutputStream out)
        {
            this.out = out;
        }

        public void run()
        {
            try
            {
                int c = 0;
                while ((c = System.in.read()) > -1)
                {
                    //System.out.print(c);
                    this.out.write(c);
                }                
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }            
        }
    }

    public static void main(String[] args)
    {
        try
        {
            (new TwoWaySerialComm()).connect("COM4");  // COM4 is port of USB-to-serial converter
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
1

I've found the issue. The serial cable I was using was too long, introducing resistance to the signal. That's it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.