RS232 pass everything throught, except <A>

Hello,

with help of this forum I assembled RS232 translator: http://forum.arduino.cc/index.php?topic=293255.0

I put this code inside:

/*
  Software serial multple serial test

 Receives from the hardware serial, sends to software serial.
 Receives from software serial, sends to hardware serial.

 The circuit:
 * RX is digital pin 10 (connect to TX of other device)
 * TX is digital pin 11 (connect to RX of other device)

 Note:
 Not all pins on the Mega and Mega 2560 support change interrupts,
 so only the following can be used for RX:
 10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69

 Not all pins on the Leonardo support change interrupts,
 so only the following can be used for RX:
 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).

 created back in the mists of time
 modified 25 May 2012
 by Tom Igoe
 based on Mikal Hart's example

 This example code is in the public domain.

 */
#include <SoftwareSerial.h>

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

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(57600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.println("Goodnight moon!");

  // set the data rate for the SoftwareSerial port
  mySerial.begin(4800);
  mySerial.println("Hello, world?");
}

void loop() // run over and over
{
  if (mySerial.available())
    Serial.write(mySerial.read());
  if (Serial.available())
    mySerial.write(Serial.read());
}

…and everything is working as expected, but can you please help me to modify above code so that when string arrives to Serial there will be printed S[CRLF] on mySerial and when when eg. 10,25 kg arrives to mySerial, 10,25 kg[SPACE] will be written to Serial? Everything else should pass trought unchanged.

Thank you in advance for suggestions.

Petr

but can you please help me to modify above code so that when string arrives to Serial there will be printed S[CRLF] on mySerial and when when eg. 10,25 kg arrives to mySerial, 10,25 kg[SPACE] will be written to Serial?

Without saving the incoming data until the end of packet marker arrives, that is not possible.

I did it! :wink: On way to device it replaces S[CR][LF] for , everything else is passed throught. On the way back I’m reading bytes until ‘\n’ and then printing just some of them. I will put my code here if anybody will want to use it.

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10,11);



void setup(){
  mySerial.begin(9600);
  Serial.begin(9600);
  mySerial.println("SW serial started\n");
  Serial.println("Serial started\n");
  
  
}

void loop(){
String inData="";
char buffer[80];
if (Serial.available() > 0)
    {
        char received = Serial.read();
        inData += received;
        if (received == 'S')
        {
          //mySerial.println("S detected");
          while (!(Serial.available()))
          {
          delay (300);
          }
          if(Serial.available())
         { 
            char received = Serial.read();
            inData += received;
            if (received == '\r')
            {
              //mySerial.print("CR detected");
              while (!(Serial.available()))
              {
                delay (300);
              }
              if(Serial.available())
              {
                char received = Serial.read();
                inData += received;
                if (received == '\n')
                {
                  //mySerial.print("LF detected");
                  mySerial.print("<A>");
                }
                else
                mySerial.print(inData);
                inData="";
              }
            }
            else
            {
              mySerial.print(inData);
              inData="";
            }
                      }
        }
        else
        {
          mySerial.print(inData);
          inData="";
        }
      
    }
    
  if (mySerial.available()){
    int i;
    mySerial.readBytesUntil('\n',buffer,80);
    Serial.print(buffer[13]);
    Serial.print(buffer[14]);
    Serial.print(buffer[15]);
    Serial.print(buffer[16]);
    Serial.print(" ");
    Serial.print(buffer[17]);
    Serial.print(buffer[18]);
    Serial.print("\r\n");
    
    //Serial.write(mySerial.read());
    
  }
}

However, there is still one issue to be resolved. According application log, I'm sending 19 bytes of data over serial port and this is not working. Original scale is sending:

8
8
3

Which is 19 as well. Can I anyhow force arduino to send chunks instead of sending everything at once?

Working log:

13:30:36.687|QSerial|handle_receive_from|DEBUG|MettlerToledo scale (8 bytes): "S[SPACE]S[SPACE][SPACE][SPACE][SPACE][SPACE]"
13:30:36.687|QSerial|handle_receive_from|DEBUG|MettlerToledo scale (8 bytes): "[SPACE]20.32[SPACE]k"
13:30:36.694|QSerial|handle_receive_from|DEBUG|MettlerToledo scale (3 bytes): "g[CR][LF]"

Current not working log:

14:41:31.612|QSerial|handle_receive_from|DEBUG|MettlerToledo scale (19 bytes): "S[SPACE]S[SPACE][SPACE][SPACE][SPACE][SPACE][SPACE]20.32[SPACE]kg[CR][LF]"

Can I force arduino to send in chunks?

Can I force arduino to send in chunks?

No. Something, though, is causing the receiver to think that the data is “chunked”. Perhaps a carriage return or line feed. If that is the case, then you need to send the same end-of-chunk marker(s) after each packet.

Inserting delay(200) between bytes resolved my problem.