在同一台PC上通过C进行COM端口编程 [英] Com port programming through c on same pc

查看:106
本文介绍了在同一台PC上通过C进行COM端口编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搜索一个示例程序,在该程序中将有代码打开com端口,将数据发送到com端口,然后在计算机屏幕上显示发送数据,并且该程序可以在Visual Studio 2010中运行,而不能在Visual Studio 2010中运行使用MFC,我要在PC上进行COM口通信,这意味着我没有其他PC可以发送数据.

i am searching for a sample program in which there would be code to open the com port send data to com port then show the send data on the screen of my computer and that can be able to run in visual studio 2010 and not using MFC,i want the com port communication on my PC,i mean i don''t have any other PC to send data.

推荐答案

例如,请参阅: www.robbayer.com/files/serial-win.pdf [
Please see, for example: www.robbayer.com/files/serial-win.pdf[^].

If you need better help, please use some search engine.

—SA


要在同一台PC上接收数据,您需要在串行端口上使用一个环回插头(无需使用硬件流控制,该插头仅将TXD线连接到RXD行).

使用Windows API函数CreateFile()打开端口,并使用WriteFile()/ReadFile()进行读写(未试的示例):

To receive data on the same PC you need a loopback plug on your serial port (without using hardware flow control this plug just connects the TXD line to the RXD line).

Use the Windows API function CreateFile() to open the port and WriteFile() / ReadFile() to write and read (untested example):

HANDLE hCom = ::CreateFile(_T("COM1"),
    GENERIC_READ | GENERIC_WRITE,
    0,
    NULL,
    OPEN_EXISTING,
    0,
    NULL);
if (hCom != INVALID_HANDLE_VALUE)
{
    DWORD dwWritten;
    ::WriteFile(hCom, "Test", strlen("Test"), &dwWritten, NULL);
    DWORD dwRead;
    char pBuf[32];
    ::ReadFile(hCom, pBuf, sizeof(pBuf), &dwRead, NULL);
    pBuf[min(dwRead, sizeof(pBuf)-1)] = '\0';
    ::MessageBox(NULL, pBuf, _T("Received text"), MB_OK);
    ::CloseFile(hCom);
}


这篇关于在同一台PC上通过C进行COM端口编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆