Menu

Serial Port

2006-03-11
2012-09-26
  • Nobody/Anonymous

    Hello, I'm a beginner with english and C programing, I'm making a program to control a servo-sonnar, I need comunicate with the servo-sonnar using the serial port (Com 1), in linux it's easy, but in windows I haven't an idea about using Windows Api's and adding libraries to Dev-C++, can anybody help me send a example code's? Thank you very much.

     
    • Anonymous

      Anonymous - 2006-03-11

      You can access serial ports through stdio, iostream, or Win32 file I/O. You simply open the 'file' on the device name ("COM1" in this instance).

      If you need to control baud rate and frame format, then you can use the Win32 API, it is somewhat cumbersome, so the quick and dirty method is to invoke the MODE command via a system() call.

      e.g.

      FILE* portfp ;

      system( "mode com1: baud=115200 parity=0 data=8 stop=1 to=off xon=off"
      portfp = fopen( "com1", "wb+" ) ;
      fprintf( portfp, "Hello, world!\n" ) ;
      fclose( portfp ) ;

      In most cases this is adequate, more complex serial I/O can be achieved through the Win32 API, but it is not very straightforward. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnfiles/html/msdn_serial.asp. There are a number of third party libraries to simplify the process.

      Clifford

       
    • Nobody/Anonymous

      Thank you very much Clifford!!! The code runs ok!!! :D

      Note:
      Put ); before the system line -->

      system( "mode com1: baud=115200 parity=0 data=8 stop=1 to=off xon=off" );

       
      • Anonymous

        Anonymous - 2006-03-11

        ...oops.

        BTW, I meant to ask what is a servo-sonnar? Did you mean a servo-motor?

        Clifford

         
    • Nobody/Anonymous

      No, jejeje, "Servo-Sonnar" is a ultrasonic distance sensor mounted on a servo-motor.

       
      • Anonymous

        Anonymous - 2006-03-12

        Ahh... servo-sonar

        Clifford

         
      • Nobody/Anonymous

        The code runs ok for send commands, because for read Com1 don't work right. I need the program do this:
        Send Command to serial port --> Wait for ack from serial port --> Send second command

        I wait the ack (character A) with:

        while(ok!="A"){
        ok=fgetc(portfp);
        }

        But don't work, any ideas?

         
    • Nobody/Anonymous

      OP, your English is very good! I hope that if I pick up a second language I do as well as you.

       
    • Anonymous

      Anonymous - 2006-03-13

      "A" is character string constant (of type const char*), wheras fgetc() returns an int; so you are comparing an int with a pointer. I would be surprised if the compiler did not at least issue a warning. You need to compare ok against the character constant 'A'.

      while(ok!='A'){

      Clifford

       
      • Nobody/Anonymous

        ""A" is character string constant (of type const char*)"

        funny, i thought "A" was of type "const char[2]."

        snork

        include <iostream>

        int main(){std::cout << typeid("A").name();}

         
        • Nobody/Anonymous

          O_o

          my smile was left out. ^_~

          snork

           
        • Anonymous

          Anonymous - 2006-03-14

          Pedant ;) - I can play this game:

          The code:

          include <stdlib.h>

          int main()
          {
          int ok = "A" ;
          if( ok == "A" ){}
          }

          When compiled as C++ gives the error:
          main.cpp:5: error: invalid conversion from const char*' toint'
          main.cpp:6: error: ISO C++ forbids comparison between pointer and integer

          And in C:
          ctest.c:5: warning: initialization makes integer from pointer without a cast
          ctest.c:6: warning: comparison between pointer and integer

          I might suppose that when a char[] is an operand of == or = it degrades to a char* just as it does when you pass it to a function - i.e. it looses its 'sizeof' information. Arrays in C/C++ are fragile things, and will degrade thus at the drop of a that!

          So that while "A" is of type const char[2], (indeed sizeof("A") == 2, where sizeof(const char) == 4), when the compiler gets to assign or compare it, it is const char. As evidenced by the failure of code such as:

          char Astring[2] ;
          Astring = "A" ;

          wheras

          char Astring[2] = "A" ;

          is fine (initialization being a different matter than assignment).

          But you of course knew all that already, and you are of course correct, but that was what I meant (which you also knew). But someone may be interested.

          Still as demonstarted by the code above, the OP should have got at least warning. I hope he is not one of those people who ignore warnings with a cavalier "oh, that's only a warning"! But at the same time I hope he is not one of those who types code directly into the forum, rather than cutting & pasting the actual code. After all that I will be really annoyed if he had 'A' all along!

          Clifford

           
          • Nobody/Anonymous

            you may be surprised at how quickly an array can degrade. i know i was. when i first wrote this--in real code--i had expected it to work--and portably.

            "typeof" as written is a gcc extension but i have support for it with templates. it would make the code to complicated to show such a simple example.

            to save you the trouble.

            Array
            Pointer
            Pointer
            Pointer

            snork

            include <iostream>

            template <typename T> struct foo;
            template <typename T> struct foo<T *>
            {
            operator const char * ()
            {
            return("Pointer");
            };
            };
            template <typename T, unsigned int S> struct foo<T[S]>
            {
            operator const char * ()
            {
            return("Array");
            };
            };

            template <typename T> const char * bar(T *)
            {
            return("Pointer");
            }

            template <typename T, unsigned int S> const char * bar(T[S])
            {
            return("Array");
            }

            int main()
            {
            char array[2];
            char * pointer;
            std::cout << foo<typeof(array)>() << '\n';
            std::cout << foo<typeof(pointer)>() << '\n';
            std::cout << bar(array) << '\n';
            std::cout << bar(pointer) << '\n';
            return(0);
            }

             

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.