Sending string to arduino via COM in VB.net

Hey all. I have this code below compiled onto my arduino mini 328:

const int Left = 1;
const int Right = 2;
const int Up = 3;
const int Down = 4;
const int Neutral = 0;
int countHold = 0;
int lastButton = 0;
int incomingByte = 0;
const int XPin = 4;
const int YPin = 5;

int xAxis;
int yAxis;
char* myStrings[]={"CENTER", "LEFT","RIGHT","DOWN","UP"};

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

int readButtonState(){
   int xAxis=map(analogRead(XPin), 0, 1023, 0, 10);
    int yAxis=map(analogRead(YPin), 0, 1023, 0, 10);
    
    if (xAxis < 4 ) return Left;
    if (xAxis > 6 ) return Right;
    if (yAxis < 4 ) return Down;
    if (yAxis > 6 ) return Up;
    return Neutral;
}

void serialReader(){
    int makeSerialStringPosition;
    int inByte;
    char serialReadString[50];
    const int terminatingChar = 13;
  
    inByte = Serial.read();
    makeSerialStringPosition=0;
  
    if (inByte > 0 && inByte != terminatingChar) {
      delay(100);
  
      while (inByte != terminatingChar && Serial.available() > 0){
        serialReadString[makeSerialStringPosition] = inByte;
        makeSerialStringPosition++;
        inByte = Serial.read();
      }
  
      if (inByte == terminatingChar)
      {
        serialReadString[makeSerialStringPosition] = 0;
        if (strcmp(serialReadString, "RESTART") == 0) Serial.println("RESTART");
      }
    }
}

void loop() {
    serialReader();         
    int button = readButtonState();
    
    if (button != lastButton) {
        Serial.println(myStrings[button]);
        lastButton = button;
        countHold = 0;
    }else if (button == 3) { //ENTER FOR UP
       countHold = 1 + countHold;
       
       if (countHold == 20){
         Serial.println("ENTER");
         countHold = 0;
       }
    }else if (button == 4) { //RESTART FOR DOWN
       countHold = 1 + countHold;
       
       if (countHold == 20){
         Serial.println("RESTART");
         countHold = 0;
       }
    }
    
    delay(50);
 }

This works just fine when i use the arduino serial monitor to send a response but when i use VB it never sends anything back for “RESTART”.

This is my VB.net code:

Imports System.IO.Ports
Imports System.Runtime.InteropServices

Public Class Form1
    Dim theResponse As String = ""
    Private port As New SerialPort("COM13", 9600, Parity.None, 8, StopBits.One)

    Private Sub port_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
        theResponse = Trim(port.ReadLine()).Replace(vbCr, "").Replace(vbLf, "")
        Call theR()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AddHandler port.DataReceived, New SerialDataReceivedEventHandler(AddressOf port_DataReceived)
        With port
            .Encoding = System.Text.Encoding.ASCII
            .NewLine = Chr(13) + Chr(10)
        End With

        port.Open()
    End Sub

    Private Sub theR()
        Debug.Print(theResponse)

        If theResponse = "ENTER" Then
            MsgBox("ENTER!")
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        port.Write(Val("RESTART"))
    End Sub
End Class

But it never sends back RESTART as it should and i do not know what it really is sending it.

Any help would be great!

David

else if (button == 4) { //RESTART FOR DOWN
       countHold = 1 + countHold;
       
       if (countHold == 20){
         Serial.println("RESTART");
         countHold = 0;
       }

theres your problem

  1. thats a horrid way to dealy, use timers
  2. x == 20 will not work, your shooting the tip of a needle a half mile away with a frisbee, try if(countHold >= 20) though the whole thing is a function for millis() and some basic math.

This is the part of the code where it would send back the "RESTART" text after receiving the serial data from VB.net:

if (strcmp(serialReadString, "RESTART") == 0) Serial.println("RESTART");

It had nothing to do with the code you posted as being the problem.

David