NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Support Forum » Communication with computer

July 09, 2011
by bhovik
bhovik's Avatar

Hi all, I am new to Programming and microelectronics. Could somebody send me code example how to send data through usb to nerdkit and read from usb for example temperature. Thank you very much.. I think it will be a lot of help for starters.

July 09, 2011
by Rick_S
Rick_S's Avatar

The tempsensor project sends the temperature through the serial connection to your computer.

July 09, 2011
by bhovik
bhovik's Avatar

I am having trouble reading it using USB. I would like to know How to communicate using USB

July 09, 2011
by Rick_S
Rick_S's Avatar

You can communicate through the USB to serial adapter that came with your NerdKit. Direct communications through USB is not something a novice is going to do and would require an extensive programming knowledge.

July 09, 2011
by missle3944
missle3944's Avatar

Just use hyperterminal or putty and point it to your com port

July 09, 2011
by bhovik
bhovik's Avatar

I NEED SIMPLE CODE. like in python but in C or C++ to read from serial port. In Python they have serial package. Does such package exist in C or C++?

July 09, 2011
by Noter
Noter's Avatar

There's not a lot of simple about using the serial port from C or C++ under windows ... here's some functions to make it easier:

DCB dcb;
HANDLE hCom;
COMMTIMEOUTS cTimeout;
BOOL fSuccess;
TCHAR *pcCommPort = TEXT("COM1"); //  Most systems have a COM1 port

void PrintCommState(DCB dcb, char *port){
    //  Print some of the DCB structure values
    printf("\nPort = %s, BaudRate = %d, ByteSize = %d, Parity = %d, StopBits = %d\n", 
        port,
        dcb.BaudRate, 
        dcb.ByteSize, 
        dcb.Parity,
        dcb.StopBits
        );
}

int OpenCommPort(char *port, char *baud){
  //  Open a handle to the specified com port.
   hCom = CreateFile((LPCTSTR) port,
                    GENERIC_READ | GENERIC_WRITE,
                    0,    //  must be opened with exclusive-access
                    NULL, //  default security attributes
                    OPEN_EXISTING, //  must use OPEN_EXISTING
                    0,    //  not overlapped I/O
                    NULL  //  hTemplate must be NULL for comm devices
                    );

   if (hCom == INVALID_HANDLE_VALUE) 
   {
       //  Handle the error.
       printf ("CreateFile failed with error %d.\n", GetLastError());
       return (1);
   }

   //  Initialize the DCB structure.
   ZeroMemory(&dcb, sizeof(DCB));
   dcb.DCBlength = sizeof(DCB);

   //  Build on the current configuration by first retrieving all current
   //  settings.
   fSuccess = GetCommState(hCom, &dcb);

   if (!fSuccess) 
   {
      //  Handle the error.
      printf ("GetCommState failed with error %d.\n", GetLastError());
      return (2);
   }

   //PrintCommState(dcb);       //  Output to console

   //  Fill in some DCB values and set the com state: 
   //  57,600 bps, 8 data bits, no parity, and 1 stop bit.
   dcb.BaudRate = atol(baud);     //  baud rate
   dcb.ByteSize = 8;             //  data size, xmit and rcv
   dcb.Parity   = NOPARITY;      //  parity bit
   dcb.StopBits = ONESTOPBIT;    //  stop bit

   dcb.fBinary=true;
   dcb.fNull=false;
   dcb.fTXContinueOnXoff=false;
   dcb.fRtsControl=RTS_CONTROL_DISABLE;
   dcb.fDsrSensitivity=false;
   dcb.fDtrControl=DTR_CONTROL_DISABLE;
   dcb.fOutX=false;
   dcb.fInX=false;
   dcb.fOutxCtsFlow=false;
   dcb.fOutxDsrFlow=false;

   fSuccess = SetCommState(hCom, &dcb);

   if (!fSuccess) 
   {
      //  Handle the error.
      printf ("SetCommState failed with error %d.\n", GetLastError());
      return (3);
   }

   //  Get the comm config again.
   fSuccess = GetCommState(hCom, &dcb);

   if (!fSuccess) 
   {
      //  Handle the error.
      printf ("GetCommState failed with error %d.\n", GetLastError());
      return (2);
   }

   PrintCommState(dcb,port);       //  Output to console

   //printf ("Serial port %s successfully reconfigured.\n", port);

    return 0;
}

int SetCommTimeout(int timeout_ms){
    cTimeout.ReadIntervalTimeout = MAXDWORD;
    cTimeout.ReadTotalTimeoutConstant = timeout_ms;
    cTimeout.ReadTotalTimeoutMultiplier = MAXDWORD;
    cTimeout.WriteTotalTimeoutConstant = 0;
    cTimeout.WriteTotalTimeoutMultiplier = 0;
    fSuccess=SetCommTimeouts(hCom, &cTimeout);
    if (!fSuccess){
        printf ("SetCommTimeouts failed with error %d.\n", GetLastError());
        return (1);
    }
    return 0;
}

int CloseCommPort(){
    fSuccess=CloseHandle(hCom);
    if (!fSuccess){
        printf ("CloseHandle failed with error %d.\n", GetLastError());
        return (1);
    }
    return 0;
}

int PutCommChar(UCHAR chrOut){
    unsigned long bytesWritten;
    fSuccess=WriteFile(hCom,&chrOut,1,&bytesWritten,0);
    if (bytesWritten!=1){
        printf ("WriteFile wrote %d bytes.\n", bytesWritten);
        return (2);
    }
    if (!fSuccess){
        printf ("WriteFile failed with error %d.\n", GetLastError());
        return (1);
    }
//  printf(">>%02X\n",chrOut);
    return 0;
}

int GetCommChar(UCHAR *chrIn){
    unsigned long bytesRead;
    fSuccess=ReadFile(hCom,chrIn,1,&bytesRead,0);
    if (!fSuccess){
        printf ("ReadFile failed with error %d.\n", GetLastError());
        return (1);
    }
    if (bytesRead!=1){
        printf ("timeout on read port %d\n",bytesRead);
        return (2);
    }
//  printf("<<%02X\n",*chrIn);
    return 0;
}
July 09, 2011
by bhovik
bhovik's Avatar

Thank You very much. I will try..

Post a Reply

Please log in to post a reply.

Did you know that essentially all power supplies' voltages drop when current is drawn from them? Learn more...