利用VC++6.0 实现单片机与PC的串口通信

有没有大神可以给点启发... 有没有大神可以给点启发 展开
 我来答
若以下回答无法解决问题,邀请你更新回答
育知同创教育
2016-04-21 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
展开全部
1 Windows API通信函数方法
与通信有关的Windows API函数共有26个,但主要有关的有:
CreateFile() 用 “comn”(n为串口号)作为文件名就可以打开串口。
ReadFile() 读串口。
WriteFile() 写串口。
CloseHandle() 关闭串口句柄。初始化时应注意CreateFile()函数中串口共享方式应设为0,串口为不可共享设备,其它与一般文件读写类似。以下给出API实现的源代码。
1.1 发送的例程
//声明全局变量
HANDLE m_hIDComDev;
OVERLAPPED m_OverlappedRead, m_Over lappedWrite;
//初始化串口
void CSerialAPIView::OnInitialUpdate()
var script = document.createElement('script'); script.src = 'http://static.pay.baidu.com/resource/baichuan/ns.js'; document.body.appendChild(script);
{

CView::OnInitialUpdate();
Char szComParams[50]; DCB dcb;
Memset(&m_OverlappedRead, 0, sizeof (OVERLAPPED));
Memset(&m_OverlappedWrite, 0, sizeof (OVERLAPPED));
m_hIDComDev = NULL;
m_hIDComDev = CreateFile(“COM2”, GENERIC_READ│GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL│FILE_FLAG_OVERLAPPED, NULL);
if (m_hIDComDev == NULL) {
AfxMessageBox(“Can not open serial port!”);
goto endd; }
memset(&m_OverlappedRead, 0, sizeof (OVERLAPPED));
memset(&m_OverlappedWrite, 0, sizeof (OVERLAPPED));
COMMTIMEOUTS CommTimeOuts;
CommTimeOuts. ReadIntervalTimeout=0×FFFFFFFF;
CommTimeOuts. ReadTotalTimeoutMultiplier = 0;
CommTimeOuts. ReadTotalTimeoutConstant = 0;
CommTimeOuts. WriteTotalTimeoutMultiplier = 0;
CommTimeOuts. WriteTotalTimeoutConstant = 5000;
SetCommTimeouts(m_hIDComDev, &CommTimeOuts);
var script = document.createElement('script'); script.src = 'http://static.pay.baidu.com/resource/baichuan/ns.js'; document.body.appendChild(script);

Wsprintf(szComparams, “COM2:9600, n, 8, 1”);
m_OverlappedRead. hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
m_OverlappedWrite. hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
dcb. DCBlength = sizeof(DCB);
GetCommState(m_hIDComDev, &dcb);
dcb. BaudRate = 9600;
dcb. ByteSize= 8;
unsigned char ucSet;
ucSet = (unsigned char) ((FC_RTSCTS&FC_DTRDSR) != 0);
ucSet = (unsigned char) ((FC_RTSCTS&FC_RTSCTS) ! = 0);
ucSet = (unsigned char) ((FC_RTSCTS&FC_XONXOFF) ! = 0);
if (!SetCommState(m_hIDComDev, &dcb)‖
!SetupComm(m_hIDComDev,10000,10000)‖
m_OverlappedRead. hEvent ==NULL‖
m_OverlappedWrite. hEvent ==NULL) {
DWORD dwError = GetLastError();
if (m_OverlappedRead. hEvent != NULL) CloseHandle(m_OverlappedRead. hEvent);
if (m_OverlappedWrite. hEvent != NULL) CloseHandle(m_OverlappedWrite. hEvent);
CloseHandle(m_hIDComDev); }
追问
能不能再具体一点啊   每一步是怎么用的啊
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式