Menu

Serial Programming using C

2009-10-26
2012-09-26
  • Alfredo Perez

    Alfredo Perez - 2009-10-26

    Hello,
    I am trying to program a serial device (Nova Source Synthesizer) via the 9Pin
    com port. I was able to program device using MatLab via the following
    commands:
    Port = serial('Com2');
    set(Port, 'BaudRate', 38400, 'Parity', 'none', 'Terminator', 'CR');
    fprintf(Port, 'FR3810');

    In this example the frequency (FR) of the synthesizer is program for 3810MHz.
    Can someone provide the basic commands to open up a communication port and
    then be able to transmit 'FR3810' using a .c code on a DEV C++ compiler. Many
    Thanks!!!

     
  • cpns

    cpns - 2009-10-28

    You can use pretty much what you have used in MATLAB (although there are more
    elegant but complex methods in Win32):

    FILE* port ;

    port = fopen( "com2", "w+b" ) ;
    system( "mode com2 baud=38400 parity=none") ;
    fprintf( port, "FR3810\r" ) ;

    Note that the port was opened in binary mode and the CR ('\r'), line
    terminator explicitly used in the printf(); that appears to be implied in the
    MATLAB code. If you did not use binary mode and use the more usual '\n', you
    get a CR+LF line termination on Windows, and just LF on Linux, and other
    operating systems may have different conventions. However it is likely that
    your device will accept either convention; I am just trying to emulate your
    MATLAB implementation.

    I made a shell call to set the port configuration, this is a bit quick and
    dirty, but works. The Win32 API has more flexible ways to perform serial I/O
    that will work better in a GUI application, see for an example/tutorial. You
    will see that it is hard work! Microsoft added a much better and more elegant
    interface for serial I/O communications in the , but Dev-C++ does not support
    that; but the frankly far better (and free) VC++ 2008 Express does, although
    you'd have to use C++/CLI, and .Net programming is much cleaner in C# in any
    case. If you are doing a lot of serial I/O and especially if you are
    implementing a GUI interface I'd recommend .Net/Windows forms, which therefore
    means using either VC++, C# or one of the other ,Net languages.

    : http://msdn.microsoft.com/en-
    us/library/ms810467.aspx

    : http://msdn.microsoft.com/en-
    us/library/system.io.ports.serialport.aspx

     
  • cpns

    cpns - 2009-10-28

    Sourceforge's new mark-up implementation really sucks! StackOverflow wuse teh
    same 'mark-down' notation and had managed to make it work!

    Where in the code it says &quote, replace with "

    Clifford

     

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.