Serial command format

Hi to all

Im using a serial device wich works when receive a specific format, and i want to send that command with an of app.

If i send the command using a serial app it works, so i asume that im doing something wrong converting the data.

The command has to be in the following format:

N 500     530 \cr

so its: N + one space + int number + five spaces + int +carriage return

Im sending the following in of but not work

string str = "N " + ofToString(500) + "     " + ofToString(530)+" \n"
ofx::IO::ByteBuffer textbuffer(str);
device.writeBytes(textbuffer);

I think that im not sending the command right

A carriage return is not a new line, try replacing that and see if it works.

Ahh thanks! i will try with \r

I read that an string in c++ always have a null character at end, so maybe im also sending extra characters using a string. Maybe its more correct to do this way?

unsigned char buf[14] = {'N', ' ' , '5', '0', '0', ' ' , ' ' , ' ' , ' ' , ' ' , '5', '3', '0','\r'};
device.writeBytes(&buf[0], 14);

The null-termination sounds right, but you could always check the ByteBuffer internals to be sure, though you’d likely need to convert from decimal to ASCII.

ofx::ByteBuffer is just a vector of bytes. Unlike ofBuffer, which hides a null terminator inside, when you initialize a ofx::ByteBuffer with a string, the length of the buffer will be the same as the std::string's size. It does not copy a null terminator into the ofx::ByteBuffer. Unlike ofBuffer, which has a getText() method that returns a string and includes the hidden null terminator, you need to use the ofx::ByteBuffer::<< to convert an ofx::ByteBuffer back to a std::string.

Anyway, the point is, your example:

unsigned char buf[14] = {'N', ' ' , '5', '0', '0', ' ' , ' ' , ' ' , ' ' , ' ' , '5', '3', '0','\r'};
device.writeBytes(&buf[0], 14);

… should work. You shouldn’t send a null terminator unless your serial device expects a \r AND ad a \0 (which I doubt it does).

Yep, it works but only with a pc

The same code in mac does not work, i have tried ofserial and ofxserial

I think that the 250k baudrate does not work on mac

The serial device only works at 250k and its a SLAB_USBtoUART

I dont want to switch to a windows machine

What can i try? I dont understand why it does not work on mac

1 Like

I would confirm that the issue is ofxSerial or ofSerial first.

Are you able to connect to your device on OSX using screen or another serial program (here are a bunch to try http://pbxbook.com/other/mac-tty.html)?

i tried a pair of serial program but they doesnt have the avility to set a custom baud rate, you have to choose one from a droplist. So i can not test to connect at 250k and another baudrate doesn work with that device.

Tried also screen, seems like i can connect (get no errors) but dont know how to get data.

I read this, and found info about baudrate alias but the cp2102 driver not work as the ftdi one and can not set the baudrate on the info.plist

You have this driver?:

http://www.silabs.com/Support%20Documents/Software/Mac_OSX_VCP_Driver_Release_Notes.txt

There is some mention of a recent fix for baud rates over 234000 …?

Yep, its that one

I read that but… i can not make it work at 250k.