Search code examples
c++linuxserial-portlibserial

C++ writing byte to serial stream


I realized this sort of question is scattered all over the Internet, but nothing seems to be pointing me right.

I'm trying to send a command to a Propeller control board through a serial stream. The connection seems to be working, but the it keeps on hitting error for any kind of command i send - it returns the same hex data of: 10 ffffffe1. It seems like the data being sent is not the correct format. The board seems to be expecting byte data, and (i think) my code seem to be doing it, but I just can't figure out what I'm doing wrong. I think I'm not converting the data correctly. Here's my code, below. Thanks everyone.

Note: the code below doesn't show reading the response; it's done in another program of mine, which works, it reads responses from serial terminals correctly as well.

#include <iostream>
#include <SerialStream.h>
using namespace LibSerial;
using namespace std;
int main(int argc, char** argv) {

SerialStream serial;
serial.Open("/dev/ttyUSB0");
serial.SetCharSize(SerialStreamBuf::CHAR_SIZE_8);
serial.SetBaudRate(SerialStreamBuf::DEFAULT_BAUD);
serial.SetNumOfStopBits(1);
serial.SetFlowControl(SerialStreamBuf::FLOW_CONTROL_NONE);
if(serial.good()){
    cout << "SUCCESSFUL: serial port opened at: /dev/ttyUSB0" << endl;
    usleep(5000);
}
else{
    cout << "ERROR: Could not open serial port." << endl;
    return 1;
}

std::string str= "ver\r"; //command to get version of firmware
const char* data = str.data();
serial.write(data, sizeof data);
return 0 ;

}


Solution

  • Yeay, after shifting around on libraries to use, i manage to solve this problem at the same time as another. This problem of mine has the same solution to another one. here you guys go: Writing STRINGS to serial port in C++ linux

    Thanks everyone for helping, really!