Permalink
Browse files

working good

added a debug option
  • Loading branch information...
1 parent fd469bf commit 3284155cdcc9d830afc812f254afb8592ec0ee1d kevin committed Mar 4, 2012
Showing with 38 additions and 3 deletions.
  1. +3 −2 README.md
  2. +35 −1 src/serial_node_service.cpp
View
@@ -12,17 +12,18 @@ Simple node that connects to a serial port and launches a ROS service. Ideally I
## Command Line
- rosrun serial_port serial_port <uC> <port> <baud>
+ rosrun serial_port serial_port <uC> <port> <baud> <debug>
* uC: a number that gets appended to the service topic incase there are multiple nodes running at once … 0, 1, 2, 3, …
* port: the serial port
* baud: the baud rate to connect at: 9600, …, 57600, 115200
+* debug: enable debugging info printing ... just put true if you want it
### Published Services:
**string:** uc<uC>_serial where <uC> was passed as a command line parameter
### Example:
- rosrun serial_port serial_port 0 /dev/cu.usbserial 9600
+ rosrun serial_port serial_port 0 /dev/cu.usbserial 9600 true
## To Do
@@ -49,6 +49,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <queue> // C++ FIFO
+#include <iostream>
#include "serial_node/serial.h"
@@ -60,12 +61,15 @@ class Serial {
sprintf(sname, "uc%d_serial",i);
name = sname;
service = n.advertiseService(name, &Serial::callback, this);
+ debug = false;
}
~Serial(){
::close(fd);
ROS_INFO("%s stopping", name.c_str());
}
+
+ inline void setDebug(bool b=true){ debug = b; }
inline void flush(void){ //discard data that was not read
tcflush (fd, TCIFLUSH);
@@ -86,6 +90,7 @@ class Serial {
ros::ServiceServer service;
int fd; //serial port file pointer
std::string name;
+ bool debug;
};
// check this!!
@@ -177,6 +182,25 @@ bool getChar(char& c, int fd, const int trys=20){
return false;
}
+inline char makeReadable(char c){
+ return (c < 32 ? '.' : (c > 126 ? '.' : c));
+}
+
+void printMsg(std::string& msg){
+ std::cout<<"---[[ "<<msg[1]<<" ]]-----------------------------------"<<std::endl;
+ std::cout<<"Size data: "<<(int)msg[2]<<"\t"<<"Size Msg: "<<msg.size()<<std::endl;
+ std::cout<<"Start/End characters: "<<msg[0]<<" / "<<msg[msg.size()-1]<<std::endl;
+ int size = (int)msg[2];
+ if(size > 0){
+ std::cout<<"Raw: ";
+ for(int i=0;i<size;i++) std::cout<<(int)msg[3+i]<<' ';
+ std::cout<<std::endl;
+ std::cout<<"ASCII: ";
+ for(int i=0;i<size;i++) std::cout<<makeReadable(msg[3+i])<<' ';
+ std::cout<<std::endl;
+ }
+ std::cout<<"-------------------------------------------"<<std::endl;
+}
bool Serial::readMsg(std::string& ucString, int size, const int trys){
int numread = 0, n = 0, numzeroes = 0;
@@ -228,6 +252,8 @@ bool Serial::readMsg(std::string& ucString, int size, const int trys){
ucString.assign(buf,numread);
+ if(debug) printMsg(ucString);
+
return true;
}
@@ -284,6 +310,7 @@ bool Serial::callback(serial_node::serial::Request& req,
//ROS_INFO("%s command: %s", name.c_str(), req.str.c_str());
Serial::write(req.str.c_str(),req.str.size());
+ //usleep(1000);
//ROS_INFO("send: %s[%d]",req.str.c_str(),req.str.size());
@@ -295,7 +322,8 @@ bool Serial::callback(serial_node::serial::Request& req,
//Serial::flush();
Serial::write(req.str.c_str(),req.str.size());
miss = 0;
- ROS_INFO("Resending: avail[%d]",Serial::available());
+ ROS_INFO("Resending: avail[%d] need[%d]",
+ Serial::available(), req.size);
}
//if(!ros::ok()) return 0;
@@ -322,6 +350,7 @@ bool Serial::callback(serial_node::serial::Request& req,
else{
ROS_ERROR("Error: %s",req.str.c_str());
res.str = "error";
+ Serial::flush();
return false;
}
}
@@ -409,6 +438,11 @@ int main(int argc, char **argv)
ROS_ERROR("ucontroller baud rate parameter invalid");
return 1;
}
+
+ bool debug;
+ if (argc > 4) debug = (strcmp(argv[4],"true") ? false : true);
+ else debug = false;
+ serial.setDebug(debug);
//ROS_INFO("Service %s on %s @ %d baud",serial.name.c_str(), port, baud);

6 comments on commit 3284155

Sir, I am working on 3-D mapping using Kinect, and am looking forward to connect my microcontroller with ROS to get Odometry information. I searched for serial communication on net and found your code. While making the package, serial.h was not found , could you please upload that file. I just started working working with ROS , so completely new to it , please help .Thank you for your time .. really appreciate your work.

Collaborator

walchko replied Mar 22, 2012

This was a strange error. When you download the code (a zip file), it will unpack it and create folder like serial_node-1234. Rename the folder to just serial_node. Python will read the srv/serial.srv and create the serial.h file located in srv_gen/cpp/include/serial_node/serial.h. Took me a little bit to figure this out ... just rename the folder to serial_node and then do "make".

Collaborator

walchko replied Mar 22, 2012

Glad you got it to compile, but this package is still really new. And you are correct, it is a little more complicated than the ReadMe says, I am trying to fix that right now, but am afraid it will take time. There should be an executable under the bin directory and make sure the folder is in your ROS_PATH, so when you do "rosrun serial_node ..." ros can find the executable.

It works for me on Apple's OSX, but I have not tried it on another operating system. Apple must include the standard errors automatically, that is why errno on that error message compiles for me ... I will look into that thanks!

You might want to try rosserial or just do regular serial port commanding using a library. Try searching through the ROS software list for other serial port code. Sorry this isn't working for you.

Collaborator

walchko replied Mar 24, 2012

Yes your parameters look fine as long as /dev/ttyUSB0 is your serial port. If you look at the code, you can see the order I pull parameters from the command line.

No that is not normal behavior ... don't know why.

For rosbridge and others, keep using answers.ros.org to solve those problems. Again, it might just be easier to write your own node that does serial comm. ROS is really for programmers, so brush up on your C++ skills and take a look at the code. Reading the code is the best way to learn how to use this stuff ... good luck!

Please sign in to comment.