0
$\begingroup$

I am trying to get some data out from the proximity sensor through USB UART converter. I don't know why but when I just run the code on mac OS with the similar code, it perfectly works fine. I would like to check if I am mistaken with about my code written in ROS2.

The only log I get when running on ROS2 is,

parallels@ubuntu-linux-22-04-02-desktop:~/agv_control$ ros2 run proximity_sense uart_sensor_node
[INFO] [1699503004.001873011] [uart_sensor_node]: UART Sensor Node has been started

I was expecting to obtain the sensor values next, but it doesn't gives anything by then.

The code I've been running on mac is,

import serial


def main(args=None):
    while (1):
        serial_port = serial.Serial('/dev/tty.usbserial-1120', baudrate=9600, timeout=1)
        serial_port.write(b'\x80\x06\x03\x77')
        data = serial_port.read(10)

        if data[3:] != b'':
            print(float(data[3:]))



if __name__ == '__main__':
    main()

However, when I run the below code with ROS2-humble on Ubuntu, the data I receive is only b''. The ROS2 code is

import rclpy
from rclpy.node import Node
import serial
from std_msgs.msg import String

class UARTSensorNode(Node):
    def __init__(self):
        super().__init__('uart_sensor_node')
        self.serial_port = serial.Serial('/dev/ttyUSB0', baudrate=9600, timeout=1)
        self.get_logger().info('UART Sensor Node has been started')
        self.prox_sen_pub = self.create_publisher(String, 'sensor_data', 10)
        self.timer = self.create_timer(0.5, self.receive_sensor_data)

    def receive_sensor_data(self):
        data = String()
        self.serial_port.write(b'\x80\x06\x03\x78')
        data = self.serial_port.read(10)
    if data[3:] != b'':
        self.get_logger().info('Distance: "%s"' % data[3:])
        self.prox_sen_pub.publish(data[3:])

def main(args=None):
    rclpy.init(args=args)
    dist_pub = UARTSensorNode()
    rclpy.spin(dist_pub)
    rclpy.shutdown()

if __name__ == '__main__':
    main()

Thanks in advance.

$\endgroup$
0

2 Answers 2

0
$\begingroup$

One thought: Have you verified your serial port permissions are good?

List permissions

ls -l /dev

On my PC I get this:

crw-rw----  1 root dialout 188,   0 Nov  8 21:46 ttyUSB0

which tells me the port isn't accessible.

running

sudo chmod a=rwx /dev/ttyUSB0

gets me to

crwxrwxrwx  1 root dialout 188,   0 Nov  8 21:59 ttyUSB0

and now I can access the port. This was an issue for me on 16. These commands are all still valid in 22.04 but maybe Google will show you an easier way to make sure the serial port is accessible.

$\endgroup$
1
  • $\begingroup$ Hi Billy, Thanks for the suggestion. Unfortunately, I get the same issue, though following your commands enabled me to "crwxrwxrwx 1 root dialout 188, 0 Nov 9 15:02 ttyUSB0." $\endgroup$
    – Bob
    Commented Nov 9, 2023 at 7:03
0
$\begingroup$

Two things: with the MAC code the msg ends by 0x77, in Ubuntu with 0x78: can it be a problem?

Second, if you skip the python code, you can do this on two shells:

shell1: cat /dev/ttyUSB0 >> just_a_dump_file.txt (this will put the serial read in the txt file)

shell2: stty -F /dev/ttyUSB0 9600 && echo -en '\x80\x06\x03\x78' (this is the equivalent of your write)

then you can read the content of the file (probably you need to use xxd)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.