Serial Communication in C

Im sure someone must have already asked this before, but ive been searching with no success.

Here is what i want to do, not sure if possible. I am in my 1st semester at school and just learned C language. I bought my arduino 4 days ago, and Ive managed to run trough a few LED exercises without a problem. What i want to do next is Communicate with my Arduino UNO from within another language, and i am familiar with C and PHP.

Here is my example code:

#define LEDR 8                  //LED
#define LEDG 10                 //
#define LEDB 12                 // 

int incomingByte = 0;	        // for incoming serial data

void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);           // set up Serial library at 9600 bps
  pinMode(LEDR,OUTPUT);
  pinMode(LEDG,OUTPUT);
  pinMode(LEDB,OUTPUT);
  Serial.print("Hello world! Arduino Loaded.\n");  // prints hello with ending line break 
  Serial.println("Usage: 1 = LED_R, 2 = LED_G, 3 = LED_B");
  Serial.print("The value of incomingByte is " );
  Serial.println(incomingByte, DEC);
}

void loop()      // run over and over again
{
  int R = 49;    //1 = 49 in ASCII
  int G = 50;    //2 = 50    ASCII
  int B = 51;    //3 = 51    ASCII
  
  if (Serial.available() > 0) 
  {
    // read the incoming byte:
    incomingByte = Serial.read();
    Serial.flush();
    
    // print incoming byte:
    Serial.print("I received: ");
    Serial.println(incomingByte, DEC);
  }
    
  if (incomingByte == R)
  {
    Serial.print("Sending LED command.\n");
    digitalWrite(LEDR, HIGH);
    delay(500);
    digitalWrite(LEDR, LOW);
    delay(500);
    incomingByte = 0;
  }
  
  if (incomingByte == G)
  {
    Serial.print("Sending LED command.\n");
    digitalWrite(LEDG, HIGH);
    delay(500);
    digitalWrite(LEDG, LOW);
    delay(500);
    incomingByte = 0;
  }
  
  if (incomingByte == B)
  {
    Serial.print("Sending LED command.\n");
    digitalWrite(LEDB, HIGH);
    delay(500);
    digitalWrite(LEDB, LOW);
    delay(500);
    incomingByte = 0;
  }
  if ((incomingByte != R) && (incomingByte != G) && (incomingByte != B)&& (incomingByte != 0))
  {
    Serial.print("Command not recognized.\n");
    incomingByte = 0;
  }
  
  
}

So this works just fine when i use the serial monitor built in the Arduino IDE, what i want to do is write a program in C to send receive serial information.

NOT sure if it helps but im using COM5.

Any help would be greatly appreciated.

One thing that is sure to get you into trouble is your use of the Serial.flush(); command. There must be a very specific reason to use that command (almost never needed), otherwise it's sure to cause you to lose incoming serial bytes. Your sketch has no control or knowledge of how many bytes may be coming down the line and when. The serial library holds incoming bytes into a memory buffer as they arrive so just use the serial.available command to keep track of if anything is there to be read.

Lefty

retrolefty:
One thing that is sure to get you into trouble is your use of the Serial.flush(); command.

Thanks, i will remove that line of code.

I'll second the suggestion that you not call Serial.flush(), and post this link from the playground:
http://todbot.com/blog/2006/12/06/arduino-serial-c-code-to-talk-to-arduino/

What kind of computer are you using? Windows? Macintosh? Linux?

Daanii:
What kind of computer are you using? Windows? Macintosh? Linux?

Windows 7 64bit.

Here's some C code that uses COM7 in Windows to read from a serial port. As you can see, it's a little complex. I have not found an easier way, though.

#include <Windows.h>
#include <iostream>
#include <MMSystem.h>
#include <stdio.h>
#include "stdafx.h"
using namespace std;



int main()
{
	HANDLE hSerial;										// set up COM7 as serial port for reading from Arduino

	hSerial = CreateFile(_T("COM7"),
						GENERIC_READ | GENERIC_WRITE,
						0,
						0,
						OPEN_EXISTING,
						FILE_ATTRIBUTE_NORMAL,
						0);

	if(hSerial==INVALID_HANDLE_VALUE){
		if(GetLastError()==ERROR_FILE_NOT_FOUND){
			cout<<"serial port does not exist.\n";
		}
	cout<<"some other error occurred.\n";
	}
	DCB dcbSerialParams = {0};

	dcbSerialParams.DCBlength = sizeof(dcbSerialParams);

	if (!GetCommState(hSerial, &dcbSerialParams)) {
		cout<<"error getting state.\n";
	}

	dcbSerialParams.BaudRate = CBR_9600;
	dcbSerialParams.ByteSize = 8;
	dcbSerialParams.StopBits = ONESTOPBIT;
	dcbSerialParams.Parity = NOPARITY;

	if(!SetCommState(hSerial, &dcbSerialParams)){
		cout<<"error setting serial port state.\n";
	}
	COMMTIMEOUTS timeouts={0};
	timeouts.ReadIntervalTimeout=50;
	timeouts.ReadTotalTimeoutConstant=50;
	timeouts.ReadTotalTimeoutMultiplier=10;
	timeouts.WriteTotalTimeoutConstant=50;
	timeouts.WriteTotalTimeoutMultiplier=10;

	if(!SetCommTimeouts(hSerial, &timeouts)){
		cout<<"error setting timeouts.\n";
	}

		DWORD queueNum = 1;
		SetupComm(hSerial, queueNum, queueNum);


	int PotFlag = 0;
	unsigned char ReadBuffer[1];
	while (PotFlag == 0) {
		DWORD BytesRead;
		ReadFile(hSerial, &ReadBuffer, 2, &BytesRead, NULL);	
		char firstByte = ReadBuffer[0];
		int xValue = int(firstByte) * 256 + ReadBuffer[1];
		cout<<"xValue: \t" << xValue <<"\n";
	}
	CloseHandle(hSerial);
}

Daanii:

Thx i will try to use this.