im trying my hand at reading data via serial communications on a windows 32bit computer. ive accomplished this, extremely crudely and have to some degree been successful. the device i am trying to read sends data in the format $A.B.C$ followed by a '\n' or newline. using the code written below, the data is read correctly most of the time but is sometimes followed by a sequence like this:

$A.B.C$
$A.B.C$
$A.B.C$
$A.B.
C$
$
A.B.C$

basically it reads data pretty weirdly. any suggestions are accepted.thank you!

#include <windows.h>
#include <stdio.h>
#include <conio.h>

int main (void)
{

    int n = 25;
    char szBuff[25 + 1] = {0};
    
    HANDLE hSerial;
    DCB dcbSerialParams = {0};
    COMMTIMEOUTS timeouts={0};
    DWORD dwBytesRead =25;

    dcbSerialParams.DCBlength=sizeof(DCB);

    hSerial = CreateFile("COM4",
                     GENERIC_READ | GENERIC_WRITE,
                     0,
                     0,
                     OPEN_EXISTING,
                     FILE_ATTRIBUTE_NORMAL,
                     0);


    if(hSerial==INVALID_HANDLE_VALUE)
    {
      if(GetLastError()==ERROR_FILE_NOT_FOUND)
      {
        puts ("cannot open port!");
        return;
      }
      
      puts ("invalid handle value!");     
      return;                            
    }

    if (!GetCommState(hSerial, &dcbSerialParams)) 
    {
      puts ("error getting state");
      return;
    }
        
    dcbSerialParams.BaudRate=CBR_57600;
    dcbSerialParams.ByteSize=8;
    dcbSerialParams.StopBits=ONESTOPBIT;
    dcbSerialParams.Parity=NOPARITY;
   
   if(!SetCommState(hSerial, &dcbSerialParams))
   {
      puts ("error setting port state");
      return;
   }
        
   
   
   while (1){      
   if(!ReadFile(hSerial, szBuff, n, &dwBytesRead, NULL)){
      puts ("serial read error fail!");
      return;
   } 
   
   else
   {
      printf ("%s\n" , szBuff);
   }
   }
    
    
   getch();     
   return 0;
   
}

Recommended Answers

All 3 Replies

Ok, what have you tried to troubleshoot this?

Have you tried sending the data at slower speed? That would be my first test.

ive tried reading the data at slower speeds (diff baud rate right?) and i get trash. ive tried changing buffer sizes too. both failed. T_T

Did you try SENDING the data at slower speed? ;)

I want you to brush up on your Win32 serial communications, here:
http://msdn.microsoft.com/en-us/library/ms810467

and be sure to download the sample program that is linked right below the top paragraphs.

Run that sample, and see if it runs OK. What I want to confirm is that:

1) your hardware is OK. If you're going from one computer's serial port to another's, you need a cross over cable - not a straight through serial cable.

2) A known good program, will run on your system, correctly.

If #1 and #2 are OK, then we know that the system and the cable/serial port, are working properly, and start narrowing down the suspects in your program.

Unfortunately, serial communication has changed hugely since DOS days, and while some serial programs will still work, others simply will not.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.