Rules | Recent posts | topic RSS | Search | Register  | Log in

Command Line Interface (CLI) using C

 
Post new topic  Reply to topic    EDAboard.com Forum Index -> PC Programming and Interfacing
Author Message
ashishvdeshpande



Joined: 05 Jul 2006
Posts: 27


Post29 Jul 2008 13:01   Command Line Interface (CLI) using C

Hi,

I want to create command Line Interface to microcontroller. Can somebody help me with simple sample code?

Regards,
Ashish
Back to top
jhbbunch



Joined: 21 Feb 2006
Posts: 220
Helped: 16


Post08 Aug 2008 23:03   Re: Command Line Interface (CLI) using C

If you are using a console on Windows this will work:

Code:
#include <stdlib.h>
#include <windows.h>

HANDLE GetSerialPort(char *);

int main(void)
{
    HANDLE h1, h2;
    char h1_buffer[] = ("Hello from Com1:");
    char h2_buffer[24];
    DWORD byteswritten = 0, bytesread = 0;
    char c1[] = {"COM3"};
    char c2[] = {"COM4"};
    h1 = GetSerialPort(c1);
    h2 = GetSerialPort(c2);
   
    WriteFile(h1, h1_buffer, 17, &byteswritten, NULL);
    ReadFile(h2, h2_buffer, strlen(h1_buffer) + 1, &bytesread, NULL);
   
    if (bytesread)
       printf("%s\n", h2_buffer);
    else
        printf("Nothing read\n");   
   
    CloseHandle(h1);
    CloseHandle(h2);

    getch();   
}

HANDLE GetSerialPort(char *p)
{
    HANDLE hSerial;
    hSerial = CreateFile(p,
                         GENERIC_READ | GENERIC_WRITE,
                         0,
                         0,
                         OPEN_EXISTING,
                         FILE_ATTRIBUTE_NORMAL,
                         0);

    DCB dcbSerialParams = {0};
    dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
    dcbSerialParams.BaudRate=CBR_19200;
    dcbSerialParams.ByteSize=8;
    dcbSerialParams.StopBits=ONESTOPBIT;
    dcbSerialParams.Parity=NOPARITY;
    SetCommState(hSerial, &dcbSerialParams);
    return hSerial;
}


I imnprovised this from : http://www.robbayer.com/files/serial-win.pdf

He also shows how to set timeouts and how to do it in an event driven manner. Obviusly this example uses polling, which should be fine.

Obviously you need to check for valid handles and valid returns from the functions.

Com3 and Com4 might seem strange but those are the virtual serial ports I was using.
Back to top
Post new topic  Reply to topic    EDAboard.com Forum Index -> PC Programming and Interfacing
Page 1 of 1 All times are GMT + 1 Hour


Abuse
Administrator
Moderators
topic RSS 
sitemap