Greetings! I need to interface a C program (compiled and executed in Visual Studio Command prompt) with the Arduino microcontroller kit. The objective is to collect the result obtained in Visual Studio Command prompt and enter the same value to Arduino program. Any small help/reference will do wonders. Thanks in advance!
Define a protocol that both can understand, with some message format that easily be recognized at start/end for reading the message and putting the data into arrays or something on the receiving end.
A protocol is simply an agreed upon process for the exchange of data. You haven't defined what data you want to exchange, or what the Arduino is to do with that data, so its hard to help you define a protocol.
But, suppose you wanted to read and set digital pins. You might send something like "<S,3,1>" to set pin 3 high, or "<S,6,0>" to set pin 6 low. You might send "<R,11>" to read pin 11.
Obviously some "commands" expect that data will be returned. Some do not.
Its just as PaulS says.
For instance, I have a master/slave situation between 2 Arduinos.
The master sends out:
'b'
0x00
0x01
0x02
0x03
The slave sits in a loop:
if (Serial.available() >0){ // anything come in?
syncbyte = Serial.read(); // yes, read the byte that's available
if (syncbyte == 'b'){ // got our first byte
while (Serial.available() <4){
// hang out for 4 more bytes
}
// 4 received, proceed
byte0 = Serial.read();
byte1 = Serial.read();
byte2 = Serial.read();
byte3 = Serial.read();
// set a flag to show a message was received
messageIn = 1;
} // end syncbyte check
}// end serial available check
if (messageIn == 1){
//clear the flag for next pass
messageIn = 0;
// and do whatever you do with the data
}
and of course this could be written to allow the slave to do other stuff while waiting for those 4 bytes,
or if you are using high serial comm's and you know the 5 bytes are coming over quick, maybe just waiting is ok.
Thanks PaulS for the reply. Let me explain the project in a bit more detailed way. I am tracking the position of sun at a particular place over the entire day. The position is tracked by a 'frame-like-structure' which is controlled by stepper motors. Now, the position of the stepper motors at different instants of time is calculated via an algorithm which is developed and compiled in C language (run in Visual Studio Command prompt). The results obtained from this C program must be transmitted serially to the Arduino platform; so that the stepper motor can move according to the order of the C program. And so I am interested to find a mechanism that the results obtained from Visual Studio command prompt should be directly transmitted to the Arduino without any human intervantion.
Thanks CrossRoads for the explaination and sharing the pseudo codes.
The results obtained from this C program must be transmitted serially to the Arduino platform;
So you must arrange for this C program to send the output to the serial port that is connected to the arduino. This will involve a change in this C program so if you have not got the source code you are stuffed.
Unless you are able to get your operating system to switch the stream from the C program from the display it is currently using to the serial port the arduino is using.
The results obtained from this C program must be transmitted serially to the Arduino platform; so that the stepper motor can move according to the order of the C program.
So, you have 3 pieces of data - which stepper should move, how many steps it should move, and which direction it should move in. Seems trivial to develop a protocol with this little variation in the information that needs to be sent.
Thanks Paul. It seems trivial because I am very new to this arduino environment. I am learning!
I intend to develop the communication between the C program and the arduino in the following manner. The C program will generate a .txt file and store its output in the .txt file. Using processing, the values in the .txt file will be read and sent over the serial port. The values sent over serial port by processing needs to be the input of Arduino program. But when I run the two programs: one in Arduino and the other in processing, I get the error message "Port already in use". Is there a way to mitigate this problem? The serial monitor needs to monitor the communication; but at the same time it hinders for both arduino and processing to compile simultaneously.
You can't have the serial monitor and Processing using the same serial port at the same time. Close the serial monitor, and put debugging code into the Processing application.