串口:读取数据有环回问题,读不到任何东西
这个问题已经被问过很多次了,我阅读了所有答案,但仍然无法找出问题所在。
我已与 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 端口读取数据,并且从不接收任何内容。我陷入困境,因为我无法真正确定问题出在哪里。任何指示都会有所帮助。
谢谢,
约格什
This problem has been asked many times, I read all the answers but still not able to figure out the problem.
I have set up connection with COM1 port
Wrote data to COM1 port using WriteFile
Now I am trying to read data from the port using ReadFile but it is not reading anything.
I have hard ware loopback the 2nd and 3rd of RS232 so that input can be read as output
#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);
}
The program is not returning any error code, but it continously waits to read data from COM1 port and never receives anything. I am stuck as I can't really decifer where the problem is. Any pointers will be helpful.
Thanks,
Yogesh
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有做任何明显的事情来设置硬件握手信号。串行端口设备几乎总是检查 RTS 和 DTR 信号,并且在它们处于活动状态之前不会发送任何内容。短于 EscapeCommFunction 以强制打开它们,设置 DCB.fDtrControl 和 fRtsControl 成员以打开硬件握手。
如果默认值不合适或被其他程序更改,则不设置基本通信属性(如波特率、奇偶校验、数据位和停止位)也会导致失败。
并使用 Putty 或超级终端等单独的程序检查接线是否正确。
You are not doing anything obvious to set the hardware handshake signals. A serial port device almost always checks the RTS and DTR signals and won't send anything until they are active. Short from EscapeCommFunction to force them on, set the DCB.fDtrControl and fRtsControl members to turn on hardware handshaking.
Not setting the basic communication properties like baudrate, parity, databits and stopbits leaves you open to failure as well if the defaults are not appropriate or were changed by another program.
And do check that wiring is okay with a separate program like Putty or Hyperterminal.