Serial port sending [Solved]

Hi

i'm working on a program that functions as a virtual screen for the arduino. This means that you can send commands through the serial port which get interpreted by a Visual Basic application. You can currently draw lines, rectangles, ellipses and strings/vars. But i have one major problem

I use the serialport.datareveived event, and everything works well for the variable sending command (see code below), but for all the other commands the arduino stops sending data after a while.
I can send thousands of var drawing commands, but after 165 commands for drawRect it stops sending. I really have no clue why this happens.

The code is posted below, the VB is not the whole code since that would take too much space, only the receiving part:

Sub EventHandler(sender As Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles Arduino.DataReceived

        Dim datareceived As Boolean

        datareceived = False
        recStr = ""
'Repeat this until the received byte is 10 (from println(); )
        While Not datareceived

            Dim recVal As Integer
            recVal = Arduino.ReadByte()
            If recVal = 10 Or recVal = 13 Then
                datareceived = True
            Else
                recStr = recStr + Chr(recVal)
            End If

        End While
'This "C" is a drawscreen command that gets everything drawn and clears the list afterwards
        If recStr <> "C" Then
'This list keeps track of everything that has to be drawn on the screen and gets erased when "C" is received
            drawList.Add(recStr)
        Else
            ArduinoScreen.Refresh()
            drawList.Clear()
        End If



    End Sub

and the code for the arduino (lib code):

#include "Arduino.h"
#include "serialScreen.h"

serialScreen::serialScreen()
{
}

void serialScreen::setFont(String string, int fontsize, String style)
{
	string = "F" + string;
	string = string + ",,";
	string = string + fontsize;
	string = string + ",,";
	string = string + style;
	
	Serial.println(string);
}

void serialScreen::drawString(String string, int x, int y)
{
	string = "S" + string;
	string = string + ",,";
	string = string + x;
	string = string + ",,";
	string = string + y;
	
	Serial.println(string);
}

void serialScreen::drawVar(int var, int x, int y) 
{
	String string = "S";
	
	string = string + var;
	string = string + ",,";
	string = string + x;
	string = string + ",,";
	string = string + y;
	
	Serial.println(string);
}

void serialScreen::drawRect(int x, int y, int w, int h)
{
	String string = "R";
	
	string = string + x;
	string = string + ",,";
	string = string + y;
	string = string + ",,";
	string = string + w;
	string = string + ",,";
	string = string + h;

	Serial.println(string);
}

void serialScreen::fillRect(int x, int y, int w, int h)
{
	String string = "Q";
	
	string = string + x;
	string = string + ",,";
	string = string + y;
	string = string + ",,";
	string = string + w;
	string = string + ",,";
	string = string + h;
	
	Serial.println(string);
}

void serialScreen::drawEllipse(int x, int y, int w, int h)
{
	String string = "E";
	
	string = string + x;
	string = string + ",,";
	string = string + y;
	string = string + ",,";
	string = string + w;
	string = string + ",,";
	string = string + h;
	
	Serial.println(string);
}

void serialScreen::fillEllipse(int x, int y, int w, int h)
{
	String string = "D";

	string = string + x;
	string = string + ",,";
	string = string + y;
	string = string + ",,";
	string = string + w;
	string = string + ",,";
	string = string + h;

	Serial.println(string);
}

void serialScreen::drawLine(int x, int y, int xb, int yb)
{
	String string = "L";
  
	string = string + x;
	string = string + ",,";
	string = string + y;
	string = string + ",,";
	string = string + xb;
	string = string + ",,";
	string = string + yb;

	Serial.println(string);
}

void serialScreen::setBrush(String string)
{
	string = "B" + string;
	
	Serial.println(string);
}

void serialScreen::setPen(String string, int width)
{
	string = "P" + string;
	string = string + ",,";
	string = string + width;
	
	Serial.println(string);
}

void serialScreen::drawScreen()
{
	Serial.println("C");
}

I hope someone understands my problem and can help me figuring out what's the matter here
Thank a lot!

Nick

You are doing a load of (unnecessary) String manipulation, and probably running out of SRAM.

Why do this

	String string = "R";
	
	string = string + x;
	string = string + ",,";
	string = string + y;
	string = string + ",,";
	string = string + w;
	string = string + ",,";
	string = string + h;

	Serial.println(string);

When

	Serial.print("R");
	
	Serial.print(x);
	Serial.print(",,");
	Serial.print(y);
	Serial.print(",,");
	Serial.print(w);
	Serial.print(",,");
	Serial.println(h);

would work, without having to build a String.

Thanks a lot! it works perfect now. that's amazing, now it's ready for publishing

I really want to thank you man :slight_smile: