#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Help with serial port in C


    Hello, can someone help me with very basic and simple example of how to:
    - open serial port
    - write to serial (just to send a simple integer "1")
    - close serial port.
    written in C.

    I tried this code:
    i try this Code:
    #include<stdio.h>
    #include<stdlib.h>
    void main()
    {
        FILE* port;
        system( "MODE COM7: BAUD=9600 PARITY=n DATA=8 STOP=1" ) ;
        port = fopen( "COM7:", "wb" ) ;
        printf("1");
        fprintf( port, "1" ) ;
        fclose( port ) ;
    }


    It runs, but the command prompt says 'device in COM7 is not available'. I double checked device manager; my device is connected to COM7 and no other devices connected to COM7.

    Perhaps there are something wrong in the code? Kindly help me.

    Thanks in advance.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,173
    Rep Power
    2222
    Using the serial port is not part of the language standard, so how to do it depends entirely on what operating system you're on.

    What operating system is this for? It appears to be MS-DOS. What compiler are you using?

    Your problem appears be due to the shell game you're playing. A shell is a user interface to the operating system. Each shell has an environment that it operates within and that environment keeps track of such things as what the current working directory is, what directories to search in when you want to run a program (PATH), what mode a serial port has been set up with. When you create a new shell, it inherits its environment from its parent shell by creating its own copy of the parent's environment. That means that any changes that are made to the environment of the child shell, AKA "sub-shell", only have any effect in that sub-shell and in any sub-shells that it may create; the parent shell's environment remains unchanged.

    Your program is running in its shell. When you call system(), you create a sub-shell. So then your
    system( "MODE COM7: BAUD=9600 PARITY=n DATA=8 STOP=1" ) ;
    creates a sub-shell, changes that environment's sub-shell with the MODE command, and then when you return from system() that sub-shell is terminated, its new environment is destroyed along with the changes you made, and your program's own environment knows nothing about any serial port set-up.

    You can try the same experiment by using system to change the current working directory. When you return from system() you will find that the current working directory has not changed.

    How you would set up COM7 from within your program, I don't know. In the past I've used different methods and, besides, it depends very heavily on your OS and on what your compiler provides you to use. So we would need to know what your OS and compiler are in order to offer any real help.

    PS
    If what I said about shells didn't make sense, you should read up on the subject; eg on Wikipedia at http://en.wikipedia.org/wiki/Shell_(computing).
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0
    Thank you for replying.
    Originally Posted by dwise1_aol
    Using the serial port is not part of the language standard, so how to do it depends entirely on what operating system you're on.
    I see.. my understanding is all wrong. I thought communication through serial port can be done directly from language standard.

    Originally Posted by dwise1_aol
    So we would need to know what your OS and compiler are in order to offer any real help.
    I am using: 32-bit Windows 7 and the compiler is Visual Studio Professional 2012.

    thnks again, my apologies for bugging you with basic question..
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    You can get a long way if you just use google, type "win32 c++" as the common prefix to your question, then add keywords for your specific question, say serial port
    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
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by salem
    You can get a long way if you just use google, type "win32 c++" as the common prefix to your question, then add keywords for your specific question
    thanks for the reply.
    I tried google but, i mean to write the code in C.
    Most examples in google are written in C++ or C#. Once there is an example written in C, the code is very complicated..
  10. #6
  11. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,112
    Rep Power
    1803
    Originally Posted by dwise1_aol
    When you call system(), you create a sub-shell. So then your
    system( "MODE COM7: BAUD=9600 PARITY=n DATA=8 STOP=1" ) ;
    creates a sub-shell, changes that environment's sub-shell with the MODE command, and then when you return from system() that sub-shell is terminated, its new environment is destroyed along with the changes you made, and your program's own environment knows nothing about any serial port set-up..
    The result of the mode command is in fact persistent. This form of the mode command is specific to the Windows command shell not MS-DOS.
  12. #7
  13. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    > I tried google but, i mean to write the code in C.
    > Most examples in google are written in C++ or C#. Once there is an example written in C, the code is very complicated..
    The win32 API is a C API to begin with.

    Most of the platform functions you can call have the same name, whether you call it from C, C++ or C# (or VB or whatever).

    You should definitely bookmark msdn.microsoft.com and learn to navigate the huge amount of information contained therein.
    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

IMN logo majestic logo threadwatch logo seochat tools logo