如何关闭和重新打开串行通讯C ++

时间:2019-05-07 22:06:05

标签: c++ serial-port

我正在尝试通过串行通信执行命令,并试图一次读取一个命令。我面临的问题是所有命令仅执行一次,而不是在单独的函数中调用。谁能帮我指导这是怎么回事

我也尝试过关闭串行端口并刷新缓冲区,但这对我不起作用,或者我做错了什么

#include <iostream>
#include <windows.h>
#include "Serial.h"
#include "stdafx.h"

SerialPort* COMConnect(){
std::pair<std::string, int> buffer = findSerialPort();
char* portName = _strdup(buffer.first.c_str());
SerialPort *COM;
COM= new SerialPort(portName);
return pilchuk;}


pair<string, int> COMWrite(char* command){

char receivedString[DATA_LENGTH];
SerialPort * COM= COMConnect();
if (COM->isConnected()){    
    bool hasWritten = COM->writeSerialPort(command, DATA_LENGTH);
    if (hasWritten){
        Sleep(10);
        int hasRead = pilchuk->readSerialPort(receivedString, DATA_LENGTH);
        COM->Close();
        return std::make_pair(receivedString, 0);
    }
    else{
        return std::make_pair("Data was not written", -1);
    }
}
else{
    return std::make_pair("Serial Port Not Connected", -1);
}

 }

  int main(void)
  {

pair<string, int> dataBuffers = COMWrite("4C0101");
cout << dataBuffers.first << std::endl;
cout << dataBuffers.second << std::endl;
pair<string, int> dataBuffer = COMWrite("4E0100");
cout << dataBuffer.first << std::endl;
cout << dataBuffer.second << std::endl;
}

我应该在结果一中得到第一个值,而在下一个结果中得到第二个值。 但是我得到的结果是dataBuffers.first而不是分开。

0 个答案:

没有答案