Question about serial communication between arduino & VB


hi,
this first post in forum, , hope in correct section.

so, got following code instructables making led pov display.
code: [select]
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 = 0; <5; i++){displayline(a[i]);delay(delaytime);}displayline(0);}
  if (c == 'b'){for (int = 0; <5; i++){displayline(b[i]);delay(delaytime);}displayline(0);}
  if (c == 'c2'){for (int = 0; <5; i++){displayline(c2[i]);delay(delaytime);}displayline(0);}
  if (c == 'd'){for (int = 0; <5; i++){displayline(d[i]);delay(delaytime);}displayline(0);}
  if (c == 'e'){for (int = 0; <5; i++){displayline(e[i]);delay(delaytime);}displayline(0);}
  if (c == 'f'){for (int = 0; <5; i++){displayline(f[i]);delay(delaytime);}displayline(0);}
  if (c == 'g'){for (int = 0; <5; i++){displayline(g[i]);delay(delaytime);}displayline(0);}
  if (c == 'h'){for (int = 0; <5; i++){displayline(h[i]);delay(delaytime);}displayline(0);}
  if (c == 'i'){for (int = 0; <5; it++){displayline(i[it]);delay(delaytime);}displayline(0);}
  if (c == 'j'){for (int = 0; <5; i++){displayline(j[i]);delay(delaytime);}displayline(0);}
  if (c == 'k'){for (int = 0; <5; i++){displayline(k[i]);delay(delaytime);}displayline(0);}
  if (c == 'l'){for (int = 0; <5; i++){displayline(l[i]);delay(delaytime);}displayline(0);}
  if (c == 'm'){for (int = 0; <5; i++){displayline(m[i]);delay(delaytime);}displayline(0);}
  if (c == 'n'){for (int = 0; <5; i++){displayline(n[i]);delay(delaytime);}displayline(0);}
  if (c == 'o'){for (int = 0; <5; i++){displayline(o[i]);delay(delaytime);}displayline(0);}
  if (c == 'p'){for (int = 0; <5; i++){displayline(p[i]);delay(delaytime);}displayline(0);}
  if (c == 'q'){for (int = 0; <5; i++){displayline(q[i]);delay(delaytime);}displayline(0);}
  if (c == 'r'){for (int = 0; <5; i++){displayline(r[i]);delay(delaytime);}displayline(0);}
  if (c == 's'){for (int = 0; <5; i++){displayline(s[i]);delay(delaytime);}displayline(0);}
  if (c == 't'){for (int = 0; <5; i++){displayline(t[i]);delay(delaytime);}displayline(0);}
  if (c == 'u'){for (int = 0; <5; i++){displayline(u[i]);delay(delaytime);}displayline(0);}
  if (c == 'v'){for (int = 0; <5; i++){displayline(v[i]);delay(delaytime);}displayline(0);}
  if (c == 'w'){for (int = 0; <5; i++){displayline(w[i]);delay(delaytime);}displayline(0);}
  if (c == 'x'){for (int = 0; <5; i++){displayline(x[i]);delay(delaytime);}displayline(0);}
  if (c == 'y'){for (int = 0; <5; i++){displayline(y[i]);delay(delaytime);}displayline(0);}
  if (c == 'z'){for (int = 0; <5; i++){displayline(z[i]);delay(delaytime);}displayline(0);}
  if (c == '!'){for (int = 0; <5; i++){displayline(excl[i]);delay(delaytime);}displayline(0);}
  if (c == '?'){for (int = 0; <5; i++){displayline(ques[i]);delay(delaytime);}displayline(0);}
  if (c == '.'){for (int = 0; <5; i++){displayline(eos[i]);delay(delaytime);}displayline(0);}
  delay(charbreak);
}

void displaystring(char* s)
{
  (int = 0; i<=strlen(s); i++)
  {
  displaychar(s[i]);
  }
}

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

here link explanation code:
http://www.instructables.com/id/attiny8545-pov-display/step9/explanation-of-the-code/#step9

the code working fine, want control using vb code, ie change displaystring vb.
so, made following code vb:
code: [select]

imports system.io
imports system.io.ports
imports system.threading

public class form1
    shared _continue boolean
    shared _serialport serialport

    private sub form1_load(byval sender system.object, byval e system.eventargs) handles mybase.load
        serialport1.close()
        serialport1.portname = inputbox("please select 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 system.object, byval e system.eventargs) handles button1.click
        serialport1.open()
        serialport1.write(textbox1.text)
        serialport1.close()
    end sub
    private sub textbox1_textchanged(byval sender system.object, byval e system.eventargs) handles textbox1.textchanged
    end sub
end class


but, not able change text in arduino. following methods tried, followed error shown arduino ide.
1)
code: [select]
void loop()

  displaystring("hello world");
}
to
code: [select]
void loop() {

displaystring(serial.read());
}

error
code: [select]
povvb.ino: in function 'void loop()':
povvb:20: error: invalid conversion 'int' 'char*'
povvb:20: error: initializing argument 1 of 'void displaystring(char*)'

2)added code beginning
code: [select]
string content = "";
  char character;
and replaced
code: [select]
void loop()

  displaystring("hello world");
}
with
code: [select]
void loop()

  while(serial.available()) {
      character = serial.read();
      content.concat(character);
  }
  displaystring(character);
}

error
code: [select]
povvb.ino: in function 'void loop()':
povvb:27: error: invalid conversion 'char' 'char*'
povvb:27: error: initializing argument 1 of 'void displaystring(char*)'

3)replaced above code with
code: [select]
void loop()

  while(serial.available()) {
      character = serial.read();
      content.concat(character);
  }
  if (content != "") {
    serial.println(content);
  }
 
  displaystring(content);
}
error
code: [select]
povvb.ino: in function 'void loop()':
povvb:30: error: cannot convert 'string' 'char*' argument '1' 'void displaystring(char*)'

4)added "string str;" beginning , replaced
code: [select]
void loop()

  displaystring("hello world");
}
with
code: [select]
void loop()
{  if(serial.available() > 0)
    {
        str = serial.readstringuntil('\n');
       
    }
  displaystring(str);
}
error
code: [select]
povvb.ino: in function 'void loop()':
povvb:25: error: cannot convert 'string' 'char*' argument '1' 'void displaystring(char*)'


i searched error codes in google, couldn't find solution problem.

you need collect data serial port array, until end-of-packet marker arrives. when end of packet marker arrives, call displaystring() function string have saved.

code: [select]
displaystring(serial.read());
serial.read() needs return int because many people (you included) call testing there read. function doesn't take int. takes array of chars, not serial.read() returns.

code: [select]
  displaystring(character);
the function not take char. takes array of chars.

code: [select]
  displaystring(content);
the function not take string. takes string.

code: [select]
  displaystring(str);
same problem above. readbytesuntil() method good, not return string. returns number of bytes read.

there readstringuntil() method returns string. string class has tochararray() method gets string instance.


Arduino Forum > Using Arduino > Programming Questions > Question about serial communication between arduino & VB


arduino

Comments

Popular posts from this blog

Connecting Raspberry Pi 2 to P10(1R)-V706 LED Dot Matrix - Raspberry Pi Forums

TypeError: <unknown> is not a numpy array - Raspberry Pi Forums

datso and removing imagetitle - Joomla! Forum - community, help and support