Arduino UNO: Send Data via RS232 :o

Hello,

I am needing help regarding the Arduino UNO. I am trying to send functions from my Ardunio Uno to an external device and then receive the data back from my external device to my Arduino UNO via RS232. My ultimate goal is to automate the device by using the Arduino UNO. I will like my Arduino UNO to automatically send functions (i.e The functions are: 1#, 2#, 3#, 4#, 5#, 6#, 7#, 8#, 9#) to my device and my device will automatically send back to the Arduino UNO with its responding data to that function (i.e. By sending 1# to the device, the device should respond back with it's voltage). Im very new to coding in Ardunio language. I originally wrote my code in C# and it worked fine but now I have to use an Arduino which isnt compatible with C#. The first thing I need to do is Send those funtions ( 1#, 2#, 3#, 4#, 5#, 6#, 7#, 8#, 9#) to my external device. Can anyone assist me or guide me in the right direction? I will post my C# code below showing how I sent those functions using C#. I will really appreciate you guys help.

Thanks in advance!

//Sends functions 1-9 along with # which is 0xD
 public void SendData(ref int temp2, SerialPort _serialPort)
        {
            try
            {

                string c = Convert.ToString(temp2);
                byte[] array_out = Encoding.ASCII.GetBytes(c);
                _serialPort.Write(array_out, 0, array_out.Length);
                byte[] array_out2 = new byte[1];
                array_out2[0] = 0xD;
                _serialPort.Write(array_out2, 0, array_out2.Length);

                _serialPort.Write(array_out, 0, array_out.Length);
                _serialPort.Write(array_out2, 0, array_out2.Length);

I am trying to send functions from my Ardunio Uno

That doesn't sound quite right. What is it exactly that you want to send from the Arduino ?

I am trying to send 1#, 2#, 3#, 4#, 5#, 6#, 7#, 8#, 9# from my arduino uno.

void setup()
{
  Serial.begin(115200);
  for (int x = 1; x < 10; x++)
  {
    Serial.print(x);
    Serial.print("#");
    Serial.print("\n");
  }
}

Note that the Arduino outputs TTL voltage levels and will need an external converter to make them RS232 levels.

void loop()
{
}
}

Thanks, however, I will like to send the functions (1#, 2#, 3#, 4#, 5#, 6#, 7#, 8#, 9#) from my arduino to my external device thats connected via RS232 (connected to pins 10, 11 for RX, TX). So shouldn't I use mySerial.write rather than Serial.print?

Heres a photo of it: https://www.dropbox.com/s/n3u0wexceav4x7b/image1.JPG?dl=0

however, I will like to send the functions (1#, 2#, 3#, 4#, 5#, 6#, 7#, 8#, 9#) from my arduino to my external device thats connected via RS232 (connected to pins 10, 11 for RX, TX). So shouldn't I use mySerial.write rather than Serial.print?

The code that I posted was to illustrate how to send values via serial. It will work just as well with SoftSerial assuming that you configure it correctly. Did you note my comment about TTL/RS232 voltage levels and the need for a converter ?

I don't usually shout but WILL YOU PLEASE STOP REFERRING TO FUNCTIONS WHEN THEY ARE NO SUCH THING.

Hahhahaha Sorry about the word "functions"... And yes, I just noticed your note about the TTL voltage levels

How do I send 1#, 2#, 3#, 4#, 5#, 6#, 7#, 8#, 9# (# is 0xD which is carriage return) using my Software Serial port? (I have an external device connected to it). I have constructed a for loop to do so but i am not receiving the data back that I want. It doesnt seem to increment to the next one. The only way I can obtain the right data is by using the Serial Monitor and typing in my desired number and having a carriage return along with it. That is the only way I can receive the data that I want. My ultimate goal is to automate my Arduino by obtaining data from from each corresponding number that I send.

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX

const int CTS = 12;    // the pin that Clear to Send is connected to 
const int RTS = 13;   // the pin that Request to Send is connected to 

void setup() {

 Serial.begin(9600);
 mySerial.begin(9600);
  pinMode(CTS, HIGH);
  Serial.println("Sending...");

SendData();


  }
  
void loop()
{
ReadData();
 }


void SendData()
{
for (int x = 0; x < 10; x++)
{
   
    mySerial.write(x);
    mySerial.write(0xD);


   }
   
}


void ReadData()
{
 
  if (mySerial.available()) {
  // Serial.println("data \n");
   int inByte = mySerial.read();
   char xchar = char(inByte);
    Serial.write(xchar); 
 }
 if (Serial.available()) {
   int inByte = Serial.read();
   mySerial.write(inByte); 
 }


}

Why have you started another thread on this subject ? We were going along nicely in http://forum.arduino.cc/index.php?topic=391650.msg2697277

I finally got my code to at least "talk" to my external device however, i am not receiving the data that I need. It seems to be getting stuck somewhere.The only way I can obtain the right data is by using the Serial Monitor and typing in my desired number and having a carriage return along with it.

Why are you using Serial.write() and why don't you send the # that you require ? Did you try the code that I suggested in the duplicate thread ?

Yes, tried the code. The reason I am using Serial.write in my "ReadData" function is so I can display my incoming data from mySerial (my RS232 module which is connected in pins 10,11 (rx,tx) and that is connected to an external device).

Like so: https://www.dropbox.com/s/n3u0wexceav4x7b/image1.JPG?dl=0