git commit -m "first commit for v2"
This commit is contained in:
43
Devices/Libraries/Ros/libserial/include/libserial/define.h
Executable file
43
Devices/Libraries/Ros/libserial/include/libserial/define.h
Executable file
@@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
|
||||
#define OFF 0
|
||||
#define ON 1
|
||||
#define MAX_MSG_LENGTH 2048
|
||||
|
||||
typedef enum parity {
|
||||
PARITY_NONE,
|
||||
PARITY_EVEN,
|
||||
PARITY_ODD
|
||||
} parity_t;
|
||||
|
||||
typedef enum stop_bits {
|
||||
STOPBIT_1,
|
||||
STOPBIT_2
|
||||
} stop_bits_t;
|
||||
|
||||
typedef enum data_bits {
|
||||
DATABIT_5,
|
||||
DATABIT_6,
|
||||
DATABIT_7,
|
||||
DATABIT_8
|
||||
} data_bits_t;
|
||||
|
||||
struct _rs485 {
|
||||
/* Socket or file descriptor */
|
||||
int s;
|
||||
/* Device: "/dev/ttyS0", "/dev/ttyUSB0" or "/dev/tty.USA19*" on Mac OS X. */
|
||||
const char* _port;
|
||||
/* Bauds: 9600, 19200, 57600, 115200, etc */
|
||||
unsigned int _baud;
|
||||
/* Data bit */
|
||||
data_bits_t _data_bit;
|
||||
/* Stop bit */
|
||||
stop_bits_t _stop_bit;
|
||||
/* Parity:*/
|
||||
parity_t _parity;
|
||||
/* Save old termios settings */
|
||||
struct termios _old_tios;
|
||||
};
|
||||
|
||||
|
||||
|
||||
88
Devices/Libraries/Ros/libserial/include/libserial/rs485.h
Executable file
88
Devices/Libraries/Ros/libserial/include/libserial/rs485.h
Executable file
@@ -0,0 +1,88 @@
|
||||
//
|
||||
// Created by Hiep on 5/12/2020.
|
||||
//
|
||||
#pragma once
|
||||
#include <ros/ros.h>
|
||||
#include "serial.h"
|
||||
|
||||
typedef struct _rs485 rs485_t;
|
||||
|
||||
class rs485: public Serial
|
||||
{
|
||||
private:
|
||||
/**
|
||||
* Configure
|
||||
* @param devfile Path name of dev
|
||||
* @param baud Baudrate
|
||||
* @param parity Parity
|
||||
* @param data_bit
|
||||
* @param stop_bit
|
||||
*/
|
||||
virtual int _rs485_connect(rs485_t &ctx);
|
||||
|
||||
/**
|
||||
* create new port
|
||||
* @param devfile Path name of dev
|
||||
* @param baud Baudrate
|
||||
* @param parity Parity
|
||||
* @param data_bit
|
||||
* @param stop_bit
|
||||
*/
|
||||
virtual void new_port(const char* devfile, unsigned int baud, parity_t parity,
|
||||
data_bits_t data_bit,stop_bits_t stop_bit);
|
||||
|
||||
public:
|
||||
/**
|
||||
* Data Sender
|
||||
* @param to_send Request to Be Sent to Server
|
||||
* @param length Length of the Request
|
||||
* @return Size of the request
|
||||
*/
|
||||
virtual ssize_t sendMsgs(uint8_t *to_send, uint16_t length);
|
||||
|
||||
/**
|
||||
* Data Receiver
|
||||
* @param buffer Buffer to Store the Data` Retrieved
|
||||
* @return Size of Incoming Data
|
||||
*/
|
||||
virtual ssize_t receiveMsgs(uint8_t *buffer) const;
|
||||
|
||||
/*
|
||||
* Reconnect to device if rs485 modules was disconected.
|
||||
*/
|
||||
virtual void reconnect(void);
|
||||
|
||||
/*
|
||||
* Set read character miximum
|
||||
* @param[in] num The character minximum to receive.
|
||||
*/
|
||||
virtual int setCharacterNumToRead(int num);
|
||||
|
||||
/**
|
||||
* Close serial port
|
||||
*/
|
||||
virtual void close_port();
|
||||
|
||||
/**
|
||||
* @brief Constructor
|
||||
* @param[in] devfile Example /dev/tty*
|
||||
* @param[in] baud Number of transmitted bits per a second
|
||||
* @param[in] parity The parity check bit {Even, None , Old }
|
||||
* @param[in] data_bit Number of bits in a transmission frame
|
||||
* @param[in] stop_bit End bit
|
||||
*/
|
||||
rs485(const char* devfile, unsigned int baud, parity_t parity,
|
||||
data_bits_t data_bit,stop_bits_t stop_bit);
|
||||
/**
|
||||
* Destructor.
|
||||
*/
|
||||
virtual ~rs485();
|
||||
/* Properties */
|
||||
protected:
|
||||
rs485_t ctx;
|
||||
uint8_t size_pkg;
|
||||
float param_safety;
|
||||
public:
|
||||
bool _connected{}; /* Write status connection of devices */
|
||||
std::string error_msg; /* To saved error message */
|
||||
};
|
||||
44
Devices/Libraries/Ros/libserial/include/libserial/serial.h
Executable file
44
Devices/Libraries/Ros/libserial/include/libserial/serial.h
Executable file
@@ -0,0 +1,44 @@
|
||||
//
|
||||
// Created by Hiep on 4/12/2020.
|
||||
//
|
||||
#pragma once
|
||||
// #include <bits/stdc++.h>
|
||||
#include <iostream> /* std::cout */
|
||||
#include <stdio.h> /* Standard input/output definitions */
|
||||
#include <string.h> /* String function definitions */
|
||||
#include <unistd.h> /* UNIX standard function definitions */
|
||||
#include <fcntl.h> /* File control definitions */
|
||||
#include <errno.h> /* Error number definitions */
|
||||
#include <termios.h> /* POSIX terminal control definitions */
|
||||
#include <cstdint> /* uin8_t */
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include "define.h"
|
||||
|
||||
class Serial
|
||||
{
|
||||
public:
|
||||
/*
|
||||
* @brief Setting baudrate
|
||||
* @param[in] b baud speed input
|
||||
*/
|
||||
virtual speed_t setBaudRate(unsigned int b);
|
||||
|
||||
/*
|
||||
* @brief Setting parity bits type
|
||||
* @param[in] b baud speed input
|
||||
*/
|
||||
virtual void setParity(termios &tios, parity p);
|
||||
|
||||
/*
|
||||
* @brief Setting stop bits type
|
||||
* @param[in] b baud speed input
|
||||
*/
|
||||
virtual void setStopBits(termios &tios, stop_bits s);
|
||||
|
||||
/*
|
||||
* @brief Setting data bits type
|
||||
* @param[in] b baud speed input
|
||||
*/
|
||||
virtual void setDataBits(termios &tios, data_bits d);
|
||||
};
|
||||
119
Devices/Libraries/Ros/libserial/include/libserial/udevadm_info.h
Executable file
119
Devices/Libraries/Ros/libserial/include/libserial/udevadm_info.h
Executable file
@@ -0,0 +1,119 @@
|
||||
#ifndef __UDEVADM_INFO_H_INCLUDE__
|
||||
#define __UDEVADM_INFO_H_INCLUDE__
|
||||
|
||||
#include <ros/ros.h>
|
||||
#include <unistd.h>
|
||||
#include <poll.h>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
#include <sys/signalfd.h>
|
||||
#include <csignal>
|
||||
#include <libudev.h>
|
||||
#include <string>
|
||||
|
||||
class udevadmInfo {
|
||||
public:
|
||||
udevadmInfo(const char *product_name);
|
||||
virtual ~udevadmInfo(void);
|
||||
virtual int init();
|
||||
virtual bool scanDevices(void);
|
||||
const char *port;
|
||||
protected:
|
||||
struct udev *udev;
|
||||
char *product_name_;
|
||||
};
|
||||
|
||||
udevadmInfo::udevadmInfo(const char *product_name)
|
||||
: product_name_((char *)product_name)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
udevadmInfo::~udevadmInfo(void){
|
||||
udev_unref(udev);
|
||||
}
|
||||
|
||||
int udevadmInfo::init() {
|
||||
udev = udev_new();
|
||||
if (!udev) {
|
||||
printf("Error while initialization!\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
sigset_t mask;
|
||||
|
||||
// Set signals we want to catch
|
||||
sigemptyset(&mask);
|
||||
sigaddset(&mask, SIGTERM);
|
||||
sigaddset(&mask, SIGINT);
|
||||
|
||||
// Change the signal mask and check
|
||||
if (sigprocmask(SIG_BLOCK, &mask, nullptr) < 0) {
|
||||
ROS_ERROR("UDEV-Error while sigprocmask(): %s\n", std::strerror(errno));
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
// Get a signal file descriptor
|
||||
int signal_fd = signalfd(-1, &mask, 0);
|
||||
// Check the signal file descriptor
|
||||
if (signal_fd < 0) {
|
||||
ROS_ERROR("UDEV-Error while signalfd(): %s\n", std::strerror(errno));
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
bool udevadmInfo::scanDevices(void) {
|
||||
struct udev_device *device;
|
||||
struct udev_enumerate *enumerate;
|
||||
struct udev_list_entry *devices, *dev_list_entry;
|
||||
|
||||
// Create enumerate object
|
||||
enumerate = udev_enumerate_new(udev);
|
||||
if (!enumerate) {
|
||||
ROS_ERROR("Error while creating udev enumerate\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Scan devices
|
||||
udev_enumerate_scan_devices(enumerate);
|
||||
|
||||
// Fill up device list
|
||||
devices = udev_enumerate_get_list_entry(enumerate);
|
||||
if (!devices) {
|
||||
ROS_ERROR("Error while getting device list\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
udev_list_entry_foreach(dev_list_entry, devices) {
|
||||
const char* path = udev_list_entry_get_name(dev_list_entry);
|
||||
// Get the device
|
||||
device = udev_device_new_from_syspath(udev, udev_list_entry_get_name(dev_list_entry));
|
||||
// Print device information
|
||||
if(udev_device_get_devnode(device) != NULL && strstr(udev_device_get_sysname(device),"ttyUSB") != NULL) {
|
||||
// ROS_INFO("DEVNODE=%s", udev_device_get_devnode(device));
|
||||
struct udev_device* usb = udev_device_get_parent_with_subsystem_devtype(device,"usb","usb_device");
|
||||
// ROS_INFO("usb = %s:%s, manufacturer = %s, product = %s, serial = %s, speed = %s, bMaxPower = %s",
|
||||
// udev_device_get_sysattr_value(usb, "idVendor"),
|
||||
// udev_device_get_sysattr_value(usb, "idProduct"),
|
||||
// udev_device_get_sysattr_value(usb, "manufacturer"),
|
||||
// udev_device_get_sysattr_value(usb, "product"),
|
||||
// udev_device_get_sysattr_value(usb, "serial"),
|
||||
// udev_device_get_sysattr_value(usb, "speed"),
|
||||
// udev_device_get_sysattr_value(usb, "bMaxPower"));
|
||||
if(strstr(udev_device_get_sysattr_value(usb, "product"), (const char *)product_name_) != NULL) {
|
||||
port = udev_device_get_devnode(device);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
continue;
|
||||
}
|
||||
// Free the device
|
||||
udev_device_unref(device);
|
||||
}
|
||||
// Free enumerate
|
||||
udev_enumerate_unref(enumerate);
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user