gpt4 book ai didi

c - 在 Windows 中使用 C 的 UART

转载 作者:太空宇宙 更新时间:2023-11-04 03:33:08 25 4
gpt4 key购买 nike

我正在尝试使用 C 在外部微 Controller 和 Windows 之间建立 UART 接口(interface)。

我使用以下代码设置 UART 参数,然后将一个字符发送到指定的 COM 端口。

我成功发送了一个角色。但是我如何收到一个回来?代码如下:

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

HANDLE hSerial;
int main()
{
// OPEN SERIAL PORT AND SET INITAL UART PARAMETERS
//=================================================
DCB dcbSerialParams = {0}; COMMTIMEOUTS timeouts = {0};
fprintf(stderr, "Opening serial port...");
hSerial = CreateFile("\\\\.\\COM3", GENERIC_READ|GENERIC_WRITE, 0, NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );

if (hSerial == INVALID_HANDLE_VALUE){fprintf(stderr, "Error\n");return 1;}
else {fprintf(stderr, "OK\n");}

// Set device parameters (115200 baud, 1 start bit, 1 stop bit, no parity)
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
if (GetCommState(hSerial, &dcbSerialParams) == 0){fprintf(stderr, "Error getting device state\n");CloseHandle(hSerial);return 1;}

dcbSerialParams.BaudRate = CBR_57600; dcbSerialParams.ByteSize = 8; dcbSerialParams.StopBits = ONESTOPBIT; dcbSerialParams.Parity = NOPARITY;

if(SetCommState(hSerial, &dcbSerialParams) == 0){fprintf(stderr, "Error setting device parameters\n");CloseHandle(hSerial);return 1;}

// Set COM port timeout settings
timeouts.ReadIntervalTimeout = 50; timeouts.ReadTotalTimeoutConstant = 50; timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 50; timeouts.WriteTotalTimeoutMultiplier = 10;

if(SetCommTimeouts(hSerial, &timeouts) == 0){fprintf(stderr, "Error setting timeouts\n"); CloseHandle(hSerial); return 1;}



// SETUP AND SEND DATA FROM UART
//==============================
int VarNum=8;
char str[15];
sprintf(str,"%ld",VarNum);


DWORD bytes_written, total_bytes_written = 0;
fprintf(stderr, "Sending bytes...");
if(!WriteFile(hSerial,str, strlen(str), &bytes_written, NULL))
{
fprintf(stderr, "Error\n");
CloseHandle(hSerial);
return 1;
}
fprintf(stderr, "%d bytes written\n", bytes_written);


// CLOSE SERIAL PORT AND EXIT MAIN FUNCTION
//=========================================
fprintf(stderr, "Closing serial port...");

if (CloseHandle(hSerial) == 0){fprintf(stderr, "Error\n"); return 1;}
fprintf(stderr, "OK\n");return 0;

}

最佳答案

您可以为此使用 ReadFile():

BOOL bOk = ReadFile(hSerial, buffer, sizeof(buffer) - 1, &bytesRead, NULL);
if (bOk && (bytesRead > 0)) {
buffer[bytesRead] = '\0';
}

从串行端口读取时,ReadFile() 应该阻塞,直到有更多数据或发生超时。 (这应该在一个单独的线程中(在一个循环中)完成,或者可能通过使用 ReadFileEx() 进行异步操作)。

When reading from a communications device, the behavior of ReadFile is governed by the current communication time-outs as set and retrieved using the SetCommTimeouts and GetCommTimeouts functions. Unpredictable results can occur if you fail to set the time-out values.

同时检查 SetCommState()PurgeComm()

关于c - 在 Windows 中使用 C 的 UART,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34576526/

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