读串口ReadFile读不到数据?

Yolanda_Yolanda 2015-05-13 10:45:01
下位机用的是FT232芯片,所以我从FTDI官网找了代码直接下载来用的(http://www.ftdichip.com/Support/SoftwareExamples/CodeExamples/VC.htm)Example 5.
代码中我只将波特率改成了8000000,跟我的下位机匹配,其他的代码没有改动,运行的时候运行到if(ReadFile(hCommPort, buf, w_data_len, &dwRead, NULL))这句代码就直接跳出了,不知道为什么?帮忙看看是什么原因?
我把代码贴出来:
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include "ftd2xx.h"

int main(int argc, char* argv[])
{
FT_HANDLE fthandle;
FT_STATUS res;
LONG COMPORT;

char COMx[5];
int n;

DCB dcb;
HANDLE hCommPort;
BOOL fSuccess;


/***********************************************************************
//Find the com port that has been assigned to your device.
/***********************************************************************/

res = FT_Open(0, &fthandle);

if(res != FT_OK){

printf("opening failed! with error %d\n", res);

return 1;
}


res = FT_GetComPortNumber(fthandle,&COMPORT);

if(res != FT_OK){

printf("get com port failed %d\n", res);

return 1;
}

if (COMPORT == -1){

printf("no com port installed \n");
}

else{
printf("com port number is %d\n", COMPORT);

}


FT_Close(fthandle);


/********************************************************/
// Open the com port assigned to your device
/********************************************************/

n = sprintf_s(COMx, "COM%d",COMPORT);

hCommPort = CreateFile(
COMx,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);

if (hCommPort == INVALID_HANDLE_VALUE)
{

printf("Help - failed to open\n");
return(1);

}


printf("Hello World!\n");

/********************************************************/
// Configure the UART interface parameters
/********************************************************/

fSuccess = GetCommState(hCommPort, &dcb);


if (!fSuccess)

{
printf("GetCommStateFailed \n", GetLastError());
return (2);

}

//set parameters.

dcb.BaudRate = 8000000;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;

fSuccess = SetCommState(hCommPort, &dcb);


if (!fSuccess)

{
printf("SetCommStateFailed \n", GetLastError());
return (3);

}


printf("Port configured \n");


/********************************************************/
// Writing data to the USB to UART converter
/********************************************************/

DWORD dwwritten = 0, dwErr;
char data_out[12] = "HELLO WORLD";
DWORD w_data_len = 12;


fSuccess = WriteFile(hCommPort, &data_out, w_data_len, &dwwritten, NULL);


if (!fSuccess)

{
dwErr = GetLastError();
printf("Write Failed \n", GetLastError());
return (4);

}


printf("bytes written = %d\n", dwwritten);

/********************************************************/
//Reading data from the USB to UART converter
/********************************************************/

char buf[256];
DWORD dwRead;


memset(buf,0,256);

if (ReadFile(hCommPort, buf, w_data_len, &dwRead, NULL))
{
printf("data read = %s\n", buf);
}


/********************************************************/
//Closing the device at the end of the program
/********************************************************/


CloseHandle(hCommPort);

getchar();
return 0;
}

运行结果如下:
com port number is 3
Hello World!
Port configured
bytes weittrn:12
...全文
822 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
Yolanda_Yolanda 2015-05-14
  • 打赏
  • 举报
回复
引用 12 楼 schlafenhamster 的回复:
可能要按 USB 编程
恩,可以按USB口编程,不过按串口应该也可以的,官网上提供了虚拟串口驱动VCP Drivers和直接读USB的驱动D2xx Drivers。我这种都想试一下,学习一下。 官网提供了一个虚拟串口驱动(http://www.ftdichip.com/Drivers/VCP.htm),介绍上说“Virtual COM port (VCP) drivers cause the USB device to appear as an additional COM port available to the PC. Application software can access the USB device in the same way as it would access a standard COM port.”
schlafenhamster 2015-05-14
  • 打赏
  • 举报
回复
可能要按 USB 编程
Yolanda_Yolanda 2015-05-14
  • 打赏
  • 举报
回复
http://www.ftdichip.com/Products/ICs/FT232H.htm 下位机用的是这个芯片,这是它的说明书
Yolanda_Yolanda 2015-05-14
  • 打赏
  • 举报
回复
引用 9 楼 worldy 的回复:
老兄你也太牛了吧,rs232 DEC实验室的测试结果为 波特率bps 1号电缆传输距离(米) 2号电缆传输距离(米) 110 1500 900 300 1500 900 1200 900 900 2400 300 150 4800 300 75 9600 75 75 你能搞到8M,不得不pf
据说FT232这个是虚拟串口,可按USB的传输速率,8M是没问题的,FT232的用户手册里也有介绍,不过我自己没有用过,我说不清楚,有懂的朋友可以来说明一下,我也学习学习。
Yolanda_Yolanda 2015-05-14
  • 打赏
  • 举报
回复
引用 14 楼 schlafenhamster 的回复:
"不过按串口应该也可以的" 要连接USB 口 RS232 与 SUB 硬件不同
我连接的是USB口,要是连RS232串口的话,那我就不会用到FT232这个芯片了
schlafenhamster 2015-05-14
  • 打赏
  • 举报
回复
"不过按串口应该也可以的" 要连接USB 口 RS232 与 SUB 硬件不同
Yolanda_Yolanda 2015-05-13
  • 打赏
  • 举报
回复
引用 3 楼 wxhxj0268 的回复:
你直接使用别人的代码,COM口选择正确吗?
COM口是对的,我用串口监控软件Bus Hound看了
笨笨仔 2015-05-13
  • 打赏
  • 举报
回复
你直接使用别人的代码,COM口选择正确吗?
Yolanda_Yolanda 2015-05-13
  • 打赏
  • 举报
回复
引用 1 楼 oyljerry 的回复:
查看跳出时返回的错误码信息来分析
程序输出的只有这个信息,从哪里查看这些返回值所代表信息呢? 线程 0x22c4 已退出,返回值为 -1073741510 (0xc000013a)。 程序“[6100] VCP_EX.exe”已退出,返回值为 -1073741510 (0xc000013a)。
oyljerry 2015-05-13
  • 打赏
  • 举报
回复
查看跳出时返回的错误码信息来分析
worldy 2015-05-13
  • 打赏
  • 举报
回复
老兄你也太牛了吧,rs232 DEC实验室的测试结果为 波特率bps 1号电缆传输距离(米) 2号电缆传输距离(米) 110 1500 900 300 1500 900 1200 900 900 2400 300 150 4800 300 75 9600 75 75 你能搞到8M,不得不pf
Yolanda_Yolanda 2015-05-13
  • 打赏
  • 举报
回复
我现在倒是有数据读上来,但是数据只读上来两行,不能连续上传,该怎么弄? 我在Bus Hound监控串口可以看到我发下去的指令是: OUT:aa 55 00 00 00 01 4f 7e 00 00 00 0d 0a 下位机接收到这条指令后就开始上传数据,上传来的数据是: IN:aa 55 00 24 00 f1 00 00 78 0e 90 76 07 a0 76 07 70 76 07 80 76 60 78 07 40 78 07 80 78 07 90 数据头aa55是正确的,但是应该有大量数据在不断传上来的,为什么只能接收到这么一点数据? 帮忙看下怎么回事?
Yolanda_Yolanda 2015-05-13
  • 打赏
  • 举报
回复
引用 6 楼 schlafenhamster 的回复:
dcb.BaudRate = 8000000; 1. 太高 2. 非标准波特率 , (误码多)
是吗?下位机我不懂,老师给的一个下位机,说波特率设计的是8兆。 我现在倒是有数据读上来,但是数据只读上来两行,不能连续上传,该怎么弄? 我在Bus Hound监控串口可以看到我发下去的指令是: OUT:aa 55 00 00 00 01 4f 7e 00 00 00 0d 0a 下位机接收到这条指令后就开始上传数据,上传来的数据是: IN:aa 55 00 24 00 f1 00 00 78 0e 90 76 07 a0 76 07 70 76 07 80 76 60 78 07 40 78 07 80 78 07 90 数据头aa55是正确的,但是应该有大量数据在不断传上来的,为什么只能接收到这么一点数据? 帮忙看下怎么回事?
schlafenhamster 2015-05-13
  • 打赏
  • 举报
回复
dcb.BaudRate = 8000000; 1. 太高 2. 非标准波特率 , (误码多)
Yolanda_Yolanda 2015-05-13
  • 打赏
  • 举报
回复
我好像知道原因了,下位机的版本的问题,之前调试的时候下位机程序设置了要接收到一条固定的命令才开始上传数据,刚想起来,现在我把这条命令发下去就有数据读的上来了,虽然读上来的数据不对,不过至少能通信了,我再调试一下看看........

16,473

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧