Question about serial communication between arduino & VB

Hi,
This is my first post in this forum, and I hope its in the correct section.

So, I got the following code from Instructables for making a led pov display.

int delayTime = 1;
int charBreak = 2.1;

int LED1 = 2;
int LED2 = 3;
int LED3 = 4;
int LED4 = 5;
int LED5 = 6;

void setup()
{
  Serial.begin(9600);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT); 
}

int a[] = {1, 6, 26, 6, 1};
int b[] = {31, 21, 21, 10, 0};
int c2[] = {14, 17, 17, 10, 0};
int d[] = {31, 17, 17, 14, 0};
int e[] = {31, 21, 21, 17, 0};
int f[] = {31, 20, 20, 16, 0};
int g[] = {14, 17, 19, 10, 0};
int h[] = {31, 4, 4, 4, 31};
int i[] = {0, 17, 31, 17, 0};
int j[] = {0, 17, 30, 16, 0};
int k[] = {31, 4, 10, 17, 0};
int l[] = {31, 1, 1, 1, 0};
int m[] = {31, 12, 3, 12, 31};
int n[] = {31, 12, 3, 31, 0};
int o[] = {14, 17, 17, 14, 0};
int p[] = {31, 20, 20, 8, 0};
int q[] = {14, 17, 19, 14, 2};
int r[] = {31, 20, 22, 9, 0};
int s[] = {8, 21, 21, 2, 0};
int t[] = {16, 16, 31, 16, 16};
int u[] = {30, 1, 1, 30, 0};
int v[] = {24, 6, 1, 6, 24};
int w[] = {28, 3, 12, 3, 28};
int x[] = {17, 10, 4, 10, 17};
int y[] = {17, 10, 4, 8, 16};
int z[] = {19, 21, 21, 25, 0};

int eos[] = {0, 1, 0, 0, 0};
int excl[] = {0, 29, 0, 0, 0};
int ques[] = {8, 19, 20, 8, 0};

void displayLine(int line)
{
  int myline; myline = line;
  if (myline>=16) {digitalWrite(LED1, HIGH); myline-=16;} else {digitalWrite(LED1, LOW);}
  if (myline>=8) {digitalWrite(LED2, HIGH); myline-=8;} else {digitalWrite(LED2, LOW);}
  if (myline>=4) {digitalWrite(LED3, HIGH); myline-=4;} else {digitalWrite(LED3, LOW);}
  if (myline>=2) {digitalWrite(LED4, HIGH); myline-=2;} else {digitalWrite(LED4, LOW);}
  if (myline>=1) {digitalWrite(LED5, HIGH); myline-=1;} else {digitalWrite(LED5, LOW);}
  
}

void displayChar(char c)
{
  if (c == 'a'){for (int i = 0; i <5; i++){displayLine(a[i]);delay(delayTime);}displayLine(0);}
  if (c == 'b'){for (int i = 0; i <5; i++){displayLine(b[i]);delay(delayTime);}displayLine(0);}
  if (c == 'c2'){for (int i = 0; i <5; i++){displayLine(c2[i]);delay(delayTime);}displayLine(0);}
  if (c == 'd'){for (int i = 0; i <5; i++){displayLine(d[i]);delay(delayTime);}displayLine(0);}
  if (c == 'e'){for (int i = 0; i <5; i++){displayLine(e[i]);delay(delayTime);}displayLine(0);}
  if (c == 'f'){for (int i = 0; i <5; i++){displayLine(f[i]);delay(delayTime);}displayLine(0);}
  if (c == 'g'){for (int i = 0; i <5; i++){displayLine(g[i]);delay(delayTime);}displayLine(0);}
  if (c == 'h'){for (int i = 0; i <5; i++){displayLine(h[i]);delay(delayTime);}displayLine(0);}
  if (c == 'i'){for (int it = 0; it <5; it++){displayLine(i[it]);delay(delayTime);}displayLine(0);}
  if (c == 'j'){for (int i = 0; i <5; i++){displayLine(j[i]);delay(delayTime);}displayLine(0);}
  if (c == 'k'){for (int i = 0; i <5; i++){displayLine(k[i]);delay(delayTime);}displayLine(0);}
  if (c == 'l'){for (int i = 0; i <5; i++){displayLine(l[i]);delay(delayTime);}displayLine(0);}
  if (c == 'm'){for (int i = 0; i <5; i++){displayLine(m[i]);delay(delayTime);}displayLine(0);}
  if (c == 'n'){for (int i = 0; i <5; i++){displayLine(n[i]);delay(delayTime);}displayLine(0);}
  if (c == 'o'){for (int i = 0; i <5; i++){displayLine(o[i]);delay(delayTime);}displayLine(0);}
  if (c == 'p'){for (int i = 0; i <5; i++){displayLine(p[i]);delay(delayTime);}displayLine(0);}
  if (c == 'q'){for (int i = 0; i <5; i++){displayLine(q[i]);delay(delayTime);}displayLine(0);}
  if (c == 'r'){for (int i = 0; i <5; i++){displayLine(r[i]);delay(delayTime);}displayLine(0);}
  if (c == 's'){for (int i = 0; i <5; i++){displayLine(s[i]);delay(delayTime);}displayLine(0);}
  if (c == 't'){for (int i = 0; i <5; i++){displayLine(t[i]);delay(delayTime);}displayLine(0);}
  if (c == 'u'){for (int i = 0; i <5; i++){displayLine(u[i]);delay(delayTime);}displayLine(0);}
  if (c == 'v'){for (int i = 0; i <5; i++){displayLine(v[i]);delay(delayTime);}displayLine(0);}
  if (c == 'w'){for (int i = 0; i <5; i++){displayLine(w[i]);delay(delayTime);}displayLine(0);}
  if (c == 'x'){for (int i = 0; i <5; i++){displayLine(x[i]);delay(delayTime);}displayLine(0);}
  if (c == 'y'){for (int i = 0; i <5; i++){displayLine(y[i]);delay(delayTime);}displayLine(0);}
  if (c == 'z'){for (int i = 0; i <5; i++){displayLine(z[i]);delay(delayTime);}displayLine(0);}
  if (c == '!'){for (int i = 0; i <5; i++){displayLine(excl[i]);delay(delayTime);}displayLine(0);}
  if (c == '?'){for (int i = 0; i <5; i++){displayLine(ques[i]);delay(delayTime);}displayLine(0);}
  if (c == '.'){for (int i = 0; i <5; i++){displayLine(eos[i]);delay(delayTime);}displayLine(0);}
  delay(charBreak);
}

void displayString(char* s)
{
  for (int i = 0; i<=strlen(s); i++)
  {
  displayChar(s[i]);
  }
}

void loop()
{
  displayString("hello world");
}

Here is the link to the explanation for the code:

The code is working fine, but I want to control it using VB code, ie change the displayString from VB.
So, I made the following code for VB:

Imports System.IO
Imports System.IO.Ports
Imports System.Threading

Public Class Form1
    Shared _continue As Boolean
    Shared _serialPort As SerialPort

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.Close()
        SerialPort1.PortName = InputBox("Please select the COM port")
        SerialPort1.BaudRate = 9600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = Parity.None
        SerialPort1.StopBits = StopBits.One
        SerialPort1.Handshake = Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.Default
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        SerialPort1.Open()
        SerialPort1.Write(TextBox1.Text)
        SerialPort1.Close()
    End Sub
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    End Sub
End Class

But, I was not able to change the text in arduino. Following are the methods I tried, followed by the error shown by arduino IDE.
1)

void loop()
{  
  displayString("hello world");
}

to

void loop() {

displayString(Serial.read()); 
}

ERROR

POVVB.ino: In function 'void loop()':
POVVB:20: error: invalid conversion from 'int' to 'char*'
POVVB:20: error: initializing argument 1 of 'void displayString(char*)'

**2)**Added this code to the beginning

String content = "";
  char character;

and replaced

void loop()
{  
  displayString("hello world");
}

with

void loop()
{  
  while(Serial.available()) {
      character = Serial.read();
      content.concat(character);
  }
  displayString(character);
}

ERROR

POVVB.ino: In function 'void loop()':
POVVB:27: error: invalid conversion from 'char' to 'char*'
POVVB:27: error: initializing argument 1 of 'void displayString(char*)'

**3)**Replaced the above code with

void loop()
{  
  while(Serial.available()) {
      character = Serial.read();
      content.concat(character);
  }
  if (content != "") {
    Serial.println(content);
  }
  
  displayString(content);
}

ERROR

POVVB.ino: In function 'void loop()':
POVVB:30: error: cannot convert 'String' to 'char*' for argument '1' to 'void displayString(char*)'

4)Added "String str;" to the beginning and replaced

void loop()
{  
  displayString("hello world");
}

with

void loop()
{  if(Serial.available() > 0)
    {
        str = Serial.readStringUntil('\n');
        
    }
  displayString(str);
}

ERROR

POVVB.ino: In function 'void loop()':
POVVB:25: error: cannot convert 'String' to 'char*' for argument '1' to 'void displayString(char*)'

I searched the error codes in google, but couldn't find a solution to my problem.

You need to collect the data from the serial port into an array, until some end-of-packet marker arrives. When the end of packet marker arrives, call the displayString() function with the string you have saved.

displayString(Serial.read());

Serial.read() needs to return an int because too many people (you included) call it with testing that there is anything to read. Your function doesn't take an int. It takes an array of chars, which is not what Serial.read() returns.

  displayString(character);

The function does not take a char. It takes an array of chars.

  displayString(content);

The function does not take a String. It takes a string.

  displayString(str);

Same problem as above. The readBytesUntil() method is good, but it does not return a String. It returns the number of bytes read.

There is a readStringUntil() method that returns a String. The String class has a toCharArray() method that gets the string from the instance.

I don't like this too:

  if (c == 'c2'){for (int i = 0; i <5; i++){displayLine(c2[i]);delay(delayTime);}displayLine(0);}

'c2' is not a char!!! replace 'c2' by 'c'.

NOTE: that the second c2:

displayLine(c2[i]);

is OK.

You should also learn, that a large proportion of "instructables" are written by gungho halfwits who have little idea what they are actually doing, at least in respect of part of what they are doing.

Thanks PaulS for your reply.

PaulS:
Same problem as above. The readBytesUntil() method is good, but it does not return a String. It returns the number of bytes read.

There is a readStringUntil() method that returns a String. The String class has a toCharArray() method that gets the string from the instance.

Did you mean like this?

void loop()
{  if(Serial.available() > 0)
    {
        str = Serial.readStringUntil(',');
        str.toCharArray(text,12);
    }
  displayString(text);
}

Arduino didn't gave any errors while compiling, and I will reply if its working or not after checking it on my circuit.

luisilva:
I don't like this too:

  if (c == 'c2'){for (int i = 0; i <5; i++){displayLine(c2[i]);delay(delayTime);}displayLine(0);}

'c2' is not a char!!! replace 'c2' by 'c'.

NOTE: that the second c2:

displayLine(c2[i]);

is OK.

I think c2 is already defined at line 21 [int c2[] = {14, 17, 17, 10, 0};]

NOPE! They are different things. One thing is the char that you want to write c == 'c' the other thing is the variable with the led's an the other things int c2[] = {14, 17, 17, 10, 0};.

You don't what to write in the string "my name is c2huc2k norris", don't you?

luisilva:
NOPE! They are different things. One thing is the char that you want to write c == 'c' the other thing is the variable with the led's an the other things int c2[] = {14, 17, 17, 10, 0};.

You don't what to write in the string "my name is c2huc2k norris", don't you?

The string 'c' corresponds to the array "c2".

Yes, and?
I know how to program and you don't.
This line:

  if (c == 'c2'){for (int i = 0; i <5; i++){displayLine(c2[i]);delay(delayTime);}displayLine(0);}

must be:

  if (c == 'c'){for (int i = 0; i <5; i++){displayLine(c2[i]);delay(delayTime);}displayLine(0);}

EDIT: If the POV is working, you can always try to write "chuck norris" instead of "hello world", and see it.

luisilva:
Yes, and?
I know how to program and you don't.
This line:

  if (c == 'c2'){for (int i = 0; i <5; i++){displayLine(c2[i]);delay(delayTime);}displayLine(0);}

must be:

  if (c == 'c'){for (int i = 0; i <5; i++){displayLine(c2[i]);delay(delayTime);}displayLine(0);}

EDIT: If the POV is working, you can always try to write "chuck norris" instead of "hello world", and see it.

Sorry, I misunderstood your point :~
It became clear when you showed(I am new to programming, and again I am sorry)

Thanks PaulS, the code is working perfectly now :slight_smile: