Interrupts are interrupting serial communication

pejuangsst14:
@sterretje
Thanks for you reply and could you tell me any further about how can i do that?

This is a modified version of Robin's second eample that demonstrates the echo

// Example 2 - Receive with an end-marker

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

void setup()
{
  Serial.begin(57600);
}

void loop()
{
  recvWithEndMarker();
  processNewData();

  // do other stuff
  ...
  ...
}

void recvWithEndMarker()
{
  static byte ndx = 0;
  char endMarker = '\n';
  char rc;

  if (Serial.available() > 0 && newData == false)
  {
    rc = Serial.read();

    // echo the character
    Serial.write(rc);

    if (rc != endMarker)
    {
      receivedChars[ndx] = rc;
      ndx++;
      if (ndx >= numChars) {
        ndx = numChars - 1;
      }
    }
    else
    {
      receivedChars[ndx] = '\0'; // terminate the string
      ndx = 0;
      newData = true;
    }
  }
}

void processNewData()
{
  if (newData == true)
  {
    // do something usefull with the new data
    ...
    ...
    
    // indicate that we're ready for new data
    newData = false;
  }
}

For the VB side, the below assumes that you have

  1. textbox called tbRxData to display data received from arduino
  2. textbox called tbTxData to enter a message to be send
  3. a button called btnTransmit; the code below is the click handler for that button.

The code is in C#, so probably will need some adjustments from your side.

private void btnTransmit_Click(object sender, EventArgs e)
{
    // clear receive textbox
    tbRxData.Clear();

    _serialPort.ReadTimeout = 500;

    // create tx data from tbTxData
    byte[] tmp = Encoding.ASCII.GetBytes(tbTxData.Text);
    byte[] transmitBuffer = new byte[tbTxData.Text.Length + 1];
    for (int cnt = 0; cnt < tmp.Length; cnt++)
    {
        transmitBuffer[cnt] = tmp[cnt];
    }
    // append newline
    transmitBuffer[transmitBuffer.Length - 1] = (byte)'\n';

    // send byte by byte, waiting for echo each time
    for (int byteCnt = 0; byteCnt < transmitBuffer.Length; byteCnt++)
    {
        _serialPort.Write(transmitBuffer, byteCnt, 1);
        try
        {
            byte echo = (byte)_serialPort.ReadByte();
            tbRxData.AppendText(String.Format("0x{0:X2} ", echo));
        }
        catch (TimeoutException toe)
        {
            tbRxData.Text = "No echo from Arduino";
            return;

        }
        catch (Exception ex)
        {
            tbRxData.Text = ex.Message;
            return;
        }
    }

}