2

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 ;

}

dsolimano
  • 8,870
  • 3
  • 48
  • 63
haikalpribadi
  • 424
  • 1
  • 7
  • 19

3 Answers3

4
serial.write(str.data(), str.size());

Your current code uses sizeof data, which is the size of a pointer, and tells you nothing about the length of the pointed-to data.

Using the "default" baud rate also seems very unlikely to work.

If you still have problems after fixing those, it's probably your non-standard SerialStream class (which is NOT part of the Standard C++ Library or POSIX). If you provide a link to it, we could take a look and see if it's right.

I would suggest using the POSIX standard functions open, tcsetattr, read, and write for your serial port usage, since these are widely used and any bugs have long since been found and fixed.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Thank you so much for the advice on str.size(). will try it first thing tomorrow morning at work. Hope it works. Yeah, that matter of default baud rate is also an issue, but i thought i didn't wanna bring it up at this point, and solve things one by one. The case is, no other setting of baud rate instead of default can work - they all return error. I dont know why. Do you think you know why? – haikalpribadi Feb 17 '12 at 19:00
  • @30hoursflight: Without seeing your serial port library, I can't tell you why other baud rates aren't working. Is it a real PC-AT serial port, or a USB-serial adapter? And I do believe that using the wrong baud rate would give the kind of garbage data your question talks about. You can't solve anything until you solve the baud rate. – Ben Voigt Feb 17 '12 at 20:27
  • however, I have another program to read serial data from the device. It has the same issue of baud rate, but it is able to read the data correct. Hence, I thought the baud rate wasn't a big issue. What do you think? the name of the serial port library is "LibSerial" as seen in the code. and i'm quite sure they're valid baud rate values. what do you think? – haikalpribadi Feb 18 '12 at 03:27
  • @30hoursflight: If there's a baud rate mismatch, no data will get through. Plus, the failure may be intermittent, because "default" often means "whatever the last program used". I've never heard of "LibSerial" before, you'll have to provide a link. Normally the `tcsetattr` function is used to configure serial ports on Linux. – Ben Voigt Feb 18 '12 at 03:56
  • I see. that makes sense. However, everytime i set the baud rate to 115200, and then i check if the connection is `.good()` it always return false. Here's the link to the library: https://launchpad.net/ubuntu/lucid/+package/libserial-doc and http://libserial.sourcearchive.com/documentation/0.6.0~rc1-0ubuntu1/classSerialPort_1_1SerialPortImpl.html And I will start looking in to `tcsetattr` – haikalpribadi Feb 18 '12 at 04:21
  • @30hoursflight: Are you passing `115200`, or `SerialPort::BaudRate::BAUD_115200`? – Ben Voigt Feb 18 '12 at 04:24
  • I'm passing SerialStreamBuf::Baud_115200. which one do you think it should be? – haikalpribadi Feb 18 '12 at 05:33
  • oh, i just looked in the SerialStreamBuf.h again, and it shows: `BAUD_115200 = SerialPort::BAUD_115200` .... – haikalpribadi Feb 18 '12 at 06:02
  • @ben-voight, i just found something wrong with my code. `.good()` seems to have been deprecated in the new releases. Instead you have to use `IsOpen()` to check if the stream is in an open good state. I changed my code, and now it works. it returns true, apparently. There are no more error response from the serial port for the commands, but there are just no response now. Also, my ReadStream program continuously reads 0 now.. Do you think u know what's going on here? – haikalpribadi Feb 18 '12 at 06:25
  • @ben-voight, no worries now. I decided to go with tcsetattr .. everything is working fine now. Thank you so much for your help, man. cheers. I'm sorry I can't mark your answer correct, cause i dont think we solved the problem, and I dont want people to visit this topic thinking that they found a solution. thanks man.. – haikalpribadi Feb 18 '12 at 08:07
  • @30hoursflight: I've edited my answer in case that's the advice you would want future readers to see. – Ben Voigt Feb 18 '12 at 17:44
0

Have you tried using

str.c_str()

instead of

str.data()

? std::string.data() isn't guaranteed to return a null-terminated c-style string. If you want to convert std::string into a c-style const char*, use .c_str()

bstamour
  • 7,746
  • 1
  • 26
  • 39
0

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!

Community
  • 1
  • 1
haikalpribadi
  • 424
  • 1
  • 7
  • 19