vc中串行通信的问题++ MFC

发布于 2024-11-08 05:05:28 字数 2594 浏览 0 评论 0原文

我正在开发一个嵌入式设备。

当我使用 putty 写入该端口时,它连接在 COM6 端口上,它会回复我结果。

例如,当你说你好时,它会回复你好。

我编写了一个写入 COM6 的程序,我能够写入 COM6 端口,但是当我读取时我没有得到任何东西。

我的代码是...

// HelloWorld.cpp :定义控制台应用程序的入口点。 //

#include "stdafx.h"
#include <iostream>
#include <windows.h>
int main()
{
    using namespace std;
//  cout << "Hello world!" << endl;



    HANDLE hSerial;

    hSerial = CreateFile("COM6",
    GENERIC_READ | GENERIC_WRITE,
    FILE_SHARE_WRITE | FILE_SHARE_READ,
    0,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    0);

    if(hSerial==INVALID_HANDLE_VALUE)
    {
        if(GetLastError()==ERROR_FILE_NOT_FOUND)
        {
//          TRACE("serial port does not exist for reading\n");
        //serial port does not exist. Inform user.
        }
//          TRACE("some other error,serial port does not exist for reading\n");
        //some other error occurred. Inform user.
    }

    DCB dcbSerialParams = {0};

    dcbSerialParams.DCBlength=sizeof(dcbSerialParams);

    if (!GetCommState(hSerial, &dcbSerialParams)) 
    {
//                  TRACE("error getting state for reading\n");
    //error getting state
    }

    dcbSerialParams.BaudRate=9600;
    dcbSerialParams.ByteSize=8;
    dcbSerialParams.StopBits=ONESTOPBIT;
    dcbSerialParams.Parity=NOPARITY;

    if(!SetCommState(hSerial, &dcbSerialParams))
    {
//                  TRACE("error setting state for reading\n");
    //error setting serial port state
    }
    COMMTIMEOUTS timeouts={0};

    timeouts.ReadIntervalTimeout=50;
    timeouts.ReadTotalTimeoutConstant=50;
    timeouts.ReadTotalTimeoutMultiplier=10;

    timeouts.WriteTotalTimeoutConstant=50;
    timeouts.WriteTotalTimeoutMultiplier=10;

    if(!SetCommTimeouts(hSerial, &timeouts))
    {
//                  TRACE("some error occured for reading\n");
        //error occureed. Inform user
    }       
    int n=100;
    char szBuff[100];
    DWORD dwBytesRead = 0;
    char szBuff1[100]="LIST";

    DWORD dwByteswrote = 0;

    if(!WriteFile(hSerial, szBuff1, n, &dwByteswrote, NULL))
    {
                    cout << "error writing" ;
    }
    cout << dwByteswrote;
    while(1)
    {
        if(!ReadFile(hSerial, szBuff, n, &dwBytesRead, NULL))
        {
            cout << "error reading";
        }
        else
        {
            cout << dwBytesRead;
            if(dwBytesRead>0)
            {
                cout << szBuff;
                break;
            }
        }
    }
}

任何人都可以帮助我吗? 我做错了什么?

I am working on an embeeded device.and it is connected on COM6 port

when i use putty to write to that port it replies me with result.

example is when you say hello,it replies hello.

I wrote a program to write to COM6,i am able to write to the COM6 port but when i am reading i am not getting anything.

my code is...

// HelloWorld.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <windows.h>
int main()
{
    using namespace std;
//  cout << "Hello world!" << endl;



    HANDLE hSerial;

    hSerial = CreateFile("COM6",
    GENERIC_READ | GENERIC_WRITE,
    FILE_SHARE_WRITE | FILE_SHARE_READ,
    0,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    0);

    if(hSerial==INVALID_HANDLE_VALUE)
    {
        if(GetLastError()==ERROR_FILE_NOT_FOUND)
        {
//          TRACE("serial port does not exist for reading\n");
        //serial port does not exist. Inform user.
        }
//          TRACE("some other error,serial port does not exist for reading\n");
        //some other error occurred. Inform user.
    }

    DCB dcbSerialParams = {0};

    dcbSerialParams.DCBlength=sizeof(dcbSerialParams);

    if (!GetCommState(hSerial, &dcbSerialParams)) 
    {
//                  TRACE("error getting state for reading\n");
    //error getting state
    }

    dcbSerialParams.BaudRate=9600;
    dcbSerialParams.ByteSize=8;
    dcbSerialParams.StopBits=ONESTOPBIT;
    dcbSerialParams.Parity=NOPARITY;

    if(!SetCommState(hSerial, &dcbSerialParams))
    {
//                  TRACE("error setting state for reading\n");
    //error setting serial port state
    }
    COMMTIMEOUTS timeouts={0};

    timeouts.ReadIntervalTimeout=50;
    timeouts.ReadTotalTimeoutConstant=50;
    timeouts.ReadTotalTimeoutMultiplier=10;

    timeouts.WriteTotalTimeoutConstant=50;
    timeouts.WriteTotalTimeoutMultiplier=10;

    if(!SetCommTimeouts(hSerial, &timeouts))
    {
//                  TRACE("some error occured for reading\n");
        //error occureed. Inform user
    }       
    int n=100;
    char szBuff[100];
    DWORD dwBytesRead = 0;
    char szBuff1[100]="LIST";

    DWORD dwByteswrote = 0;

    if(!WriteFile(hSerial, szBuff1, n, &dwByteswrote, NULL))
    {
                    cout << "error writing" ;
    }
    cout << dwByteswrote;
    while(1)
    {
        if(!ReadFile(hSerial, szBuff, n, &dwBytesRead, NULL))
        {
            cout << "error reading";
        }
        else
        {
            cout << dwBytesRead;
            if(dwBytesRead>0)
            {
                cout << szBuff;
                break;
            }
        }
    }
}

Can anyone help me in this??
what is the wrong thing i am making??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苄①跕圉湢 2024-11-15 05:05:28

可能您必须在“LIST”命令之后发送“换行”或“回车”才能真正使设备发送数据?

尝试发送“LIST\r”、“LIST\n”或“LIST\r\n”。

It might be that you have to send a "line feed" or a "carriage return" after the "LIST" command to actually make the device send data?

Try sending "LIST\r", "LIST\n" or "LIST\r\n".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文