57 fprintf(stderr,
"Unable to command baud rate change");
58 perror(
"openConnection");
66 struct termios options;
67 tcgetattr(handle, &options);
68 cfsetispeed(&options, B115200);
69 cfsetospeed(&options, B115200);
72 if (tcsetattr(handle, TCSADRAIN, &options)) { fprintf(stderr,
"tcsetattr() failed!\n"); }
80 tcgetattr(handle, &check);
81 if (cfgetispeed(&check) != B115200) {
82 fprintf(stderr,
"Unable to set target baud. Wanted %d, got %d\n", 115200,
95 fprintf(stderr,
"Unable to command baud rate change");
96 perror(
"openConnection");
127 static int index = 0;
166 if (errno != EAGAIN) {
167 fprintf(stderr,
"Unexpected error while reading from serial port (handle ID: 0x%02x)\n",
169 fprintf(stderr,
"read returned \"%s\" in readMessage\n", strerror(errno));
177 while (!(buf[(*index)] == 0xB5) && (*index) < (*hw)) {
180 if ((*index) == (*hw)) {
181 if ((*hw) > 0 && (*index) > 0) {
189 if (ti == 0) { out->
sync1 = 0xFD; }
193 if (((*hw) - (*index)) < 8) {
201 out->
sync1 = buf[(*index)];
202 out->
sync2 = buf[(*index) + 1];
204 out->
msgID = buf[(*index) + 3];
205 out->
length = buf[(*index) + 4] + (buf[(*index) + 5] << 8);
207 if (out->
sync2 != 0x62) {
215 if (((*hw) - (*index)) < (out->
length + 8)) {
218 if (ti == 0) { out->
sync1 = 0xFD; }
226 for (uint8_t i = 0; i < out->
length; i++) {
227 out->
data[i] = buf[(*index) + 6 + i];
231 for (uint16_t i = 0; i < out->
length; i++) {
232 out->
extdata[i] = buf[(*index) + 6 + i];
241 (*index) += 8 + out->
length;
276 bool ubx_waitForMessage(
const int handle,
const uint8_t msgClass,
const uint8_t msgID,
const int maxDelay,
278 const time_t deadline = time(NULL) + maxDelay;
279 while (time(NULL) < deadline) {
282 if ((out->
msgClass == msgClass) && (out->
msgID == msgID)) {
return true; }
300 fprintf(stderr,
"Attempted to send invalid message\n");
304 uint8_t head[6] = {out->
sync1,
308 (uint8_t)(out->
length & 0xFF),
309 (uint8_t)(out->
length >> 8)};
311 ssize_t ret = write(handle, head, 6);
313 perror(
"writeMessage:head");
329 perror(
"writeMessage:data");
334 ret = write(handle, tail, 2);
336 perror(
"writeMessage:tail");
int openSerialConnection(const char *port, const int baudRate)
Open a serial connection at a given baud rate.
int flag_to_baud(const int flag)
Convert a termios baud rate flag to a numerical value.
bool ubx_waitForMessage(const int handle, const uint8_t msgClass, const uint8_t msgID, const int maxDelay, ubx_message *out)
Read (and discard) messages until required message seen or timeout reached.
bool ubx_writeMessage(int handle, const ubx_message *out)
Send message to attached device.
bool ubx_readMessage_buf(int handle, ubx_message *out, uint8_t buf[UBX_SERIAL_BUFF], int *index, int *hw)
Read data from handle, and parse message if able.
bool ubx_readMessage(int handle, ubx_message *out)
Static wrapper around ubx_readMessage_buf()
#define UBX_SERIAL_BUFF
Serial buffer size.
int ubx_openConnection(const char *port, const int initialBaud)
Set up a connection to a UBlox module on a given port.
void ubx_closeConnection(int handle)
Close a connection opened with ubx_openConnection()
bool ubx_setBaudRate(const int handle, const uint32_t baud)
Send UBX port configuration to switch baud rate.
bool ubx_check_checksum(const ubx_message *msg)
Verify checksum bytes of UBX message.
Internal representation of a UBX message.
uint8_t csumB
Checksum part B.
uint8_t sync1
Should always be 0xB5.
uint8_t sync2
Should always be 0x62.
uint8_t data[256]
Data if length <= 256.
uint8_t msgClass
A value from ubx_class.
uint8_t msgID
Message ID byte.
uint16_t length
Message length.
uint8_t csumA
Checksum part A.