JQadrad 0 Newbie Poster

Hey all,

I am working on a project with an FPGA but I need serial communication between the FPGA and the desktop since the memory of the FPGA is limited and I need to be able to send longer inputs to the FPGA by using buffers on the desktop.

After hours of browsing and reading, I still can't figure out why my code isn't working. I saw similar code snippets on different websites, but I can't seem to get the serial communication to work. The writing part is working (probably), but the reading goes wrong. Every 'real character' is followed by three null terminators. Writing a string of 19 characters works and the FPGA I am using gives the correct data on the display. The FPGA should send the input back to the serial port. In the Hyperterminal this is working without any problem, but using C or the Bray Terminal it is not working. I can see the 3 null terminators in the Bray Terminal too. I guess HyperTerminal deletes those characters?

Can someone maybe point me on my mistake and tell me what I am doing wrong?

Thanks in advance =)

My current code:

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include    <commdlg.h>
//#include  <windef.h>
#define    BUFFERLENGTH 19

void writeToSerial(char *line, HANDLE hSerial, DWORD dwBytesWritten);
void printBuffer(char * buffRead, DWORD dwBytesRead);


int main(){
    HANDLE hSerial;
    COMMTIMEOUTS timeouts;
    COMMCONFIG dcbSerialParams;
    char line[BUFFERLENGTH];
    char buffWrite[BUFFERLENGTH];
    char buffRead[BUFFERLENGTH+1];
    DWORD dwBytesWritten, dwBytesRead;
    /* Create a handle to the serial port */    
    hSerial = CreateFile("COM3",
                            GENERIC_READ | GENERIC_WRITE,
                            0,
                            NULL,
                            OPEN_EXISTING,
                            FILE_ATTRIBUTE_NORMAL,
                            NULL);
    /* Check if the handle is valid */                        
    if(hSerial == INVALID_HANDLE_VALUE){
       if(GetLastError() == ERROR_FILE_NOT_FOUND){
          printf("Serial port does not exist \n");
       }else{
          printf("Port occupied. Please close terminals!\n");
       }
    }else{
       printf("Handle created\n");    
       /* Check the state of the comm port */
       if(!GetCommState(hSerial, &dcbSerialParams.dcb)){
          printf("Error getting state \n");
       }else{
          printf("Port available\n"); 
          /* Configure the settings of the port */
          dcbSerialParams.dcb.DCBlength = sizeof(dcbSerialParams.dcb);
          /* Basic settings */    
          dcbSerialParams.dcb.BaudRate = CBR_57600;
          dcbSerialParams.dcb.ByteSize = 8;
          dcbSerialParams.dcb.StopBits = ONESTOPBIT;
          dcbSerialParams.dcb.Parity = NOPARITY;
          /* Misc settings */
          dcbSerialParams.dcb.fBinary = TRUE;
          dcbSerialParams.dcb.fDtrControl = DTR_CONTROL_DISABLE;
          dcbSerialParams.dcb.fRtsControl = RTS_CONTROL_DISABLE;
          dcbSerialParams.dcb.fOutxCtsFlow = FALSE;
          dcbSerialParams.dcb.fOutxDsrFlow = FALSE;
          dcbSerialParams.dcb.fDsrSensitivity= FALSE;
          dcbSerialParams.dcb.fAbortOnError = TRUE;
          /* Apply the settings */
          if(!SetCommState(hSerial, &dcbSerialParams.dcb)){
            printf("Error setting serial port state \n");
          }else{
            printf("Settings applied\n");  
            GetCommTimeouts(hSerial,&timeouts);
              //COMMTIMEOUTS timeouts = {0};
            timeouts.ReadIntervalTimeout = 50;
            timeouts.ReadTotalTimeoutConstant = 50;
            timeouts.ReadTotalTimeoutMultiplier = 10;
            timeouts.WriteTotalTimeoutConstant = 50;
            timeouts.WriteTotalTimeoutMultiplier= 10;

            if(!SetCommTimeouts(hSerial, &timeouts)){
                  printf("Error setting port state \n");
            }else{
                /* Ready for communication */
                strcpy(line,"Something else\r   ");
                //****************Write Operation*********************//
                writeToSerial(line, hSerial, dwBytesWritten);
                //***************Read Operation******************//
                if(ReadFile(hSerial, buffRead, 4*(BUFFERLENGTH-1), &dwBytesRead, NULL)){
                   printBuffer(buffRead, dwBytesRead);                     
                }
             }          
          }
       }
    }
    CloseHandle(hSerial);
    system("PAUSE");
    return 0;

}
void printBuffer(char * buffRead, DWORD dwBytesRead){
     int j, k = 0;
     char outputLine[BUFFERLENGTH];
     for(j = 0; j <  4*(BUFFERLENGTH-1); j++){
         if(buffRead[j] != '\0' && buffRead[j] != 13){    
             outputLine[k] = buffRead[j];
             k++;
         }
     }   
     printf("Output: '%s'\n", outputLine);
}

void writeToSerial(char *line, HANDLE hSerial, DWORD dwBytesWritten){
    WriteFile(hSerial, line, BUFFERLENGTH, &dwBytesWritten,NULL);
    if(dwBytesWritten){
       printf("Writing success, you wrote '%s'\n", line);
    }else{
       printf("Writing went wrong =[\n");      
    }
}
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.