Programming Serial Communication Using C to talk to Arduino

Hello!
I have been assigned to program a serial communication to send and receive data from Arduino to PC(windows), using C. I've looked through the Arduino's Serial software websites, but none of them seemed to have a clear explanation or just no explanation at all how to program the serial communication. For the serial communication, I would like to be able to send and receive the data in specific packaged format( ascii, binary, etc..), and be able to decode it.What sort of functions would I need to write and are there any tutorials out there? I can't seem to find any.. Thank you.

I have been assigned to program a serial communication to send and receive data from Arduino to PC(windows), using C.

Why C? Its not the easiest way to do it. C# makes it trivial.

What sort of functions would I need to write

One to open the correct serial port correctly.
One to read data from the serial port.
One to send data to the serial port.
One to close the serial port when you are done.

Thanks for the reply!

Why C? Its not the easiest way to do it. C# makes it trivial.

I'm using C since another device that I'm trying to communicate to is written in C. I thought it might be easier if I tried to use the same language for all. What's the benefit of using C# over C or C++ or any other languages?

I'm using C since another device that I'm trying to communicate to is written in C.

Hardware written in C? Strange...

What's the benefit of using C# over C or C++ or any other languages?

Opening, closing, reading from, and writing to the serial port are as simple as opening, closing, reading from, and writing to a file, because the same base class is used.

Thanks. What sort of format does Arduino send its data in? How would I decode the message? By same base class, wha sort of class, functions should I use?

What sort of format does Arduino send its data in?

Binary or ASCII, depending on which method you use.

How would I decode the message?

With a decoder ring. Just send 2 box tops and a self-addressed stamped envelope to...

By same base class, wha sort of class, functions should I use?

		public static System.IO.Ports.SerialPort port;

Use F1 to get help about the class.

PaulS:

I have been assigned to program a serial communication to send and receive data from Arduino to PC(windows), using C.

Why C? Its not the easiest way to do it. C# makes it trivial.

It's not hard on unix at all, since everything is a file.
C:

int ardfd = fopen("/dev/ttyACM0", "rw");

C++:

ifstream arduino("/dev/ttyACM0");

Then you just read from the file like you would anything else.
It can get a little tricky setting up the serial but you can just copy this line:

    char sttycmd[256] = "stty -F /dev/ttyACM0 cs8 115200 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts");

It's not hard on unix at all, since everything is a file.

All you have to do is install unix for Windows, right? OP did says Windows...

Thanks for the replies! I looked into unix, and it looks much simpler, but the project that I'm working is for a company, and they use windows for all the software quality checks. I thought of using C#, but I have to write a main control program that also talks to another device that's written in C. So I'm not too sure, if I use that many variety of programming languages to achieve this.. Writing serial port program for Windows seems very hard and confusing.

Your question is mis-conceived.

The serial interface is a standard which has been around since the 1950's.

If you want to program a serial connection on the PC, looking at the Arduino documentation is
not the place to start. You need software that will implement a standard serial communication
protocol, from the PC end.

This can be quite difficult to do. It is absurd, in this day and age, that from Java it is effectively
impossible.

It is possible using C, C++, C# or the "Processing" language.