gpt4 book ai didi

c - 串行通讯不工作。读取文件出错

转载 作者:行者123 更新时间:2023-11-30 14:25:39 25 4
gpt4 key购买 nike

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

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

提前致谢=)

#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 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 */
line = "Something else\r";
//****************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] != '\0'){
printf("%d: %c\n", 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];

关于c - 串行通讯不工作。读取文件出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10126338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com