Vb.net write serial data processing issues

Hey guys,
This is my first post, and my first week using Arduinos. My first project is to try to control two servo motors with a vb program on my computer. The idea is to be able to control my two servos using my voice. I have the computer code down, with exception to the actual writing data to the comport. I am stumped as to what data type I should use in this case. I have two servos, and therefore my defining values that the computer figures out and needs to send are: which servo to move, and to what degree should it move. I have tried using a byte array and here is what my code looks like:

VB END:

Imports System.IO
Imports System.IO.Ports
Imports System.Threading
Imports System.Speech
Public Class Form1 
    Dim WithEvents arduno As New Arduino_Net.Arduino
    Dim WithEvents recog As New Speech.Recognition.SpeechRecognizer

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim gram As New Recognition.SrgsGrammar.SrgsDocument
        Dim commandRule As New Recognition.SrgsGrammar.SrgsRule("commands")
        Dim commandlist As New Recognition.SrgsGrammar.SrgsOneOf("a ninety", "a one eighty", "a zero", "be ninety", "be one eighty", "be zero")
        commandRule.Add(commandlist)
        gram.Rules.Add(commandRule)

        gram.Root = commandRule
        recog.LoadGrammar(New Recognition.Grammar(gram))
        SerialPort1.WriteBufferSize = 10
        SerialPort1.Open()
        Timer1.Enabled = True
    End Sub

    Private Sub reco_SpeechRecognized(ByVal sender As Object, ByVal e As System.Speech.Recognition.RecognitionEventArgs) Handles recog.SpeechRecognized
        Dim buff() As Byte = {0, 0, 0, 0}
        Select Case e.Result.Text
            Case "a one eighty"
                buff = {1, 1, 8, 0}
                SerialPort1.Write(buff, 0, 4)
                SerialPort1.BaseStream.Flush()
            Case ("a zero")
                buff = {1, 0, 0, 0}

                SerialPort1.Write(buff, 0, 4)
                SerialPort1.BaseStream.Flush()
            Case "a ninety"
                buff = {1, 0, 9, 0}

                SerialPort1.Write(buff, 0, 4)
                SerialPort1.BaseStream.Flush()
            Case "be one eighty"
                buff = {0, 1, 8, 0}

                SerialPort1.Write(buff, 0, 4)
                SerialPort1.BaseStream.Flush()
            Case "be zero"
                buff = {0, 0, 0, 0}

                SerialPort1.Write(buff, 0, 4)
                SerialPort1.BaseStream.Flush()
            Case "be ninety"
                buff = {0, 0, 0, 0}

                SerialPort1.Write(buff, 0, 4)
                SerialPort1.BaseStream.Flush()
        End Select

    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        recog.Enabled = True
    End Sub
    Private _msg As String
    Private _type As MessageType
    Public Enum MessageType
        Incoming
        Outgoing
        Normal
        Warning
        [Error]
    End Enum

    <STAThread()> _
    Private Sub DisplayData(ByVal type As MessageType, ByVal msg As String)
        _displaywindow.Invoke(New EventHandler(AddressOf DoDisplay))
    End Sub
    Private Sub DoDisplay(ByVal sender As Object, ByVal e As EventArgs)
        _displaywindow.SelectedText = String.Empty
        _displaywindow.SelectionFont = New Font(_displaywindow.SelectionFont, FontStyle.Bold)
        _displaywindow.AppendText(_msg)
        _displaywindow.ScrollToCaret()
    End Sub

    Private Sub SerialPort1_DataReceived(sender As Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Dim msg As String = SerialPort1.ReadExisting()
        'display the data to the user
        _type = MessageType.Incoming
        _msg = msg
        DisplayData(MessageType.Incoming, msg + "" + Environment.NewLine + "")
    End Sub
    
End Class

Arduino end:

#include <Servo.h>
Servo servo1;    // control pin for servo motor  
Servo servo2;
const int MaxChars = 3;
char recievedata[MaxChars+1];
int index;
int received;
int degree;
char senddata;
int trip = 0;
int doub = 0;
int sing = 0;
int wloop = 0;

  
  
void setup() {  
  servo1.attach(9);  
  servo2.attach(10);
  received = 0;
  recievedata[received] = '\0';
  Serial.begin(9600);  
  Serial.println("      Arduino Serial Servo Control");  
  Serial.println();  
}  
  
void loop() {  
  // wait for serial input  
    if (Serial.available()>0) {
      recievedata[received++] = Serial.read();
      recievedata[received] = '\0';
      if (received > (sizeof(recievedata))) {
        if (recievedata[0]==1){index = 1;}
        if (recievedata[0]==0){index = 0;}
      
        if (recievedata[1] == 0) {trip = 0;}
        if (recievedata[1] == 1) {trip = 1;}
        wloop = 9;
        while (wloop > 0){
          if (recievedata[2] == wloop) {doub = wloop;}
          wloop = (wloop - 1);
        }
        wloop = 9;
        while (wloop > 0){
          if (recievedata[3] == wloop) {doub = wloop;}
          wloop = (wloop - 1);
        }
       degree = (sing + (10*doub) + (trip * 100));
       if (index = 1) {
  servo2.write(degree);
  }
if (index = 0) {
  servo1.write(degree);
}
}
}

recievedata[received] = '\0';
received = 0;
degree = 0;
index = 0;
sing = 0;
doub = 0;
trip = 0;
}

I realize that this is a very robust way or parsing the data, but at this stage, I am just trying to get something that works. It has been a lot of trial and error, so I have sacrificed correct technique to see if I could bluntly interpret the data. Unfortunately, this code does not work. I was hoping I could get a new set of eyes on my problem and see what solutions people can come up with.

Thanks in advanced

This:

const int MaxChars = 3;

is hardly consistent with:

                buff = {1, 1, 8, 0}
                SerialPort1.Write(buff, 0, 4)
      if (received > (sizeof(recievedata))) {

sizeof(receiveddata) will return 4. In order for received to be larger than that, it must be 5 or more. That means that you need to have written a value to receiveddata[5], which is two elements beyond the end of the array.

Your code for parsing the data in receiveddata is far more complex than it needs to be. receiveddata should contain "1180", for instance. After determining which servo (the first position) to operate on, set that position in the array to '0', and pass the resulting array ("0180") to atoi() and you're done.

so I have sacrificed correct technique to see if I could bluntly interpret the data.

You have sacrificed correct technique for sure. I don't see that you accomplished your goal, though.

Thanks for the reply PaulS,

I have read some of your other posts and was happy to see you take interest in mine as well. MaxChars refers to the amount of digits I use for the degree value. The actual data being written is four bits long, as seen in the definition of recievedata[]: "char recievedata[MaxChars+1];"

The array is four chars long, so I can see how the next problem might be remedied if the value was "if (received < (sizeof(recievedata))) {" instead.

Why must I set the position in the array back to zero once I determine what the value is initially? I guess I am just confused exactly what this line puts into my recievedata array: "recievedata[received++] = Serial.read();" If you could explain this, I would appreciate it. To my understanding, this puts my vb.net array into the recievedata array, with index 0 from the vb program corresponding to the 0 index for my arduino code.

How do you get rid of the first value, when converting the array to an integer value? if the array reads {0,1,8,0}, would I write:

if (Serial.available()>0) {
      recievedata[received++] = Serial.read();
      recievedata[received] = '\0';
      if (received < (sizeof(recievedata))) {
        if (recievedata[0]==1){index = 1;}
        if (recievedata[0]==0){index = 0;}
      degree = atoi(recievedata[1])
      }
      }

I know this isn't right, but idk how I would accomplish this. Again, sorry for my novelty, I am just beginning with the Arduino language after all

EDIT: This throws an error: invalid conversion from 'char' to 'const char'

EDIT2: I have messed around with the code with no progress yet seen:

#include <Servo.h>
Servo servo1;    // control pin for servo motor  
Servo servo2;
const int MaxChars = 4;
char receivedata[MaxChars];
int index;
int received;
int degree;
char senddata;

  
  
void setup() {  
  servo1.attach(9);  
  servo2.attach(10);
  received = 0;
  receivedata[received] = '\0';
  Serial.begin(9600);  
  Serial.println("      Arduino Serial Servo Control");  
  Serial.println();  
}  
  
void loop() {  
  // wait for serial input  
    if (Serial.available()) {
      receivedata[received++] = Serial.read();
      receivedata[received] = '0';
      if (received >= (sizeof(receivedata)-1)) {
      if (receivedata[0]==1){index == 1;}
      if (receivedata[0]==0){index == 0;}
      degree = atoi(receivedata);
       
      if (index == 1) {
        servo2.write(degree);
        }
      if (index == 0) {
        servo1.write(degree);
        }
      received = 0;
      degree = 0;
      index = 0;
      }
    }
  }

Anything that jumps out here as incorrect?

The actual data being written is four bits long, as seen in the definition of recievedata[]: "char recievedata[MaxChars+1];"

The +1 is for the NULL that is appended later, not for the servo number that precedes the angle value.

Why must I set the position in the array back to zero once I determine what the value is initially?

So that you start writing to the array at the beginning.

I guess I am just confused exactly what this line puts into my recievedata array: "recievedata[received++] = Serial.read();" If you could explain this, I would appreciate it. To my understanding, this puts my vb.net array into the recievedata array, with index 0 from the vb program corresponding to the 0 index for my arduino code.

There are several things happening in that statement. The Serial.read() function gets the next byte from the incoming serial buffer. That statement stores the value in some position in the recievedata array, AND increments the position where the next character will be stored.

How do you get rid of the first value, when converting the array to an integer value? if the array reads {0,1,8,0}, would I write:

      if (received < (sizeof(recievedata))) {
        if (recievedata[0]==1){index = 1;}
        if (recievedata[0]==0){index = 0;}
      degree = atoi(recievedata[1]);

There are several problems, here. First, received, at this point in the code, will contain values like 1, 2, 3, etc. The sizeof() function (the parentheses around it are not needed) will return 4, with the code as it is now. So, the block will be executed when received is 1. Since you are trying to receive 4 characters, it is inappropriate to be executing this block unless received EQUALS sizeof(receivedata).

Once you set index based on the value in receivedata[ 0 ], you can change the value in receivedata[ 0 ].

       if (recievedata[0]==1)
         index = 1;
       else if (recievedata[0]==0)
         index = 0;
       recievedata[0] = ' '; // Set first character to space (or 0, if you prefer)
       degree = atoi(recievedata);

Still does not work correctly.. When I debug the statement as follows, the program shows me that degree is always 0 for some reason.

#include <Servo.h>
Servo servo1;    // control pin for servo motor  
Servo servo2;
const int MaxChars = 4;
char receivedata[MaxChars];
int index;
int received;
int degree;
char senddata;

  
  
void setup() {  
  servo1.attach(9);  
  servo2.attach(10);
  received = 0;
  receivedata[received] = '\0';
  Serial.begin(9600);  
  Serial.println("      Arduino Serial Servo Control");  
  Serial.println();  
}  
  
void loop() {  
  // wait for serial input 
    if (Serial.available()) {
      receivedata[received++] = Serial.read();
      if (received = sizeof(receivedata)) {
      if (receivedata[0]==1)
        index = 1;
      else if (receivedata[0]==0)
        index = 0;
      receivedata[0] = ' ';
      degree = atoi(receivedata);
      Serial.print(atoi(receivedata));
      if (index == 1) {
        servo2.write(degree);
        }
      if (index == 0) {
        servo1.write(degree);
        }
            }
    }
  }

It prints 0000. I do not understand what could be preventing this code from validly parsing the char array. I think that it has something to do with the way my program loops. Perhaps I am not setting all of the defaults correctly after it writes to the servo.

      if (received = sizeof(receivedata)) {

This is an assignment statement. You want ==, so that it is an equality test.

EDIT: I figured out a way around this stupid atoi thing that is seeming not to work very well. This code below works flawlessly with my vb program. Thank you PaulS for helping guide my debug process

#include <Servo.h>
Servo servo1;    // control pin for servo motor  
Servo servo2;
const int MaxChars = 4;
char receivedata[MaxChars];
int index;
int received;
int degree;
char senddata;

  
  
void setup() {  
  servo1.attach(9);  
  servo2.attach(10);
  received = 0;
  receivedata[received] = '\0';
  Serial.begin(9600);  
  Serial.println("      Arduino Serial Servo Control");  
  Serial.println();  
}  
  
void loop() {  
  // wait for serial input 
    if (Serial.available()) {
      receivedata[received++] = Serial.read();
      if (received == sizeof(receivedata)) {
      if (receivedata[0]==1)
        index = 1;
      else if (receivedata[0]==0)
        index = 0;
      receivedata[0] = ' ';
      degree = ((receivedata[1] * 100) + (receivedata[2] * 10) + receivedata[3]);
      Serial.print(received);
      received=0;
      if (index == 1) {
        servo2.write(degree);
        }
      if (index == 0) {
        servo1.write(degree);
        }
            }
    }
  }