Arduino Serial.read (from bat file W7) [Solved]

ok im new to arduino and having a really hard time trying to convince arduino to do what i want, so please correct me if i do anything wrong :slight_smile:

so i have Win7 64bit & arduino-1.0.5-r2 - software (tested on multiple computers with unsucessfull results)
Arduino UNO & arduino Nano - hardware (tested same thing on both devices with same unsucessfull results)

reproduction of problem:
1/ install driver (from arduino-1.0.5-r2 folder)
2/ port and arduino selected in menu correctly
3/ load code check & upload correct too
4A/ ctrl+shift+M open up a serial monitor
5A/ type "on" without quotes, send COM4 everything works led13 is up and shining

4B/ commandline (serial monitor is closed, so port is not occupied)
5B/ type "echo on > COM4" without quotes hit enter nothing happends

4+5C/ i also tryed php fopen COM3, but results were the same as previous

so that is basically my problem i am unable to communicate with arduino in any else program than serial monitor
i have checked that serial port is 9600 baud on both sides, and virtually everythinkg and nothing works i am lost and cant find a working solution, please help, advise working solution to this

source code:

void setup()
{
pinMode(13, OUTPUT); // Set pin 13 as digital out
Serial.begin(9600); // baud rate
Serial.flush();
}

void loop()
{
String input = "";
while (Serial.available() > 0)
{
input += (char) Serial.read();
delay(5);
}

if (input == "on")
digitalWrite(13, HIGH);

if (input == "off")

digitalWrite(13, LOW);
}

trying to make work:

echo on > COM4

You have nothing in your Arduino code to enable it to discard any irrelevant characters so it may simply be that it is trying to intepret something like "no" or "CRo".

This demo of communicating with an Arduino using Python may be helpful. You can also send the data from the Serial Monitor and it may work the way you are trying. The data starts with < and ends with > so the Arduino knows where it is.

...R

4+5C/ i also tryed php fopen COM3, but results were the same as previous

Your Arduino is connect to COM4, and you get no response on COM3. Gee, I wonder why.

5B/ type "echo on > COM4" without quotes hit enter nothing happends

Did the RX light on the Arduino flash? If so, your statement is patently wrong.
If not, how, using echo, are you setting the baud rate, the number of start bits, the parity, etc?

Have you tried
echo "on" > COM4
?

ok, i tryed using method Robin2 mentioned and this is what i get:

const byte buffSize = 40;
char inputBuffer[buffSize];
byte bytesRecvd = 0;
boolean readInProgress = false;
boolean newDataFromPC = false;

char messageFromPC[buffSize] = {0};

void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT); // Set pin 13 as digital out
}

//=============

void loop() {
getDataFromPC();
if (strcmp(messageFromPC, "on") == 0) {
digitalWrite(13, HIGH);;
}

if (strcmp(messageFromPC, "off") == 0) {
digitalWrite(13, LOW);;
}
}

void getDataFromPC() {
if(Serial.available() > 0) {
char x = Serial.read();

if (x == '>') {
readInProgress = false;
newDataFromPC = true;
inputBuffer[bytesRecvd] = 0;
char * strtokIndx; // this is used by strtok() as an index

strtokIndx = strtok(inputBuffer,","); // get the first part - the string
strcpy(messageFromPC, strtokIndx);
}

if(readInProgress) {
inputBuffer[bytesRecvd] = x;
bytesRecvd ++;
if (bytesRecvd == buffSize) {
bytesRecvd = buffSize - 1;
}
}

if (x == '<') {
bytesRecvd = 0;
readInProgress = true;
}
}
}

well, i got further than before
after i upload the code it does not work .... fail, but, then i run serial monitor, type nothing and close him
after that commandline WORKS! (first success yay!)

echo "" > COM4

obviously serial monitor changes COM4 settings, that i have confirmed with

MODE COM4

right now i am trying to replicate same state as serial monitor does

PaulS
sorry for 4+5C example, it was from second PC using COM3, so the port is always right
i have tryed following statements

echo on > COMn
echo "on" > COMn
echo "on\n" > COMn
echo "on\r" > COMn
echo ON > COMn

none of them worked
as i understand serial monitor doesn't send anything except raw text

on

so my goal is also to send raw text

ok, im stuck at DTR any ideas?
using windows

MODE COM4

i found following:

Windows default state:

Status for Device COM4:

Baud: 115200
Parity: None
Data Bits: 8
Stop Bits: 2
Timeout: OFF
XON/XOFF: OFF
CTS handshaking: OFF
DSR handshaking: OFF
DSR sensitivity: OFF
DTR circuit: OFF
RTS circuit: OFF

possible options:

MODE COM4 BAUD=9600 PARITY=n DATA=8

result:

Status for Device COM4:

Baud: 9600
Parity: None
Data Bits: 8
Stop Bits: 1
Timeout: OFF
XON/XOFF: OFF
CTS handshaking: OFF
DSR handshaking: OFF
DSR sensitivity: OFF
DTR circuit: OFF
RTS circuit: OFF

command

echo "" > COM4

not working
after running serial monitor from arduino and closing again

Status for Device COM4:

Baud: 9600
Parity: None
Data Bits: 8
Stop Bits: 1
Timeout: OFF
XON/XOFF: OFF
CTS handshaking: OFF
DSR handshaking: OFF
DSR sensitivity: OFF
DTR circuit: ON
RTS circuit: OFF

notice DTR

command

echo "" > COM4

works

if i googled right DTR is value on serial port set by arduino ?
how do i do that ?

Faction:
ok, i tryed using method Robin2 mentioned and this is what i get:

It looks like you modified my code, or modified your code to look like like mine.

Start simple. Get my code to work unchanged.

If you have problems with that I may be able to help.

When you know that it works you can try changing things.

...R

if i googled right DTR is value on serial port set by arduino ?
how do i do that ?

It is not set by the Arduino. It IS set by the Serial Monitor application.

help mode
shows that dtr=on is an option that can be used on the mode command.

Robin2
yes i used your code and it worked well, so i started modifing it, cuting useless off, i need only / for now, from that point i can go on, after i finish everything, i will submit fully working solution here
unfortunetly i am having problems with serial port, not related to script itself

PaulS
thanx, i misstyped this when i was trying to set DTR=ON (DRT) myself, it works fine now

MODE COM4 BAUD=9600 PARITY=n DATA=8 DTR=ON
echo "" > COM4

is working!

im going to sum this up and write down fully working solution for this when i finish it

Solved! its not great but it does what i need it to do

Arduino:

void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT); // Set pin 13 as digital out
}

void loop() {
String input = "";
while (Serial.available() > 0)
{
input += (char) Serial.read();
delay(5);
}

if (input.indexOf("on") >=0)
digitalWrite(13, HIGH);
if (input.indexOf("off") >=0)
digitalWrite(13, LOW);

}

PC (bat)

MODE COM4 BAUD=9600 PARITY=n DATA=8 DTR=ON
echo "on" > com4

PC (php)

<?php if (!ISSET($_GET["stav"])) $_GET["stav"] = "off"; exec("MODE COM4 BAUD=9600 PARITY=n DATA=8 DTR=ON"); sleep(2); // it takes 1.3 sec for MODE until its applyed on RS232 $fp = fopen("COM4", "w"); fwrite($fp, $_GET["stav"]); fclose($fp); ?>onoff

the mode part "MODE COM4 BAUD=9600 PARITY=n DATA=8 DTR=ON" has to be run only once, but after each reboot or arduino upload or any serial port reset

thank you very much both for parser Robin2 & for serial port mode PaulS