質問

刈り取りされた装置に取り組んでいます。そしてそれはCOM6ポートで接続されています

PuTTYを使用してそのポートに書き込むと、結果は私に返信します。

例はこんにちはと言うとき、それはこんにちは。

私は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;
            }
        }
    }
}
.

誰もが私を助けてくれることができますか? 私が作っている間違ったことは何ですか??

役に立ちましたか?

解決

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".

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top