I have a few questions about communicating with the Arduino via Serial.
-Is the "file-writing" method the fastest or most streamlined method for communicating with the Arduino in C++. It seems like there would be a faster way.
-Are there reserved parts of the incoming data? I know the Arduino grabs bytes and puts them into a buffer that you then read. But are all these bytes fair game, or are some of them reserved for some sort of overhead?
-Does this data perform any particular functions for the arduino? I know the ASCII spec includes things like "start packet" and "end packet" and various other commands. Are these commands used by the arduino, if so, are they excluded from the buffer?
The focus of these questions is this: I'm trying to create a binary-level language for communicating with and controlling the arduino. Something like machine code.
For example: AF F0 08 00
10101111 11110000 00001000 00000000
1010 could mean to change RGB values for a light. This command looks for 3 bytes immediately afterwards:
11111111 Red = 255
00000000 Green = 0
10000000 Blue = 128
And then 4 remaining 0's to finish of the last byte. or perhaps have a closing code.
-Is the "file-writing" method the fastest or most streamlined method for communicating with the Arduino in C++. It seems like there would be a faster way.
Define what "file-writing" method is! I communicate a lot with my Arduino but never using files.
-Are there reserved parts of the incoming data? I know the Arduino grabs bytes and puts them into a buffer that you then read. But are all these bytes fair game, or are some of them reserved for some sort of overhead?
I guess your talking about a serial communication. You can use all bytes transferred for your own purpose, no bytes are reserved.
It's been a while since I have used Arduino, since I started work, but I believe the default method of communicating with the Arduino in C++ is posted on this site.
It was something along the lines of "CreateFile("///.//COM10")" Which creates a "file". And then from there, you read from and write to this "file". To me, it seemed a little hackish.
But nevermind that. I would like to know how YOU communicate with the arduino.
But nevermind that. I would like to know how YOU communicate with the arduino.
Simple delimited serial code below. The arduino only reads a single byte at a time from the hardware serial input buffer. You have to collect them into a c-string or String to evaluate the bytes as a whole. When some applications on a pc do "file-writing", they open the serial port, send the data, then close the serial port. Opening/closing the virtual serial port established by the arduino serial driver, causes the arduino to reset and data corruption issues. this can be defeated by adding a resistor or capacitor to the arduino.
//zoomkat 3-5-12 simple delimited ',' string parse
//from serial port input (via serial monitor)
//and print result out serial port
//send on, or off, from the serial monitor to operate LED
int ledPin = 13;
String readString;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
Serial.println("serial LED on/off test with , delimiter"); // so I can keep track
}
void loop() {
if (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == ',') {
Serial.println(readString); //prints string to serial port out
//do stuff with the captured readString
if(readString.indexOf("on") >=0)
{
digitalWrite(ledPin, HIGH);
Serial.println("LED ON");
}
if(readString.indexOf("off") >=0)
{
digitalWrite(ledPin, LOW);
Serial.println("LED OFF");
}
readString=""; //clears variable for new input
}
else {
readString += c; //makes the string readString
}
}
}
Please provide a link to the library offering the CreateFile() command on the Arduino. It's definitely not the default way to communicate with the Arduino.
Because that call includes "COM10" I get the impression we don't talk about programming the Arduino but about programming your Windows machine. You should state that explicitly because we here expect that you're talking about programming the Arduino if you don't explicitly state otherwise.
I guess you better ask your question in a Visual C++ forum, because that's how to program a serial interface in Windows and have nothing to do with Arduino (except that Arduinos sometimes communicate with a PC by a serial connection). Arduinos may also have an Ethernet shield but questions about how to change the MTU on a Windows machine are not appropriate in an Arduino forum.