Click here to Skip to main content
15,881,763 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello Everybody,

I am doing a college work. I have to make a programm that receives serial data from a Arduino and plots it, i am using the win32 api. I am having a problem in receiving this data. It just writes randon numbers.


Here is the program:
C++
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>
#define MAX 100


int main()
{
  
  
   DCB dcb;
   HANDLE hCom;
   BOOL fSuccess;

   char pcCommPort[50] = "COM4";

   hCom = CreateFile( pcCommPort,
                    GENERIC_READ | GENERIC_WRITE,
                    0,    // must be opened with exclusive-access
                    NULL, // no 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 %lu.\n", GetLastError());
       return (1);
   }

   // Build on the current configuration, and skip setting the size
   // of the input and output buffers with SetupComm.

   fSuccess = GetCommState(hCom, &dcb);

   if (!fSuccess)
   {
      // Handle the error.
      return (2);
   }

  

   dcb.BaudRate = CBR_9600;     // set the baud rate
   dcb.ByteSize = 8;             // data size, xmit, and rcv
   dcb.Parity = ODDPARITY;        // no parity bit
   dcb.StopBits = ONESTOPBIT;    // one stop bit

   fSuccess = SetCommState(hCom, &dcb);

   if (!fSuccess)
   {
      // Handle the error.

      return (3);
   }
pcCommPort);

 

long int x=0;
int tx;
DWORD num;
char in[1000];
    tx = ReadFile(hCom,&in,strlen(in),&num,NULL);
    if (tx == TRUE) {
          //  printf("SUCCESSFULL READ\n");
            }
        else{
            //printf("UNSUCCESSFULL READ");
        }

    //printf("NUMBER OF BYTES READ: \t%d\n",&num);
    for (x=0;x<1000;x++){
    printf("num = %d\n",in[x]);
    }

    CloseHandle(hCom);

return 0;
    }



Can anybody help me?
Posted
Updated 20-Mar-15 1:14am
v3

1 solution

You are not checking how much input you received, but printing 1000 numbers each time.
and if the input is in characters you should not be printing the values as numbers. Try changing to:
C++
printf("NUMBER OF BYTES READ: \t%d\n", num);
for (x=0; x < num; x++)
{
    printf("in[%d] = %c\n", x, in[x]);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900