gpt4 book ai didi

c - 串行端口 : Read data problem with loopback, 未读取任何内容

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

这个问题已经被问过很多次了,我看了所有的答案,还是没能弄清楚问题所在。

我已经建立了与 COM1 端口的连接使用 WriteFile 将数据写入 COM1 端口现在我正在尝试使用 ReadFile 从端口读取数据,但它没有读取任何内容。我有硬件环回 RS232 的第二个和第三个,这样输入就可以作为输出读取

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


int main(int argc, char *argv[])

{

DCB dcb;
HANDLE hCom;
BOOL fSuccess;
char *pcCommPort = "COM3";

hCom = CreateFile( _T("COM3"), //pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, // must be opened with exclusive-access
0, // no security attributes
OPEN_EXISTING, // must use OPEN_EXISTING
FILE_ATTRIBUTE_NORMAL, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);


if (hCom == INVALID_HANDLE_VALUE)
{
// Handle the error.
printf ("CreateFile failed with error %d.\n", GetLastError());
}

// Build on the current configuration, and skip setting the size
// of the input and output buffers with SetupComm.
fSuccess = GetCommState(hCom, &dcb);

if (!fSuccess)
{
// Handle the error.
printf ("GetCommState failed with error %d.\n", GetLastError());
}

dcb.BaudRate = 9600; // set the baud rate
dcb.ByteSize = 8; // data size, xmit, and rcv
dcb.Parity = NOPARITY; // no parity bit
dcb.StopBits = ONESTOPBIT; // one stop bit

fSuccess = SetCommState(hCom, &dcb);

if (!fSuccess)
{
// Handle the error.
printf ("SetCommState failed with error %d.\n", GetLastError());
}


printf ("Serial port %s successfully reconfigured.\n", pcCommPort);

char Buff[] = "Hello";
char Buff2[50] = {};

DWORD dwNumBytesWritten;
DWORD dwBytesTransferred;

printf("\n\n\n\n\n\n Start writting ! \n");

WriteFile (hCom, // Port handle
Buff, // Pointer to the data to write
sizeof(Buff), // Number of bytes to write
&dwNumBytesWritten, // Pointer to the number of bytes written
NULL // Must be NULL for Windows Embedded CE
);


printf("\n Bytes Written to the terminal ");
for( int j=0; j<dwNumBytesWritten; j++)
printf("%c",Buff[j]);


printf("\n Byte length %d \n", dwNumBytesWritten);
printf("\n\n\n\n\n\n Start reading !\n");

ReadFile (hCom, // Port handle
Buff2, // Pointer to data to read
dwNumBytesWritten, // Number of bytes to read
&dwBytesTransferred, // Pointer to number of bytes read
NULL // Must be NULL for Windows Embeddded CE
);


for( int j=0; j<dwNumBytesWritten; j++)
printf("%c",Buff2[j]);

printf("\n BytesRead from the terminal:%d \n",dwBytesTransferred);

CloseHandle(hCom);

int num;
scanf("%d", &num);
}

程序没有返回任何错误代码,但它一直在等待从 COM1 端口读取数据,但从未收到任何东西。我被困住了,因为我无法真正确定问题出在哪里。任何指示都会有所帮助。

谢谢,

约格什

最佳答案

您没有做任何明显的事情来设置硬件握手信号。串行端口设备几乎总是检查 RTS 和 DTR 信号,并且在它们处于事件状态之前不会发送任何内容。从 EscapeCommFunction 强制它们打开,设置 DCB.fDtrControl 和 fRtsControl 成员以打开硬件握手。

不设置波特率、奇偶校验、数据位和停止位等基本通信属性,如果默认设置不合适或被其他程序更改,您也会面临失败。

并使用 Putty 或 Hyperterminal 等单独的程序检查接线是否正常。

关于c - 串行端口 : Read data problem with loopback, 未读取任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4126457/

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