Passing data to arduino through windows command line - arduino

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.

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

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/

Related

Sim800L lag/delay before incoming calls are visible to arduino

I use SIM800L GSM module to detect incoming calls and generally it works fine. The only problem is that sometimes it takes up to 8 RINGS before the GSM module tells arduino that someone is calling (before RING appears on the serial connection). It looks like a GSM Network congestion but I do not have such issues with normal calls (I mean calls between people). It happens to often - so it cannot be network/Provider overload. Does anybody else had such a problem?
ISP/Provider: Plus GSM in Poland
I don't put any code, because the problem is in different layer I think
sorry that I didn't answer earlier. I've tested it and it turned out that in bare minimum code it worked OK! I mean, I can see 'RING' on the serial monitor immediately after dialing the number. So it's not a hardware issue!
//bare minimum code:
void loop() {
if(serialSIM800.available()){
Serial.write(serialSIM800.read());
}
if(Serial.available()){
serialSIM800.write(Serial.read());
}
}
In my real code I need to compare calling number with the trusted list. To do that I saved all trusted numbers in the contact list on the sim card (with the common prefix name 'mytrusted'). So, in the main loop there's if statement:
while(mySerial.available()){
incomingByte = mySerial.read();
inputString += incomingByte;
}
if (inputString.indexOf("mytrusted") > 0){
isTrusted = 1;
Serial.println("A TRUSTED NUMBER IS CALLING");
}
After adding this "if condition" Arduino sometimes recognize trusted number after 1'st call, and sometimes after 4'th or 5'th. I'm not suspecting the if statement itself , but the preceding while loop, where incoming bytes are combined into one string.
Any ideas, what can be improved in this simply code?
It seems, I found workaround for my problem. I just send a simple 'AT' command every 20 seconds to SIM800L (it replies with 'OK' ). I use timer to count this 20 seconds interval (instead of simply delay function)
TimerObject *timer2 = new TimerObject(20000); //AT command interval
....
timer2->setOnTimer(&SendATCMD);
....
void SendATCMD () {
mySerial.println("AT");
timer2->Stop();
timer2->Start();
}
With this simple modification Arduino always sees incoming call immediately (after 1 ring)

Two xbee beacons drowning each other out?

I'm trying to create a pair of arduino/xbee beacons that will transmit some information (ultimately GPS coordinates) to each other at all times. The problem is that when they're both on, neither one seems to receive. I'm guessing it's because both might be transmitting at the same time and drowning the others message out. However, I'm not sure of a better approach since neither one is a slave or master. Here's my setup:
Hardware:
A pair of XBee-Pro 900 XSC S3B xbees.
A pair of teensy (arduino compatible with multiple UARTs)
XBee Setup
VID: 542D
DT: 8153
Serial connected to Serial 3 on Teensy
Code
long lastTxTime = 0
void setup() {
Serial.begin(9600);
Serial3.begin(9600);
}
void loop() {
long now = millis();
// Send ever 0.5 seconds
if (now - lastTxTime > 500) {
Serial.println("SEND!");
Serial3.println("WOOT!");
lastTxTime = now;
}
delay(100);
// Print anything received
while(Serial3.available()) {
char c = Serial3.read();
Serial.print(c);
}
}
When I connect both Teensy's to separate terminals, both of them output "SEND" and rarely receive a "WOOT". I put one of the XBees straight on a USB board to a terminal and it output most of the "WOOT" messages from the other.
What can I do to make this work?
What you need to look into is flow control. In the wireless standard, they use RTS (Request to Send) & CTS (Clear to Send). The RTS/CTS pins on Xbee might be what you need,

SMS is truncated when I read it from Siemens TC35 GSM module and Arduino

I'm doing some experiments with Arduino+Siemens TC35 GSM module and I would like to be able to read an SMS that I send to this device.
I have assembled my device following more or less this scheme:
with the difference that I don't use a buzzer nor a relay, just an LCD display. You can see the full picture here:
The scheme should work, because for example I have been able to send an SMS from Arduino to my mobile phone, but I'm having some problems parsing the SMS I send to my Arduino.
(note: I will hide my number substituting some numbers with ***)
I initialize the GSM module like this:
mySerial.print("AT+CMGF=1\r\n");
and I try to read my SMS like this:
void readSMS()
{
mySerial.print("AT+CMGR=6\r\n");
delay(1000);
char c;
while (mySerial.available()>0){
c = (char)mySerial.read();
Serial.print(c);
}
}
but I always get a truncated SMS. This is what I see in my Serial monitor:
AT+CMGF=1
OK
AT+CMGR=6
+CMGR: "REC READ","AT+CMGR=6
+CMGR: "REC READ","+4475********",,"14/04/25,21:08:AT+CMGR=6
+CMGR: "REC READ","+4475********",,"14/04/25,21:08:AT+CMGR=6
+CMGR: "REC READ","+4475********",,"14/04/25,21:08:AT+CMGR=6
+CMGR: "REC READ","+4475********",,"14/04/25,21:08:AT+CMGR=6
what's wrong with my code?
Thank you so much for any help.
p.s: also other commands that are supposed to work (for example the one to delete all SMS: AT+CMGD=1,4) don't work at all and give me error.
p.p.s: I wish I could use the GSM.h library that is available for Arduino, but I guess it's only compatible with the original Arduino GSM Shield.
I'm not an Arduino expert in any capacity, so there may be better ways to do this in the API, but I'd try something like this (delays can probably be lowered)
void readSMS()
{
mySerial.print("AT+CMGR=6\r\n"); // Send request
int count = 5; // Number of 100ms intervals before
// assuming there is no more data
while(count-- != 0) { // Loop until count = 0
delay(100); // Delay 100ms
while (mySerial.available() > 0){ // If there is data, read it and reset
c = (char)mySerial.read(); // the counter, otherwise go try again
Serial.print(c);
count = 5;
}
}
}
Another - probably better - option would be to just loop without a delay until you get a complete answer. That of course assumes that you know what to look for (<cr><lf>OK<cr><lf> would seem to be the case here, but I'm too weak on the Hayes spec to be sure)

Arduino Serial Communication not receiving entire message

I have a problem with the Arduino communication. It's quite hard to describe so I cant fit it in the title. Anyway here are the following:
So I have this code for my receiving end:
if(Serial1.available())
{
while(Serial1.available())
{
uint8_t inByte = Serial1.read();
inByte = inByte ^ k;
Serial.write(inByte);
}
Serial.println(" done");
}
It's supposed to print in one line and print done when it's done. The Serial1.available() seems to skip the next Serial1.available(), I don't know what's going on. Anyway here's my current, bad, output:
h done
e done
l done
l done
o done
done
when it should be:
hello done
I'm sorry if this could've been phrased better but that's all I can type now, my brain is kinda in pain. I've never experienced this behavior in a Windows c++ console application.
If you are calling that routine in loop() then yes, it will read from the serial buffer and immediately return since you are probably not sending the data fast enough.
A better way to handle this sort of thing is to use a control char which indicates the end of a message OR if you have a specific data format you expect to receive, then keep a count of the chars which have come in until the data format limit is reached.
There is discussion here which you may find useful: Serial Duplex using Arduino Also there are example sketches that ship with the Arduino IDE: Menu: Examples: Communication:
Also, read all the entries under the Serial listing for Arduino. Good stuff there.
So the routine you develop for working with Serial input really depends on your project and the kind of data you are receiving. In your example above, if you were to use a control char, it might look like this:
while(Serial1.available()){
char c = Serial1.read();
if (c == '*'){
Serial.println(" done");
} else {
Serial.write(c);
}
}

Strange initial output using Serial.print

When I'm writing to the serial interface, I'm getting strange and unexpected output when my sketches first run. The output seems to be a variant of what should be printed:
eg:
String text1 = "foobar";
void setup() {
Serial.begin(9600);
Serial.print("\n");
Serial.print(text1);
}
void loop() {
}
Results in the output:
fo
foobar
(the new line appears before "fo" but I couldn't work out how to include it).
So some variant of whatever is supposed to be printed, gets printed before the actual text that is supposed to be printed. Changing the output, changes the anomalous text (sometimes it'll be two characters, sometimes three). Making changes that don't affect the output and recompiling has no effect on the anomalous text.
I'm a total Arduino newbie (I only started writing my own code today), but I can only assume this isn't normal.
I'm using a Freetronics EtherTen and the 1.0 IDE
thanks in advance
Arduino is restarting your sketch when you open its serial port on the computer.
so it prints out, and then initialized again.
after
Serial.begin(9600);
try to put either:
delay(500)
or
while (!Serial); // while the serial stream is not open, do nothing:
This is most likely a Serial communication Reset issue as Eran W pointed out. See my previous answer here.
The Arduino automatically resets when it receives serial communication from most things other than the Arduino IDE. This is why you can send from the IDE but not anything else.
I have an Uno and put a capacitor between Reset and Ground.Here's a page with some good info on the subject.
Good luck. http://arduino.cc/playground/Main/DisablingAutoResetOnSerialConnection
You should probably terminate your string with a 0.
Like:
String text1 = "foobar",0;

Resources