Format String

Hi

I'm reading a rs232 input and outputting it , reading on tx1 and sending on rx1 receiving on rx0 and sending to hyper terminal on tx0

I'm getting information to hyper terminal but its just numbers for example :

54139175971051731391911911591591591919561229235

the code I'm using to read the data and send is :

void setup() {
  Serial2.begin(9600);
  Serial1.begin(9600);

}

void loop() {
  // read from port 2, send to port 1:
  if (Serial2.available()) {
     Serial1.println((String)Serial2.read()) ;
  }
}

why would it be numbers and not the formatted string I'm sending it , any ideas how i would get the formatted string outputted i then want to store the formatted string in a String class variable to do something else with the String before i send it back out again

thank you...

why would it be numbers and not the formatted string I’m sending it

How are we supposed to know what you are sending it?

     Serial1.println((String)Serial2.read()) ;

Why, pray tell, are you casting the int read from the serial port as a String? Why did you feel it necessary to perform a cast at all?

HI

Thank you for replying , my mistake the information that the rs232 is sending is for example

<01> :TEST:MORETEST

i want to collect this information and store in a variable String (class)

thank again.

the numbers are the ASCII codes of the characters you entered. ASCII codes can be 1 to 3 digits. So your stream could be this ascii codes

54 13 91 75 97 105 173 13 91 91 19 115 91 59 159 19 195 61 22 92 35

54 139 175 97 105 173 139 191 191 159 159 159 19 19 56 122 92 35

As ascii values are often between 0..127 the line below has statistically the best chance 54 13 91 75 97 10 51 73 13 91 91 19 115 91 59 15 91 91 95 61 22 92 35 "6 \n [ K a \r 3 i \n [ [ ?? s [ ; ?? [ [ _ = ?? \ #"

looks like random keyboard strokes ...

BUt as said above, we don't know what you send to the input....

void setup() 
{
  Serial2.begin(9600);
  Serial1.begin(9600);
}

void loop() 
{
  // read from port 2, send to port 1:
  if (Serial2.available())
  {
    char c = Serial2.read();
    Serial1.println(c) ;
  }
}

Hi Thank you

a typical string and format thats sent is would be

<01> :TEST:MORETEST

try this (not tested)

String s;

void setup() 
{
  Serial2.begin(9600);
  Serial1.begin(9600);
}

void loop() 
{
  // read from port 2, send to port 1:
  if (Serial2.available())
  {
    char c = Serial2.read();
    s += c;
    Serial1.println(s.toCharArray()) ;
  }
}

Thank you

I get this error message

In function 'void loop()': Converter:22: error: no matching function for call to 'String::toCharArray()' /Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/WString.h:80: note: candidates are: void String::toCharArray(char*, unsigned int)

Skip the String crap for now. Get the sending and receiving of characters working. Then, work on storing the data.

void loop() 
{
  // read from port 2, send to port 1:
  if (Serial2.available())
  {
    char c = Serial2.read();
    Serial1.println(c) ;
  }
}

Thank you

I get this error message

It says: time to read the manual :wink: - http://arduino.cc/en/Reference/StringToCharArray -

appreciated

i now see this coming out the Arduino mega2560 port 2

Ø66‹¯ai­‹¿¿ŸŸŸ¿_=åë

going in to port 1 is

<01> :TEST:MORETEST

i have confirmed that <01> :TEST:MORETEST is going to Arduino by connection directly to hyper terminal

What is connected to port 2? Is it an RS232 line? Do the line rates match?

Hi

Port 2 is connected to a instrument that send out a rs232 data stream, if i connect the instrument to hyper terminal i get <01> :TEST:MORETEST for example , instrument is setup as 9600 , 8 , 1 ,none same as hyper terminal and can see <01> :TEST:MORETEST printed to the terminal

if i send a String from the mega 2560 again using default 9600 , 8 , 1 , (from port 1 )i can see the string in hyper terminal that i assigned it to send.

but as soon as i connect the instrument to the mega 2560 on Serial2 and connect hyper terminal to Serial1 (via ttl converter) i just get a bunch of numbers i.e 54139175971051731391911911591591591919561229235

Help very much appreciated been on it now most the day … driven me crazy…

You can try the below code with the serial monitor (the normal serial monitor setup is different than hyperterminal).

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

String readString;

void setup() {
	Serial.begin(9600);
        Serial.println("serial test 0021"); // so I can keep track of what is loaded
        }

void loop() {

        while (Serial.available()) {
        delay(1);  
    	if (Serial.available() >0) {
        char c = Serial.read();
        readString += c;}
        }
        
      if (readString.length() >0) {
      Serial.println(readString);
      
      readString="";
      } 
   }

Thank you

when i typed in the console this worked ok

i modified the code to read from Serial1

String readString;

void setup() {
	Serial.begin(9600);
        Serial1.begin(9600);
        Serial.println("serial test 0021"); // so I can keep track of what is loaded
        }

void loop() {

        while (Serial1.available()) {
        delay(1);  
    	if (Serial1.available() >0) {
        char c = Serial1.read();
        readString += c;}
        }
        
      if (readString.length() >0) {
      Serial.println(readString);
      
      readString="";
      } 
   }

What i get in the console is

Ø
66?¯ai­?¿¿???¿_=åë

should be

<01> :TEST:MORETEST

100% failed char’s… have you checked baudrate at both sides? cabling?

BTW code looks OK but please use CRTL-T to auto-indent your code … makes it easier to find matching brackets :wink:

void loop()
{
  while (Serial1.available())
  {
    delay(1);  
    //if (Serial1.available() >0)  // <<<<< not needed as the while did test the availability allready
   // {
      char c = Serial1.read();
      readString += c;
    //}
  }
        
  if (readString.length() >0) 
  {
    Serial.println(readString);
    readString="";
  } 
}

Thank you ,

Check and double checked the rates , parity act all the same 9600 , 8 , N , 1

When i connect the cables from the instrument to hyper terminal no probs i see all and all is good (no connecting to mega2560 at a all)

The input (Serial2) connects to pin 2 , pin3 and 5 and the same on the instrument The output (Serial1) connects to pin 2 , pin3 and 5 then to com port

Serial 2 Pin 5 connects to the ground of the Arduino , pins 2 + 3 connects to the serial port 2 on Arduino

Serial1 Pin 5 connects to the ground of the Arduino , tx and rx of Arduino to rx + tx of MAX232

MAX232 tx and rx outputs to 9 way d , 9 way d to pc com port

All advice greatly appriciated :0

HI

Thank you you was correct it was my connection into the Arduino , i was connecting direct to Arduino i now have it connecting through a MAX232 is before it enters Arduino on Serial3

i now have a new issue , the code I’m using is

SoftwareSerial mySerialOut(4,5);//rx - tx

String readString;

void setup() 
        {
          Serial3.begin(9600);
          mySerialOut.begin(9600);
        }


void loop() {
     
  if (Serial3.available() >0) 
  {
      while (Serial3.available()) // when the port as data get it until it as none
      {
        char c = Serial3.read();
        readString += c;
      }

        String dataString = readString ;//assing the sting to process
        readString = "" ;// we are now done with readString array destroy

the string from the rs232 is held in dataString when i output this string it sometimes misses the first character for example if my input is

<123>:TEST:MORETEST

i get

<
123>:TEST:MORETEST

Any ideas please as why this would be …

Thanks again

Serial communications doesn’t necessarily happen at the same time and rate you expect it to. Remember that the entire transmission is in characters, not in words or messages.

If your other device outputs a newline after each command, you could do something like this:

void loop() {
  while ( Serial3.available() ) {
    char c = Serial3.read();
    if ( c == '<' ) readString = ""; // clear if new character
    readString += c;
    if ( c== '\n' ) dataString = readString; // only set dataString if newline.
  }
}
  while (Serial1.available())
  {
    delay(1);  
    //if (Serial1.available() >0)  // <<<<< not needed as the while did test the availability allready
   // {

Won’t do any good, robtillaart. It’s been pointed out many times that the delay() and if test are not needed, but zoomkat keeps posting the same poor code over and over.

Won't do any good, robtillaart. It's been pointed out many times that the delay() and if test are not needed, but zoomkat keeps posting the same poor code over and over

PaulS, it is not perfect code and can use improvements. At least robtillaart has posted the improvements. That being said, I have never seen PaulS post any complete working code. PaulS over and over tells new users to use end of packet delimiters, but in my time in this forum I've never seen him post an actual complete working example. Somebody find an example where has. My feeling is you can't. I'm waiting. :)