none
problems with Select Case and read from serial VB2015 RRS feed

  • Question

  • Hello

    I'm having problems with select case and reading from serial device.

    In my serial receive I can see that I'm getting the right strings but I can't get my case select to read it

     Private Sub Display()
            Dim readcase As String
            Dim message As String
    
            Received.AppendText(New String(RXAscii))
            Received.AppendText(vbCrLf)        ' Switch to a new line after every transmission
            readcase = RXAscii
    
            ' Trace.WriteLine(Date.Now + readcase.ToLower)               ' Gir output for logging
    
            message = Date.Now + readcase & " is writing to registry"
    
            Select Case readcase.ToLower()
                Case "ledpin10on"
                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin10", "true")     'LED lys på
                    Trace.WriteLine(message)
                Case "ledpin10off"
                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin10", "false")     'LED lys på
                    Trace.WriteLine(message)
                Case "ledpin11on"
                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin11", "true")
                    Trace.WriteLine(message)
                Case "ledpin11off"
                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin11", "false")
                    Trace.WriteLine(message)
                Case "ledpin12on"
                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin12", "true")
                    Trace.WriteLine(message)
                Case "ledpin12off"
                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin12", "false")
                    Trace.WriteLine(message)
                Case "ledpin13on"
                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin13", "true")
                    Trace.WriteLine(message)
                Case "ledpin13off"
                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin13", "false")
                    Trace.WriteLine(message)
    
                Case "a"
                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin10", "true")     'LED lys på
                    Trace.WriteLine(message)
                Case "b"
                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin10", "false")     'LED lys på
                    Trace.WriteLine(message)
                Case "c"
                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin11", "true")
                    Trace.WriteLine(message)
                Case "d"
                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin11", "false")
                    Trace.WriteLine(message)
                Case "e"
                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin12", "true")
                    Trace.WriteLine(message)
                Case "f"
                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin12", "false")
                    Trace.WriteLine(message)
                Case "g"
                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin13", "true")
                    Trace.WriteLine(message)
                Case "h"
                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin13", "false")
                    Trace.WriteLine(message)
                Case Else
                    'Trace.WriteLine(message + "case else")
                    Trace.WriteLine(readcase + Date.Now + "Ingen forandring på ledPin")               ' Gir output for logging
            End Select
        End Sub
    
    


    thise is the code I cant get to work. I'm reading from an Arduino device.

    Arduino code

    /*
    
    
      The circuit:
      - pushbutton attached to pin 2, 3, and 4 from +5V
      - 10 kilohm resistor attached to pin 2 from ground
      - 10 kilohm resistor attached to pin 3 from ground
      - 10 kilohm resistor attached to pin 4 from ground
      - LED attached from pin 10 to ground
      - LED attached from pin 11 to ground
      - LED attached from pin 12 to ground
      - LED attached from pin 13 to ground  
    */
    
    // this constant won't change:
    
    const int  buttonPin2 = 2;    // the pin that the pushbutton is attached to 2
    const int  buttonPin3 = 3;    // the pin that the pushbutton is attached to 3
    const int  buttonPin4 = 4;    // the pin that the pushbutton is attached to 4
    // const int  buttonPin5 = 5;    // the pin that the pushbutton is attached to 5
    
    const int ledPin10 = 10;       // the pin that the LED is attached to 10
    const int ledPin11 = 11;       // the pin that the LED is attached to 11
    const int ledPin12 = 12;       // the pin that the LED is attached to 12
    const int ledPin13 = 13;       // the pin that the LED is attached to 13
    
    // Variables will change:
     int buttonState1 = 0;         // current state of the button
    // int lastButtonState1 = 0;     // previous state of the button
    
     int buttonState2 = 0;         // current state of the button
    // int lastButtonState2 = 0;     // previous state of the button
    
     int buttonState3 = 0;         // current state of the button
    // int lastButtonState3 = 0;     // previous state of the button
    
     int LEDState1 = 0;            // current state of the LED output 1
    // int lastLEDState1 = 0;        // previous state of the LED output 1
    
     int LEDState2 = 0;            // current state of the LED output 2
    // int lastLEDState2 = 0;        // previous state of the LED output 2
    
     int LEDState3 = 0;            // current state of the LED output 3
    // int lastLEDState3 = 0;        // previous state of the LED output 3
    
     int LEDState4 = 0;            // current state of the LED output 4
    // int lastLEDState4 = 0;        // previous state of the LED output 4
    
    
    void setup() {
      // put your setup code here, to run once:
      // initialize the button pin as a input:
      pinMode(buttonPin2, INPUT);
      pinMode(buttonPin3, INPUT);
      pinMode(buttonPin4, INPUT);
      // pinMode(buttonPin5, INPUT);
     
      // initialize the LED as an output:
      pinMode(ledPin10, OUTPUT);
      pinMode(ledPin11, OUTPUT);
      pinMode(ledPin12, OUTPUT);
      pinMode(ledPin13, OUTPUT);
     
      // initialize serial communication:
      Serial.begin(9600, SERIAL_8N1);
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
      if (Serial.available() > 0) {
        int inByte = Serial.read();
            switch (inByte) {
          case 'a':
            digitalWrite(ledPin10, HIGH);
            Serial.println("ledpin10on");
            delay(30);
            Serial.println("");
            Serial.println("a");
            LEDState1 == HIGH;
            break;
          case 'b':
            digitalWrite(ledPin10, LOW);
            Serial.println("ledpin10off");
            delay(30);
            Serial.println("");
            Serial.println("b");
            LEDState1 == LOW;
            break;
          case 'c':
            digitalWrite(ledPin11, HIGH);
            Serial.println("ledpin11on");
            delay(30);
            Serial.println("");
            Serial.println("c");
            LEDState2 == HIGH;
            break;
          case 'd':
            digitalWrite(ledPin11, LOW);
            Serial.println("ledpin11off");
            delay(30);
            Serial.println("");
            Serial.println("d");
            LEDState2 == LOW;
            break;
          case 'e':
            digitalWrite(ledPin12, HIGH);
            Serial.println("ledpin12on");
            delay(30);
            Serial.println("");
            Serial.println("e");
            LEDState3 == HIGH;
            break;
          case 'f':
            digitalWrite(ledPin12, LOW);
            Serial.println("ledpin12off");
            delay(30);
            Serial.println("");
            Serial.println("f");
            LEDState3 == LOW;
            break;  
          case 'g':
            digitalWrite(ledPin13, HIGH);
            Serial.println("ledpin13on");
            delay(30);
            Serial.println("");
            Serial.println("g");
            LEDState4 == HIGH;
            break;
          case 'h':
            digitalWrite(ledPin13, LOW);
            Serial.println("ledpin13off");
            delay(30);
            Serial.println("");
            Serial.println("h");
            LEDState4 == LOW;
            break;  
          default:
            // turn all the LEDs off:
              digitalWrite(ledPin10, LOW);
              digitalWrite(ledPin11, LOW);
              digitalWrite(ledPin12, LOW);
              digitalWrite(ledPin13, LOW);
              Serial.println("ledpin10off");
              delay(30);
              Serial.println("");
              Serial.println("b");
              delay(30);
              Serial.println("");
              Serial.println("ledpin11off");
              delay(30);
              Serial.println("");
              Serial.println("d");
              delay(30);
              Serial.println("");
              Serial.println("ledpin12off");
              delay(30);
              Serial.println("");
              Serial.println("f");
              delay(30);
              Serial.println("");
              Serial.println("ledpin13off");
              delay(30);
              Serial.println("");
              Serial.println("h");
              break;
        } //-------Close switch inByte  
      } //---------Close serial available
    
    buttonState1 = digitalRead(buttonPin2);
    LEDState4 = digitalRead(ledPin13);
    
      // compare the LEDState to turn on or off
        if ( digitalRead(buttonPin2) == HIGH) {
          // Serial.println("buttonState1 on");
          
          if (LEDState4 == HIGH) {
            digitalWrite(ledPin13, LOW);
            Serial.println("ledpin13off");
            delay(30);
            Serial.println("");
            Serial.println("h");
            LEDState4 == LOW;
            }
            delay(50);
          if (LEDState4 == LOW) {
            digitalWrite(ledPin13, HIGH);
            Serial.println("ledpin13on");
            delay(30);
            Serial.println("");
            Serial.println("g");
            LEDState4 == HIGH;
            }
            delay(50);
        }
        else{
        delay(50);
        } //---------Close compare LEDstat to previous state
    
    buttonState2 = digitalRead(buttonPin3);
    LEDState2 = digitalRead(ledPin11);
    
    
      // compare the LEDState to turn on or off
        // if (buttonState2 == HIGH) {
        if ( digitalRead(buttonPin3) == HIGH) {
          // Serial.println("buttonState2 on");
          
          if (LEDState2 == HIGH) {
            digitalWrite(ledPin11, LOW);
            Serial.println("ledpin11off");
            delay(30);
            Serial.println("");
            Serial.println("d");
            LEDState2 == LOW;
            }
            delay(50);
          if (LEDState2 == LOW) {
            digitalWrite(ledPin11, HIGH);
            Serial.println("ledpin11on");
            delay(30);
            Serial.println("");
            Serial.println("c");
            LEDState2 == HIGH;
            }
            delay(50);
        }
        else{
        // Serial.println("buttonState3 off");
        delay(50);
        } //---------Close compare LEDstat to previous state
    
    buttonState3 = digitalRead(buttonPin4);
    LEDState3 = digitalRead(ledPin12);
    
    
      // compare the LEDState to turn on or off
        // if (buttonState2 == HIGH) {
        if ( digitalRead(buttonPin4) == HIGH) {
          // Serial.println("buttonState3 on");
          
          if (LEDState3 == HIGH) {
            digitalWrite(ledPin12, LOW);
            Serial.println("ledpin12off");
            delay(30);
            Serial.println("");
            Serial.println("f");
            LEDState3 == LOW;
            }
            delay(50);
          if (LEDState3 == LOW) {
            digitalWrite(ledPin12, HIGH);
            Serial.println("e");
            delay(30);
            Serial.println("");
            Serial.println("ledpin12on");
            LEDState3 == HIGH;
            }
            delay(50);
        }
        else{
        // Serial.println("buttonState3 off");
        delay(50);
        } //---------Close compare LEDstat to previous state
    
      // save the current state as the last state, for next time through the loop
      //lastLEDState1 = LEDState1;
     
    } //-----------Close void loop
    


    All the visual basic code from form1

    Imports System.IO
    Imports Microsoft.Win32
    Imports System
    Imports System.IO.Ports
    Imports System.Text
    Imports System.Threading
    Imports System.Threading.Thread
    
    
    Public Class HMI_Bolig_Klient
    
        Dim xLedPin10 As String                                   'Status LED Pin 10 Arduino
        Dim sMatchLedPin10 As Boolean                             ' LEDPIN10 Funnet i tekst fil
        Dim xLedPin11 As String                                   'Status LED Pin 10 Arduino
        Dim sMatchLedPin11 As Boolean                             ' LEDPIN11 Funnet i tekst fil
        Dim xLedPin12 As String                                   'Status LED Pin 10 Arduino
        Dim sMatchLedPin12 As Boolean                             ' LEDPIN12 Funnet i tekst fil
        Dim xLedPin13 As String                                   'Status LED Pin 10 Arduino
        Dim sMatchLedPin13 As Boolean                             ' LEDPIN13 Funnet i tekst fil
        Dim xbuttonPin4 As String                                  'Status Button 4 Arduino
        Dim sMatchButton4 As Boolean                             ' Button4 Funnet i tekst fil
    
        Dim SpaceCount As Byte = 0
        Dim LookUpTable As String = "0123456789ABCDEF"
        Dim RXArray(2047) As Char ' Text buffer. Must be global to be accessible from more threads.
        Dim RXCnt As Integer      ' Length of text buffer. Must be global too.
        Dim RXAscii As String
    
    
    
        Private Sub Read_Register_Tick(sender As Object, e As EventArgs) Handles Read_Register.Tick
            'Leser registri verdier og setter de i variabelene
            L_R_LysAlt.Text = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "LysAlt", "Default Value")
            L_R_Lys1.Text = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "Lys1", "Default Value")
            L_R_Lys2.Text = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "Lys2", "Default Value")
            L_R_Lys3.Text = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "Lys3", "Default Value")
            L_R_Lys4.Text = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "Lys4", "Default Value")
            L_R_Skall1.Text = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "Skall1", "Default Value")
            L_R_Skall2.Text = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "Skall2", "Default Value")
            L_R_Skall3.Text = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "Skall3", "Default Value")
            L_R_Skall4.Text = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "Skall4", "Default Value")
            L_R_Kj.Text = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ErTempKj", "Default Value")
            L_R_Bad.Text = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ErTempBad", "Default Value")
            L_R_Gang.Text = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ErTempGang", "Default Value")
            L_R_Stue.Text = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ErTempStue", "Default Value")
            L_R_Trapp.Text = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ErTempTrapp", "Default Value")
            L_R_Sov1.Text = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ErTempSov1", "Default Value")
            L_R_Sov2.Text = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ErTempSov2", "Default Value")
            L_R_Sov3.Text = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ErTempSov3", "Default Value")
            L_R_Set_Kj.Text = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "SetTempKj", "Default Value")
            L_R_Set_Bad.Text = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "SetTempBad", "Default Value")
            L_R_Set_Gang.Text = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "SetTempGang", "Default Value")
            L_R_Set_Stue.Text = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "SetTempStue", "Default Value")
            L_R_Set_Trapp.Text = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "SetTempTrapp", "Default Value")
            L_R_Set_Sov1.Text = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "SetTempSov1", "Default Value")
            L_R_Set_Sov2.Text = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "SetTempSov2", "Default Value")
            L_R_Set_Sov3.Text = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "SetTempSov3", "Default Value")
            L_R_ledPin10.Text = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin10", "Default Value")
            L_R_ledPin11.Text = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin11", "Default Value")
            L_R_ledPin12.Text = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin12", "Default Value")
            L_R_ledPin13.Text = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin13", "Default Value")
    
        End Sub
    
        Sub readtextfile()
    
            Using MyReader As New Microsoft.VisualBasic.
                          FileIO.TextFieldParser(
                            "C:\Temp\test.txt")                         ' Leser textfil med denne plasseringen
                MyReader.TextFieldType = FileIO.FieldType.Delimited
                MyReader.SetDelimiters(";")                             ' Linje lesing stopper når ; tegnet kommer
                Dim currentRow As String()
                While Not MyReader.EndOfData
                    Try
                        currentRow = MyReader.ReadFields()
                        Dim currentField As String
                        For Each currentField In currentRow
    
                            Select Case currentField                                                                                'Case funksjon som setter LED pinner av eller på
                                Case "xLedPin10 = 1"
                                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin10", "true")     'LED lys på
                                Case "xLedPin10 = 0"
                                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin10", "false")     'LED lys av
                                Case "xLedPin11 = 1"
                                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin11", "true")     'LED lys på
                                Case "xLedPin11 = 0"
                                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin11", "false")     'LED lys av
                                Case "xLedPin12 = 1"
                                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin12", "true")     'LED lys på
                                Case "xLedPin12 = 0"
                                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin12", "false")     'LED lys av
                                Case "xLedPin13 = 1"
                                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin13", "true")     'LED lys på
                                Case "xLedPin13 = 0"
                                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin13", "false")     'LED lys av
                            End Select
                            Trace.WriteLine(currentField)               ' Gir output for logging
                            'MsgBox(currentField)
                        Next
                    Catch ex As Microsoft.VisualBasic.
                        FileIO.MalformedLineException                       ' feil i tekstfila
                        'MsgBox("Line " & ex.Message & "is not valid and will be skipped.")
                    End Try
                End While
            End Using
        End Sub
    
        Private Sub T_LysAlt_TextChanged(sender As Object, e As EventArgs) Handles T_LysAlt.TextChanged
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "LysAlt", T_LysAlt.Text)
        End Sub
    
        Private Sub T_Lys1_TextChanged(sender As Object, e As EventArgs) Handles T_Lys1.TextChanged
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "Lys1", T_Lys1.Text)
        End Sub
    
        Private Sub T_Lys2_TextChanged(sender As Object, e As EventArgs) Handles T_Lys2.TextChanged
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "Lys2", T_Lys2.Text)
        End Sub
    
        Private Sub T_Lys3_TextChanged(sender As Object, e As EventArgs) Handles T_Lys3.TextChanged
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "Lys3", T_Lys3.Text)
        End Sub
    
        Private Sub T_Lys4_TextChanged(sender As Object, e As EventArgs) Handles T_Lys4.TextChanged
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "Lys4", T_Lys4.Text)
        End Sub
    
        Private Sub T_Skall1_TextChanged(sender As Object, e As EventArgs) Handles T_Skall1.TextChanged
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "Skall1", T_Skall1.Text)
        End Sub
    
        Private Sub T_Skall2_TextChanged(sender As Object, e As EventArgs) Handles T_Skall2.TextChanged
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "Skall2", T_Skall2.Text)
        End Sub
    
        Private Sub T_Skall3_TextChanged(sender As Object, e As EventArgs) Handles T_Skall3.TextChanged
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "Skall3", T_Skall3.Text)
        End Sub
    
        Private Sub T_Skall4_TextChanged(sender As Object, e As EventArgs) Handles T_Skall4.TextChanged
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "Skall4", T_Skall4.Text)
        End Sub
    
        Private Sub T_ErTempKj_TextChanged(sender As Object, e As EventArgs) Handles T_ErTempKj.TextChanged
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ErTempKj", T_ErTempKj.Text)
        End Sub
    
        Private Sub T_ErTempBad_TextChanged(sender As Object, e As EventArgs) Handles T_ErTempBad.TextChanged
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ErTempBad", T_ErTempBad.Text)
        End Sub
    
        Private Sub T_ErTempGang_TextChanged(sender As Object, e As EventArgs) Handles T_ErTempGang.TextChanged
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ErTempGang", T_ErTempGang.Text)
        End Sub
    
        Private Sub T_ErTempStue_TextChanged(sender As Object, e As EventArgs) Handles T_ErTempStue.TextChanged
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ErTempStue", T_ErTempStue.Text)
        End Sub
    
        Private Sub T_ErTempTrapp_TextChanged(sender As Object, e As EventArgs) Handles T_ErTempTrapp.TextChanged
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ErTempTrapp", T_ErTempTrapp.Text)
        End Sub
    
        Private Sub T_ErTempSov1_TextChanged(sender As Object, e As EventArgs) Handles T_ErTempSov1.TextChanged
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ErTempSov1", T_ErTempSov1.Text)
        End Sub
    
        Private Sub T_ErTempSov2_TextChanged(sender As Object, e As EventArgs) Handles T_ErTempSov2.TextChanged
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ErTempSov2", T_ErTempSov2.Text)
        End Sub
    
        Private Sub T_ErTempSov3_TextChanged(sender As Object, e As EventArgs) Handles T_ErTempSov3.TextChanged
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ErTempSov3", T_ErTempSov3.Text)
        End Sub
    
        Private Sub T_SetTempKj_TextChanged(sender As Object, e As EventArgs) Handles T_SetTempKj.TextChanged
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "SetTempKj", T_SetTempKj.Text)
        End Sub
    
        Private Sub T_SetTempBad_TextChanged(sender As Object, e As EventArgs) Handles T_SetTempBad.TextChanged
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "SetTempBad", T_SetTempBad.Text)
        End Sub
    
        Private Sub T_SetTempGang_TextChanged(sender As Object, e As EventArgs) Handles T_SetTempGang.TextChanged
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "SetTempGang", T_SetTempGang.Text)
        End Sub
    
        Private Sub T_SetTempStue_TextChanged(sender As Object, e As EventArgs) Handles T_SetTempStue.TextChanged
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "SetTempStue", T_SetTempStue.Text)
        End Sub
    
        Private Sub T_SetTempTrapp_TextChanged(sender As Object, e As EventArgs) Handles T_SetTempTrapp.TextChanged
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "SetTempTrapp", T_SetTempTrapp.Text)
        End Sub
    
        Private Sub T_SetTempSov1_TextChanged(sender As Object, e As EventArgs) Handles T_SetTempSov1.TextChanged
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "SetTempSov1", T_SetTempSov1.Text)
        End Sub
    
        Private Sub T_SetTempSov2_TextChanged(sender As Object, e As EventArgs) Handles T_SetTempSov2.TextChanged
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "SetTempSov2", T_SetTempSov2.Text)
        End Sub
    
        Private Sub T_SetTempSov3_TextChanged(sender As Object, e As EventArgs) Handles T_SetTempSov3.TextChanged
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "SetTempSov3", T_SetTempSov3.Text)
        End Sub
    
        Private Sub T_ledPin10_TextChanged(sender As Object, e As EventArgs) Handles T_ledPin10.TextChanged
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin10", T_ledPin10.Text)
        End Sub
    
        Private Sub T_ledPin11_TextChanged(sender As Object, e As EventArgs) Handles T_ledPin11.TextChanged
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin11", T_ledPin11.Text)
        End Sub
    
        Private Sub T_ledPin12_TextChanged(sender As Object, e As EventArgs) Handles T_ledPin12.TextChanged
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin12", T_ledPin12.Text)
        End Sub
    
        Private Sub T_ledPin13_TextChanged(sender As Object, e As EventArgs) Handles T_ledPin13.TextChanged
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin13", T_ledPin13.Text)
        End Sub
    
        Private Sub B_readtextfile_Click(sender As Object, e As EventArgs) Handles B_readtextfile.Click
            'Call readtextfile()
            If TimerReadTextfile.Enabled = True Then
                TimerReadTextfile.Enabled = False
                B_readtextfile.Text = "Timer text on"
            Else
                TimerReadTextfile.Enabled = True
                B_readtextfile.Text = "Timer text off"
            End If
        End Sub
    
        Private Sub TimerReadTextfile_Tick(sender As Object, e As EventArgs) Handles TimerReadTextfile.Tick
            Call readtextfile()
        End Sub
    
        Private Sub B_Avslutt_Click(sender As Object, e As EventArgs) Handles B_Avslutt.Click
            End
        End Sub
    
        Private Sub B_Arduino_Click(sender As Object, e As EventArgs) Handles B_Arduino.Click
            'lukke vindu
            My.Forms.Arduino.Show()
        End Sub
    
        Private Sub B_ComTest_Click(sender As Object, e As EventArgs) Handles B_serial_text.Click
            Me.Hide()                                                                                       'lukke vindu
            My.Forms.Text_register_serial.Show()
    
        End Sub
    
        ' Make a new System.IO.Ports.SerialPort instance, which is able to fire events.
        Dim WithEvents COMPort As New SerialPort
    
        Private Sub Receiver(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles COMPort.DataReceived
    
            Do
                RXAscii = 0
                Do
                    RXAscii = COMPort.ReadLine
                    '       RXAscii = COMPort.ReadExisting
                Loop Until (COMPort.BytesToRead = 0)
                '----- End of communication protocol handling -------------------------------------------------------------
                Me.Invoke(New MethodInvoker(AddressOf Display)) ' Start "Display" on the UI thread
            Loop Until (COMPort.BytesToRead = 0)  ' Don't return if more bytes have become available in the meantime
    
        End Sub
    
    
        Private Sub Display()
            Dim readcase As String
            Dim message As String
    
            Received.AppendText(New String(RXAscii))
            Received.AppendText(vbCrLf)        ' Switch to a new line after every transmission
            readcase = RXAscii
    
            ' Trace.WriteLine(Date.Now + readcase.ToLower)               ' Gir output for logging
    
            message = Date.Now + readcase & " is writing to registry"
    
            Select Case readcase.ToLower()
                Case "ledpin10on"
                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin10", "true")     'LED lys på
                    Trace.WriteLine(message)
                Case "ledpin10off"
                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin10", "false")     'LED lys på
                    Trace.WriteLine(message)
                Case "ledpin11on"
                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin11", "true")
                    Trace.WriteLine(message)
                Case "ledpin11off"
                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin11", "false")
                    Trace.WriteLine(message)
                Case "ledpin12on"
                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin12", "true")
                    Trace.WriteLine(message)
                Case "ledpin12off"
                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin12", "false")
                    Trace.WriteLine(message)
                Case "ledpin13on"
                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin13", "true")
                    Trace.WriteLine(message)
                Case "ledpin13off"
                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin13", "false")
                    Trace.WriteLine(message)
    
                Case "a"
                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin10", "true")     'LED lys på
                    Trace.WriteLine(message)
                Case "b"
                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin10", "false")     'LED lys på
                    Trace.WriteLine(message)
                Case "c"
                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin11", "true")
                    Trace.WriteLine(message)
                Case "d"
                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin11", "false")
                    Trace.WriteLine(message)
                Case "e"
                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin12", "true")
                    Trace.WriteLine(message)
                Case "f"
                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin12", "false")
                    Trace.WriteLine(message)
                Case "g"
                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin13", "true")
                    Trace.WriteLine(message)
                Case "h"
                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\HMI_Bolig", "ledPin13", "false")
                    Trace.WriteLine(message)
                Case Else
                    'Trace.WriteLine(message + "case else")
                    Trace.WriteLine(readcase + Date.Now + "Ingen forandring på ledPin")               ' Gir output for logging
            End Select
        End Sub
    
    
        ' Transmitter subroutine.
    
        Private Sub Transmitter(ByVal sender As Object, ByVal e As EventArgs) Handles SendButton.Click
            Received.AppendText(vbCrLf)        ' Switch to a new line after every transmission
            SpaceCount = 0
            Dim TextString As String
            Dim TXArray(2047) As Byte
            'Dim I As Integer
            Dim J As Integer = 0
            Dim Ascii As Boolean = False
            Dim Quote As Boolean = False
            ' Dim Temp As Boolean
            Dim Second As Boolean = False
            Dim TXByte As Byte = 0
            'Dim CharByte As Byte
    
            If COMPort.IsOpen And Ch_Send_Ascii.Checked Then
                TextString = Transmitted.Text
                Try
                    COMPort.Write(TextString)
                Catch ex As Exception
                    MsgBox(ex.Message & "  Check CTS signal or set Flow Control to None.")
                End Try
            Else
                MsgBox("COM port is closed. Please select a COM port")
            End If
    
        End Sub
    
        ' This subroutine handles a change of COM ports.
    
        Private Sub PortSelection(ByVal sender As Object, ByVal e As EventArgs) Handles COMPortsBox.SelectedIndexChanged
            RTSLamp.BackColor = Color.Gray
            DTRLamp.BackColor = Color.Gray
            If COMPort.IsOpen Then
                COMPort.RtsEnable = False
                COMPort.DtrEnable = False
                ClosePort()
                ' NOTE. Because of the use of Invoke, the port should normally be closed from a different thread, see
                '   MaxiTesterClosing, but to ensure that the following delay works, this is not done in this case.
                ' Empty the message queue before the UI thread goes to sleep to update the modem lamps and the ComboBox.
                Application.DoEvents()
                Sleep(200)                      ' Wait 0.2 second for port to close as this does not happen immediately.
            End If
            COMPort.PortName = COMPortsBox.Text
            COMPort.BaudRate = 9600        ' Default for Max-i: 9600 bit/s, 8 data bits, no parity, 1 stop bit
            COMPort.WriteTimeout = 2000         ' Max time to wait for CTS = 2 sec.
            Try
                COMPort.Open()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            BaudRateBox.Text = COMPort.BaudRate.ToString
            BitsBox.Text = COMPort.DataBits.ToString
            ParityBox.Text = COMPort.Parity.ToString
            FlowControlBox.Text = COMPort.Handshake.ToString
            If COMPort.IsOpen Then
                COMPort.RtsEnable = True
                RTSLamp.BackColor = Color.LightGreen
                COMPort.DtrEnable = True
                DTRLamp.BackColor = Color.LightGreen
            End If
        End Sub
    
        ' This subroutine is activated when the form is loaded. It does all the basic initializations.
    
        Private Sub MaxiTesterLoad(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            For Each COMString As String In My.Computer.Ports.SerialPortNames ' Load all available COM ports.
                COMPortsBox.Items.Add(COMString)
            Next
            COMPortsBox.Sorted = True
            BaudRateBox.Items.Add("110")
            BaudRateBox.Items.Add("300")
            BaudRateBox.Items.Add("600")
            BaudRateBox.Items.Add("1200")
            BaudRateBox.Items.Add("1800")
            BaudRateBox.Items.Add("2400")
            BaudRateBox.Items.Add("4800")
            BaudRateBox.Items.Add("7200")
            BaudRateBox.Items.Add("9600")
            BaudRateBox.Items.Add("14400")
            BaudRateBox.Items.Add("19200")      ' Min. FIFO size 3 Bytes (8030 or 8530)
            BaudRateBox.Items.Add("38400")
            BaudRateBox.Items.Add("57600")      ' Min. FIFO size 8 bytes
            BaudRateBox.Items.Add("115200")     ' Min. FIFO size 16 bytes (16C550)
            BaudRateBox.Items.Add("230400")     ' Min. FIFO size 32 bytes (16C650)
            BaudRateBox.Items.Add("460800")     ' Min. FIFO size 64 bytes (16C750)
            BaudRateBox.Items.Add("921600")     ' Min. FIFO size 128 bytes (16C850 or 16C950)
            BitsBox.Items.Add("5")
            BitsBox.Items.Add("6")
            BitsBox.Items.Add("7")
            BitsBox.Items.Add("8")
            ParityBox.Items.Add("None")
            ParityBox.Items.Add("Odd")
            ParityBox.Items.Add("Even")
            ParityBox.Items.Add("Mark")         ' Leaves the parity bit set to 1
            ParityBox.Items.Add("Space")        ' Leaves the parity bit set to 0
            FlowControlBox.Items.Add("None")
            FlowControlBox.Items.Add("RequestToSend")
            FlowControlBox.Items.Add("RequestToSendXOnXOff")
            FlowControlBox.Items.Add("XOnXOff")
            RTSLamp.BackColor = Color.Gray
            DTRLamp.BackColor = Color.Gray
            CTSLamp.BackColor = Color.Gray
            DSRLamp.BackColor = Color.Gray
        End Sub
    
    
        ' This subroutine is used to close the COM port. Because the program uses Invoke instead of BeginInvoke, this
        '   routine is usually called on a separate (new) thread to prevent a close-down deadlock.
    
        Private Sub ClosePort()
            If COMPort.IsOpen Then COMPort.Close()
        End Sub
    
        Private Sub ClearReceivedText(ByVal sender As Object, ByVal e As EventArgs) Handles ClearButton.Click
            Received.Text = ""
            SpaceCount = 0
        End Sub
    
        ' This subroutines inserts a Break condition.
    
        Private Sub SendBreak(ByVal sender As Object, ByVal e As EventArgs) Handles BreakButton.Click
            If COMPort.IsOpen Then
                COMPort.BreakState = True
                Sleep((11000 / COMPort.BaudRate) + 10)
                ' Min. 11 bit delay (startbit, 8 data bits, parity bit, stopbit). 10 mS have been added to ensure that
                '   the delay is always active. If the delay time is less than the task switching time, the two BreakState
                '     instructions may be activated immediately after each other ! On a multiprocessor system, you must
                '       add 15 mS instead.
                COMPort.BreakState = False
            Else
                MsgBox("No COM Port Selected")
            End If
        End Sub
    
        ' This subroutine handles a change in Baud Rate.
    
        Private Sub BaudRateSelection(ByVal sender As Object, ByVal e As EventArgs) Handles BaudRateBox.SelectedIndexChanged
            COMPort.BaudRate = CInt(BaudRateBox.Text)
        End Sub
    
        ' This subroutine handles a change in number of data bits
    
        Private Sub DataBitsSelection(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BitsBox.SelectedIndexChanged
            COMPort.DataBits = CInt(BitsBox.Text)
        End Sub
    
        ' This subroutine handles a change in the parity.
    
        Private Sub ParitySelection(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ParityBox.SelectedIndexChanged
            COMPort.Parity = CType([Enum].Parse(GetType(Parity), ParityBox.Text), Parity)
        End Sub
    
        ' This subroutine handles a change in the flow control (handshake).
    
        Private Sub SoftwareFlowControlSelection(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FlowControlBox.SelectedIndexChanged
            COMPort.Handshake = CType([Enum].Parse(GetType(Handshake), FlowControlBox.Text), Handshake)
        End Sub
    
        ' This subroutine updates the modem control signal lamps
    
        Private Sub ModemLamps(ByVal sender As Object, ByVal e As SerialPinChangedEventArgs) Handles COMPort.PinChanged
            If COMPort.DsrHolding Then
                DSRLamp.BackColor = Color.LightGreen
            Else
                DSRLamp.BackColor = Color.Gray
            End If
            If COMPort.CtsHolding Then
                CTSLamp.BackColor = Color.LightGreen
            Else
                CTSLamp.BackColor = Color.Gray
            End If
        End Sub
    
        ' This subroutine saves the content of the Received TextBox
    
        Private Sub SaveText(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim SaveFileDialog1 As New SaveFileDialog()
            SaveFileDialog1.Filter = "Text Files (*.txt)|*.txt"
            SaveFileDialog1.Title = "Save Received As"
            If SaveFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK _
                And SaveFileDialog1.FileName.Length > 0 Then
                My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, Received.Text, False) ' Overwrite file
            End If
        End Sub
    
    
    End Class
    

    I'm new to programing so any help would be grate. Thanks

    Espen

    Sunday, October 22, 2017 11:01 PM

All replies

  • You should insert a breakpoint at the start of the select case and trace the code through one line at a time while watching the values of the variables. That will show which case is getting matched or not,  and why the code for that match isn't executing.   If a string that looks like it should match isn't matching then you need to examine the string in binary (eg, show as Hex) to make sure that there are no non-displaying characters in the data stream.  My guess is that there is a Cr or Cr/Lf at the end of the text that is not accounted for in the select case strings.


    • Edited by Acamar Sunday, October 22, 2017 11:17 PM fmt
    Sunday, October 22, 2017 11:16 PM
  • I would suggest focusing on one part of the code that is not behavior as expected, place a regular break-point and when hit inspect data and variables to determine what is going on.

    You can also set break-points with conditional expressions by first setting a break-point, hover over it, select the gear/setting.

    Set a condition (you get Intellisense when doing this) e.g.

    Un-check "Continue execution" so the debugger will break on the condition. Of course if the condition is not hit there is something wrong with your logic so you then might want to go back to a conventional break-point.

    Bottom line is few developers take time to learn how to use the power of the Visual Studio debugging tools. Me, I pretty much use them at least once a day in a typical week, not only for when something is not working as expected but also to understand how a particular piece of code is working (especially if someone else wrote it).

    When you say "I'm new to programing so any help would be grate", this is one thing that will serve you well if this is the type of work you plan on doing for a living.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, October 22, 2017 11:27 PM
    Moderator
  • Thank you both for got replays.

    I have tried to put in some break point to see whats going on.

    I sent a "a" from program to the arduino board. And it replays withe a "ledpin10on" and "a" as I have programed.

    The break code is then.

    Received.AppendText(New String(RXAscii))
        RXAscii: "ledpin10on" & vbCr
        Received.: {Text=vbCrLf & "ledpin10on" & vbCr & vbCrLf & "a" & vbCr & vbCrLf & vbCrLf}

    Received.AppendText(vbCrLf)    
        RXAscii: "ledpin10on" & vbCr
        Received: {Text = vbCrLf & "ledpin10on" & vbCr & vbCrLf & "a" & vbCr & vbCrLf & vbCrLf & "ledpin10on" & vbCr}

    readcase = RXAscii
        RXAscii: "ledpin10on" & vbCr
        Received: {Text = vbCrLf & "ledpin10on" & vbCr & vbCrLf & "a" & vbCr & vbCrLf & vbCrLf & "ledpin10on" & vbCr}

    Select Case readcase.ToLower()
            message: "23.10.2017 21:52:49ledpin10on" & vbCr & " is writing to registry"
            readcase: "ledpin10on" & vbCr
            
    Case Else
        message: "23.10.2017 21:52:49ledpin10on" & vbCr & " is writing to registry"

    For me it looks like i have to many vbCr and vbCrLf. So I will try to change the code

    Arduino
     removed:

        Serial.println("");
        Serial.println("b");
    	
    //So it will only print on line per case.
          case 'a':
            digitalWrite(ledPin10, HIGH);
            Serial.println("ledpin10on");
            LEDState1 == HIGH;
            break;
    		


    VB code change

     Received.AppendText(New String(RXAscii))
            Received.AppendText(vbCrLf)        ' Switch to a new line after every transmission
            readcase = RXAscii

    To

            Received.AppendText(New String(RXAscii))
            readcase1 = RXAscii
            Received.AppendText(vbCrLf)        ' Switch to a new line after every transmission
    
            message = Date.Now + readcase & " is writing to registry"
    
            readcase1 = Replace(readcase, "& vbCr", "")
            '"ledpin10on" & vbCr
    
            Select Case readcase.ToLower()

    I tried to use the Replace function to remove "& vbCr" from the string. But when I came down to the
    Select Case readcase.ToLower() the string was empty

    I think I'm on the right track but I need some more help with cleaning up my string before the case statment.

    Tried to follow this example

    Dim withParts As String = "Books and Chapters and Pages"
    Dim withoutParts As String = Replace(withParts, "and ", "")

    I'm pretty new to programing like this. Structured text. I use codesys in my work. But then I use ladder, function block diagram and sequential function chart. We use it to control buildings, so it lots of simple in and outputs.

    The Visual basic programing now is in a school project. Hopefully I can get a little better understand so I can do a even better job:)

    Thank you

    Espen

    Monday, October 23, 2017 9:05 PM
  • Tried to follow this example

    Dim withParts As String = "Books and Chapters and Pages"
    Dim withoutParts As String = Replace(withParts, "and ", "")

    Something like:

    RXAscii = RXAscii.Replace(ChrW(13), "")
    RXAscii = RXAscii.Replace(ChrW(10), "")

    It would be preferable to fix it at the source.
    • Edited by Acamar Monday, October 23, 2017 10:27 PM fmt
    Monday, October 23, 2017 10:26 PM