Thread: C and Arduino

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    7

    C and Arduino

    Hi there,

    I want to send 3 commands via serial (USB) to an Arduino based device. The thing that i want to do is this: I have a program in C that reads a txt file. In that file I write 3 values: 1, 2, 3. I write one value than I delete it and write another.
    My C program should read those values and send a command to Arduino via Serial USB. This command should light a LED.

    Something like this: if I write 1 in the txt file the Arduino will light the first LED, write 2-> second LED will be ON, and so..

    My question is: what should I add to my C program so the program could send this kind of commands?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Well to begin with, what is your host OS and what compiler are you using?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    7
    Right!

    I use windows XP and 7 with visual studio 2010.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Start here CreateFile function (Windows)
    Scroll down to "Communications Resources"
    Start reading.

    You should be able to google some examples with any luck.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Aug 2013
    Posts
    7
    I can't just send the command from the C program?

    This is not what I need

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I can't just send the command from the C program?
    Yes, once you properly establish communications with your device, which is the purpose of the above link.

    This is not what I need
    Then show us exactly what you need. Post the code showing what you've tried.

    Jim

  7. #7
    Registered User
    Join Date
    Aug 2013
    Posts
    7
    Ok, I understand. I am going to post the code soon.

  8. #8
    Registered User
    Join Date
    Aug 2013
    Posts
    7
    Ok, so this is my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
    
       int n;
       FILE * fp;
    
       fp = fopen ("file.txt", "r");
       
       while(1){
       rewind(fp);
       
       fscanf(fp, " %d", &n);
       
       if(n==1)
            printf("led1\n");
    
       if(n==2)
            printf("led2\n");
    
       if(n==3)
            printf("led3\n");
      
    
      // system ("pause");
       };
    
       fclose(fp);
      
       return(0);
    
    
    }
    What I want to do is this: if(n==1) turn ON LED1
    if(n==2) turn ON LED2
    if(n==3) turn ON LED3

    I think that instead turn ON LED1 I have to write a command to tell Arduino: turn ON LED1. This is what I don't know.
    Last edited by bamwels; 08-16-2013 at 02:55 PM.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You will need to open the serial/USB port, configure the post, and write characters to that port, it is not as simple as opening a file. Although it is the same principle. The link you were given is the starting point, the documentation for the call needed to open your port for communication.

    Here is one example of doing basically what your trying to do. Serial Send.

    There are also several books dealing with this issue. Here is one example: The Windows Serial Port Handbook

    And in case you want a Windows or Linux example.,

    And don't forget this is only half the battle. You'll still need to properly setup your Arduino to properly communicate with your program.


    Jim

  10. #10
    Registered User
    Join Date
    Aug 2013
    Posts
    7
    I have made some 'research' and I read the CreateFile function (Windows) and I also find Serial Communication in Windows

    I have a question: everywhere it says that I have to open my COM port.
    Code:
    HANDLE m_hCommPort = ::CreateFile( szPortName,
           GENERIC_READ|GENERIC_WRITE,  // access ( read and write)
           0,                           // (share) 0:cannot share the
                                        // COM port
           0,                           // security  (None)
           OPEN_EXISTING,               // creation : open_existing
           FILE_FLAG_OVERLAPPED,        // we want overlapped operation
           0                            // no templates file for
                                        // COM port...
           );
    This is an example of how I can open the COM port. My question: I have to write this code in cmd? I have no idea of how I can do this, maybe it sounds stupid...

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > I have to write this code in cmd?
    No, you write it in a C or C++ program.

    Surely you don't mean "cmd.exe", the command line interpreter....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Depending on your circumstances you may wish to look into libusb (see windows_backend for example).

    That way you have the advantage that your application will be cross-platform (for the most part) and if/when you switch to Linux or OSX (for example) making your code work should be relatively trivial.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arduino Simulator using orwell dev cpp
    By Paulware in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 05-15-2013, 09:48 PM
  2. help in c communication with arduino
    By kapustelis in forum C Programming
    Replies: 8
    Last Post: 02-06-2013, 02:35 AM
  3. arduino
    By Annonymous in forum Tech Board
    Replies: 7
    Last Post: 05-29-2012, 11:13 PM
  4. Arduino LED Clock
    By qtip2293 in forum C++ Programming
    Replies: 1
    Last Post: 10-11-2011, 02:05 PM
  5. Arduino programming
    By Terese in forum C++ Programming
    Replies: 5
    Last Post: 12-11-2010, 01:03 PM