Controlling an Arduino with VBScript

Hello everyone,

I have an Arduino Uno I need to control using VBScript. This is the script I've been using to write to the serial buffer:

Const ForWriting = 2
Dim fso, f

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("COM7:9600,N,8,1", ForWriting)

WScript.Sleep(5000)
f.Write "3"
WScript.Sleep(5000)
f.Close

I get an error when attempting to run the script that says "File not found" referencing the 5th line. COM7 is the USB port my Arduino is connected to. The IDE / serial monitor are closed. My OS is Windows 11. Any help with this would be greatly appreciated.

Hello, Alex. Welcome to the Arduino forum.
You seem to be missing the Arduino half of your project. There needs to be an Arduino program to read the data you are sending to it and to interpret the message and do what you ask. You, yourself, need to write that Arduino program, so both the VB program and the Arduino program can work as a unit.

Hello Paul,

Below is the Arduino code to read the serial buffer. Currently I'm just flashing an LED but down the line the controller will (hopefully) run a motor on a scientific instrument I'm working on.

int lightPin = 12; //Pin for the led
int pulseCount = 3; // Number of times the led flashes
int delayTime1 = 500; // Delay time
int delayTime2 = 10; //Longer Delay
int i = 0; //Counter
int testBlinks = 0;

#include <HardwareSerial.h>

void setup() {
  // put your setup code here, to run once:
pinMode(lightPin, OUTPUT);
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  while(i < 1){
    if( Serial.available() > 0 ){
      delay(delayTime2); //Delay to give buffer time to fill
      testBlinks = 0; //Set test blinks to zero
      testBlinks = readBuffer(); //Read buffer and apply number to test blinks
      blink( testBlinks) ; // Call blink function to blink led set # of times
    }
  }
}


//A function to read the serial buffer

char incBuffer; //Variable to store incoming character from buffer
int bufferSize = 0; //Variable to store size of buffer when passed

int readBuffer(){
  //Determine buffer size and initialize a character array
  bufferSize = Serial.available();
  char bufferContents[ bufferSize - 1 ];
  //Iterate through the buffer. If a character is not the new line character add it to the array. If it is, terminate the array with \0
  for( int j = 0 ; j <= bufferSize - 1 ; j++ ){
    incBuffer = Serial.read();
    if( incBuffer != '\n' ){
      bufferContents[ j ] = incBuffer;
    }
    else if( incBuffer == '\n' ){
      bufferContents[ j ] = '\0';
      return( atoi( bufferContents ) ); //combine to int before returning
    }

  }
}

//A function to blink an led

void blink(int numberOfBlinks){
  for(int k = 0 ; k < numberOfBlinks ; k++){
    digitalWrite(lightPin, HIGH);
    delay(delayTime1);
    digitalWrite(lightPin, LOW);
    delay(delayTime1);
  }
}

Edit I meant to add that this code does what I expect when using the IDE's built in serial monitor

I haven't looked at VB in 30 years! Don't expect you will find much help on an Arduino forum.

Hello alexbrodie

Welcome to the worldbest Arduino forum ever.

Take some time read, study, modify and test the "Serial Event example" provided by the IDE.

hth

Have a nice day and enjoy coding in C++.

https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/opentextfile-method

Is "COM7:9600,N,8,1" a valid file name?

Shouldn't it look more like "c:\testfile.txt" as used in the example on the above link.

Syntax
object .OpenTextFile (filename , [ iomode , [ create , [ format ]]])Blockquote

Would it be a good idea to have the "create" parameter set to true, so that the missing file is created?

create Optional. Boolean value that indicates whether a new file can be created if the specified filename doesn't exist. The value is True if a new file is created; False if it isn't created. The default is False.

opened COM ports in Visual Studio VB.NET without problem but VBScript is a different technology
for VBScript try a web search for vbscript com port you will get plenty of links