Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Command Line Interface (CLI) using C

Status
Not open for further replies.

ashishvdeshpande

Newbie level 6
Joined
Jul 5, 2006
Messages
13
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,363
Hi,

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

Regards,
Ashish
 

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 : **broken link removed**

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.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top