C语言 串行通信不工作.读取文件出错



经过几个小时的浏览和阅读,我仍然无法弄清楚为什么我的代码不起作用。我在不同的网站上看到了类似的代码片段,但我似乎无法让它工作。写作部分正在工作,但阅读出错。每个"真实字符"后面跟着三个空终止符。编写 19 个字符的字符串就可以了,我使用的 FPGA 会在显示屏上给出正确的数据。FPGA 应反转输入并将此包发送到串行端口。在超级终端中,这可以正常工作。

有人可以指出我的错误并告诉我我做错了什么吗?

提前感谢=)

#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, *buffWrite, *buffRead;
    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 createdn");    
       /* Check the state of the comm port */
       if(!GetCommState(hSerial, &dcbSerialParams.dcb)){
          printf("Error getting state n");
       }else{
          printf("Port availablen"); 
          /* 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 appliedn");  
            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 */
                line = "Something elser";
                //****************Write Operation*********************//
                writeToSerial(line, hSerial, dwBytesWritten);
                //***************Read Operation******************//
                if(ReadFile(hSerial, buffRead, BUFFERLENGTH, &dwBytesRead, NULL)){
                   printBuffer(buffRead, dwBytesRead);                     
                }
             }          
          }
       }
    }
    CloseHandle(hSerial);
    system("PAUSE");
    return 0;
}
void printBuffer(char * buffRead, DWORD dwBytesRead){
     int j;
     for(j = 0; j < dwBytesRead; j++){
         if(buffRead[j] != ''){    
           printf("%d: %cn", j, buffRead[j]);
         }
     }   
}
void writeToSerial(char *line, HANDLE hSerial, DWORD dwBytesWritten){
    WriteFile(hSerial, line, 19, &dwBytesWritten,NULL);
    if(dwBytesWritten){
       printf("Writing success, you wrote '%s'n", line);
    }else{
       printf("Writing went wrong =[n");      
    }
}

在这一行中:

if(ReadFile(hSerial, buffRead, BUFFERLENGTH, &dwBytesRead, NULL))

buffRead参数是一个未初始化的指针。将声明更改为:

char *line, *buffWrite, buffRead [BUFFERLENGTH+1];

Hyper 终端可能不会显示空字符,因此您的 fpga 可能实际上正在发送它们。

您可以尝试使用十六进制模式下Br@y终端进行测试,或者用示波器查看线。

最新更新