Way to utilize a variable on the computer, on the arduino micro

Arduino: Micro. Goal: Check if system is Windows 7 or other and then go through certain operations specific to the distro. Sample Code:

Keyboard.println("powershell.exe"); delay(1000); Keyboard.println("$winVersion = Get-WmiObject -Class Win32_OperatingSystem | ForEach-Object -MemberName Caption"); //this is apart of the immediate above line

if ( Keyboard.println("$winVersion") == Keyboard.println("Microsoft Windows 7 Professional") ) { ........stuff } else { ........other stuff }

Problem: Since i'm using the Keyboard.h library, I'm only sending keystrokes that can only be stored on the computer locally. Because that's the case, I'm unable to compare the two from the arduino.

Side note: I cannot place the if-statement within the keyboard.println function because that would not align with my goal above. Based on the distribution, I'm going to perform a different set of commands.

Is this possible or impossible? Lead me down the right path and if you need any additional information, let me know. Thank you!

If you send keystrokes to your PC you need to have something listening on the other side... you can’t really know which program has the Keyboard focus from your arduino

And if you want to read something back, then you neeed to ensure that the program which was listening sends something back through Serial back to your arduino, not just display something on your pc screen.

I have looked into Serial and it seems like the right direction since it is, "Used for communication between the Arduino board and a computer". I've played with it a bit and getting some understanding but the issue now is the listener you mentioned. From my interpretation, you believe there isn't a way to construct one. But what's a feasible way? if there is another way I can check the OS version for compatibility purposes, then i'm all ears. Or will I have to fix the configuration for one type of OS?

Thanks!

EDIT

So.. I found a way to interact. Since I'm using powershell along with this.. I came across 2 articles that makes it seem feasible. Have I found a solution? No. Do I feel like I'm going down the right path? I believe so.

Link for powershell component to open/read serial port - https://blogs.msdn.microsoft.com/powershell/2006/08/31/writing-and-reading-info-from-serial-ports/

Link for something. didn't actually use anything in this article. Just gave me the idea to use powershell since they are using python- https://www.meccanismocomplesso.org/en/arduino-tutorial-serial-data-actuator/

Update, unsure on how where to go from here. I’ve come up with code that I hope works but unsure because of two factors: (1) I can’t monitor what the computer is sending through the serial; (2) Unsure on how the loop() interacts and starts with setup(). Here’s my code.

#include <HID.h>
#include <Keyboard.h>

int incomingByte = 0;   // for incoming serial data
int win7Test[] = {77,105,99,114,111,115,111,102,116,32,87,105,110,100,111,119,115,32,55,32,80,114,111,102,101,115,115,105,111,110,97,108}; //predefined array
int comparetoArray[] = {}; // initialization to compare to append to and compare to above predefined array
int indexer = 0; //counter to replicate loop

// Init function
void setup()
{
  Keyboard.begin(); // Start Keyboard and Mouse
  Serial.begin(9600); //opens serial at 9600 rate
  delay(500);
  
  Keyboard.println("powershell.exe"); //start powershell
  delay(1000);

  Keyboard.println("$winVersion = Get-WmiObject -Class Win32_OperatingSystem | ForEach-Object -MemberName Caption"); //powershell command that gets the OS version


  Keyboard.println("$port= new-Object System.IO.Ports.SerialPort $( [System.IO.Ports.SerialPort]::getportnames()),9600,None,8,one"); //powershell's way of interacting with the serial port by getting the port the arduino is connected to and setting the rate
  Keyboard.println("$port.open()"); //opening the serial port
###### 1 ##########
  Keyboard.println("$port.WriteLine(\"$($winVersion)\")"); //writing the OS version to the arduino through the serial port 

  

         ######### 2 ###############
          if (win7Test == comparetoArray) //comparing the predefined array with newly created array
          {
            Keyboard.println("They're equal!"); //test output
          }
          else
          {
            Keyboard.println("They're NOT equal!"); //test output
  
  Keyboard.end(); //stop keyboard takeover
  
}


void loop()  
{
########### 3 ###########
     if (Serial.available() > 0) { //checking if there is anything going through the serial port
          // read the incoming byte:
          incomingByte = Serial.read(); //reads the decimal coming in and storing in variable

          comparetoArray[indexer] = incomingByte; //storing incoming decimals to compare to predefined array above
          indexer++; // plus 1 counter to replicate loop structure

          }

  }
}

@ #### 1 #### referencing the command Keyboard.println("$port.WriteLine(\"$($winVersion)\")");
As I’ve mentioned, since I can’t monitor what’s being sent through the serial port, I’m not sure if the variable I’m sending is being utilized. The variable $winVersion has the OS version (Ex: Microsoft Windows 7 Professional). If you’re not familiar with powershell, variables have to lead with a ‘$’ to be referenced and I have it encapsulated with “$()” because i’m storing it as an object to pass. Sometimes it will work with or without depending on what’s inside, this is just my safe way since I’m unsure of what’s being passed.

@ #### 2 #### referencing the entire if statement
I don’t know where to put this. Since it relies on the loop to complete the array, normally I would put it after the loop finishes but that can’t be the case in this language and therefore stuck.

@ #### 3 #### referencing the entire if statement
This is tied to ##2## but not exaclty. I want more information on when this loop is executed. After the entire setup()? The only reason I need it is when I’m passing values from the computer to the serial port and unsure where to place that besides here but this doesn’t seem right. Where exactly in the code?

 Keyboard.println("$port= new-Object System.IO.Ports.SerialPort $( [System.IO.Ports.SerialPort]::getportnames()),9600,None,8,one"); //powershell's way of interacting with the serial port by getting the port the arduino is connected to and setting the rate
  Keyboard.println("$port.open()"); //opening the serial port
###### 1 ##########
  Keyboard.println("$port.WriteLine(\"$($winVersion)\")"); //writing the OS version to the arduino through the serial port

By the way, this is only a snippet of the code for relevant information only. So if you try and copy & paste this to run and see the errors, there MIGHT be some errors. I don’t mind releasing all of this bad code but i’m just showing the relevant parts so there is less for your eyes to parse through. Thanks again!

If you can decide what will run on the pc (build a companion program with Processing for example) then that program can listen to a selected serial com port, interface with your pc and the end user, and send stuff back.

Why do you really need to know which os is on the other side to send just keystrokes without knowing who is the listening app? What are you trying to do?

IF #####1##### is supposed to print something to Serial for the Arduino and #####2##### is supposed to compare that something to some other something, then shouldn't there be some lines between that read the something from the Serial line?