James Oswald
James Oswald

Reputation: 603

Passing data to arduino through windows command line

Im attempting to play with sending data to my arduino and keep running into the same problem. The code on the arduino is as follows:

void setup()                   
{
  Serial.begin(9600);
  for (int i = 3; i <= 13; i++)
  {
    pinMode(i, OUTPUT);
    digitalWrite(i, LOW);
  }
}

void loop()                      
{  
  if (Serial.available())
  { 
    char ch = Serial.read();
    int it = ch - '0';
    digitalWrite(it, HIGH);
    delay(1000);
    digitalWrite(it, LOW);
  }
} 

This basically makes it so when you send a character over the serial monitor to the device it lights up the light connected to the specified pin for one second. In the built in serial monitor this works fine, you send the device a number 1-9 (haven't figured out how to do 10+ yet) and the specified light turns on, just as intended. However, my goal is to write a c++ program to send data to the device using the system() command from windows.h. Before i can do that i need the command to send data to the device. I found:

echo i > COM1 //with i being the number to be sent over

Well i tried that and got a fairly interesting result half the time i would receive this message in the command line:

C:/users/XXXXX> echo 7 > COM3 //im 100% sure im using com3
Access is denied

The other half of the time i would see the data go through (The RX light would light up) but nothing would happen, the light connected to pin 7 wouldn't light up. I immedatly thought that you might need to pass the data in ASCII, but nope,

echo 55 > COM3

produced the same result. If any one knows how to send data over window command line to arduio i would really appreciate it, thanks.

Upvotes: 1

Views: 4053

Answers (2)

a_netanel
a_netanel

Reputation: 161

I had a similar problem.

First, the "Access is denied" error is caused by the Serial Monitor holding the port.

Second, simply "echoing" a string will not work properly, because you also send along the line termination. The trick is to send something like this:

set /p x="A" <nul >\\.\COM4

Source: https://batchloaf.wordpress.com/2013/02/12/simple-trick-for-sending-characters-to-a-serial-port-in-windows/

Upvotes: 0

James Oswald
James Oswald

Reputation: 603

Welp after no response here and 7 more hours of research i finally found the solution to my problem here. basically from command line do

powershell //to enter powershell
$port= new-Object System.IO.Ports.SerialPort COM#,Baudrate,None,8,one 
//to create a new port object
$port.open() //to open a connection
$port.WriteLine(data)
$port.close()

Looks like the key thing is that you cant just send raw data over to the arduino, you need to first open a connection before with the arduino before it will actually recognize the data as valid serial input.

update: If you want to run it from command line all you need to do is write a powershell script like so:

$com = $args[0]
$baud = $args[1]
$write = $args[2]

$port = $port= new-Object System.IO.Ports.SerialPort $com,$baud,None,8,one
$port.open()
$port.write($write)
$port.close()

Which can then be called from the command line and have the arguments passed like so:

powershell.exe -ExecutionPolicy Bypass -file filelocation/test.ps1 COM3 2400 7

Upvotes: 3

Related Questions