Serial interfacing with C Programming

I'm trying to interface an Arduino with a program on a computer, which I am writing in C programming. I am using the resource below, but I'm wondering whether its too complicated.

http://www.robbayer.com/files/serial-win.pdf

I've also checked the Interfacing with C section on Arduino playground, but that file has quite a few errors.

Does anyone know an easy to implement method to communicate and handshake the Arduino with the Computer using C programming?

I'm trying to interface an Arduino with a program on a computer, which I am writing in C programming.

This implies that you are having less than 100% success. But, you don't show any code, or explain what problems you are having.

I am using the resource below, but I'm wondering whether its too complicated.

No.

Does anyone know an easy to implement method to communicate and handshake the Arduino with the Computer using C programming?

No such thing. Now, C#, on the other hand, makes it very easy. But, you have to be using Windows to use C#.

If you are having problems, tell us about them instead of complaining about how hard it is.

PaulS:

I'm trying to interface an Arduino with a program on a computer, which I am writing in C programming.

This implies that you are having less than 100% success. But, you don't show any code, or explain what problems you are having.

I am using the resource below, but I'm wondering whether its too complicated.

No.

Does anyone know an easy to implement method to communicate and handshake the Arduino with the Computer using C programming?

No such thing. Now, C#, on the other hand, makes it very easy. But, you have to be using Windows to use C#.

If you are having problems, tell us about them instead of complaining about how hard it is.

The code I used is in the link I attached. If you say it is not complicated, I will use that then.

My problem is that though I try to send data through, the Arduino doesnt get it.

The only reason I posted this was to see if there were any libraries out there which would make it easier.

Thanks for helping out and answering my questions.

My problem is that though I try to send data through, the Arduino doesnt get it.

Post the Arduino code, the C code, and an explain of the connection - what COM port, what applications are open, etc., and we'll help you with it.

I've often thought that the "Arduino didn't get it", when it turned out to be my code that "didn't get it".

Here is my Serial code which runs on Windows, written in C.

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

int main()
{
    HANDLE SerialHandle;

    DCB dcbSerialParams = {0};

    COMMTIMEOUTS timeouts = {0};

    char serialBuffer = 123;

    DWORD dwBytesRead = 0;

    //int n = 0;

    SerialHandle = CreateFile("COM3",GENERIC_WRITE,0,0,OPEN_EXISTING,
                              FILE_ATTRIBUTE_NORMAL,0);

    if (SerialHandle == INVALID_HANDLE_VALUE)
    {
        if(GetLastError() == ERROR_FILE_NOT_FOUND)
        {
            printf("Serial port doesnt exist\n");
        }
        printf("Some other error");
    }

    dcbSerialParams.DCBlength = sizeof(dcbSerialParams);

    if(!GetCommState(SerialHandle, &dcbSerialParams))
    {
        printf("Error getting state\n");
    }

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

    if (!SetCommState(SerialHandle,&dcbSerialParams))
    {
        printf("Error getting serial port state\n");
    }

    timeouts.ReadIntervalTimeout = 50;
    timeouts.ReadTotalTimeoutConstant = 50;
    timeouts.ReadTotalTimeoutMultiplier = 10;
    timeouts.WriteTotalTimeoutConstant = 50;
    timeouts.WriteTotalTimeoutMultiplier = 10;

    if (!SetCommTimeouts(SerialHandle, &timeouts))
    {

        printf("Error Occured setting timeouts\n");
    }

    while(1)
    {
        if(!WriteFile(SerialHandle,serialBuffer,1,&dwBytesRead,NULL))
        {
            printf("Write error occured\n");
        }
    }

    return 0;

}

When this code is compiled, I get a few warnings. They are shown below.

E:\Programming\serialcom\main.c||In function 'main':expressionless:
E:\Programming\serialcom\main.c|62|warning: passing argument 2 of 'WriteFile' makes pointer from integer without a cast|
c:\program files (x86)\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1........\include\winbase.h|2033|note: expected 'PCVOID' but argument is of type 'char'|
||=== Build finished: 0 errors, 1 warnings ===|

And this is my Arduino Code.

char serialData;

void setup()
{
  Serial.begin(9600);
  Serial.println("Serial test starting");
}

void loop()
{
  if (Serial.available() > 0)
  {
    serialData = Serial.read();
    Serial.print("This is what you entered: ");
    Serial.println(serialData);
  }
}

When I run both applications, these are my observations.

  1. Regardless of whether the Arduino Serial Monitor is open or not, I always get a "Write Error" i.e. the windows app couldnt write to the serial port.
  2. Regardless of which COM port I enter, (Arduino was on COM3) I entered COM 6, 8 etc. It still gave the "Write Error" which means the windows app didnt check to see if it was an existing port or not.
  3. As I run my windows serial app, the "L" (activity?) LED and "TX" LED on the Arduino blink for a few miliseconds and then dont blink after that. (this happens regardless of whether the serial monitor is open or not.

Your C application is trying to open a serial port to talk to the Arduino, in write-only mode. Any other application that tries to use the same serial port must either be closed or assured to not have the serial port open. That means that the Serial Monitor MUST be closed, and the IDE can not be trying to upload code to the Arduino at the same time.

You do not show the actual output that you get from the program, but you do mention a "Write Error". So, I'll assume, for now, that the C application was successful at opening the serial port.

You really should show EXACT output, where possible, rather than waving your arms so much.

Now, operating on the assumption that the C application did open the serial port, this causes the Arduino to reset, unless you have modified the Arduino to not reset on serial connections. You haven't mentioned doing this, so I'll assume that you haven't. Notice that this is now the second assumption I've had to make.

You then begin flooding the Arduino with input. Or, you try to.

Section 4 of the document you linked to shows how to create an array to contain data read from the Arduino. It mentions that writing is exactly the same, except for using WriteFile instead of ReadFile. That is, the arguments to both functions is exactly the same. The difference is that one function will read from the array and write to the serial port (WriteFile) while the other will read from the serial port and write to the array (ReadFile).

Your call to WriteFile does not include an array as one of the arguments. The compiler warned you that this was likely wrong, but you chose to ignore the warning. When you supply an argument to a C function, that argument is passed to the function either by-value or by-reference. Simple types are passed by value. Complex types are passed by reference. Arrays are complex types, so they are passed by reference.

Now, what this means is this. The 3rd argument is the size of the array. You supply a 1, which the compiler makes a copy of, in the call to the function. This is pass-by-value. The actual value is copied.

The 2nd argument, on the other hand, is an array of data to write. Since the argument is a complex type, it is passed be reference. This means that what you are supplying is the address where the data is. If you create an array, and put some data in that array, and then tell the function where the array is, in memory, it can get the data. This is a pass-by-reference. You are simply telling the function where the data is, rather than copying the data for the function to use.

Now, you told the compiler that the data to pass was at address 123. Do you think you have read access to data at that address? No, of course you don't. Low addresses like that are for the OS to use, not your application.

Now, if you follow directions, and create an array, instead, with some data in it, the address of the array will be used (automatically).

But, you will still have a problem. Whatever data is in that array will be written to the Arduino over and over, with no delay. This will very quickly swamp the Arduino's serial buffer, and data will soon be lost.

The Arduino reads one character from the serial buffer, after it finishes booting. For every character read, it writes 29 characters back to the serial port ("This is what you entered: ", the character read, a carriage return, and a line feed).

The C application is not configured to read from the serial port, and there is no code to do so, so why you are writing anything to the serial port is a mystery.