35 #ifndef ROS_EMBEDDED_LINUX_COMMS_H 36 #define ROS_EMBEDDED_LINUX_COMMS_H 47 #include <sys/types.h> 48 #include <sys/socket.h> 49 #include <netinet/in.h> 50 #include <netinet/tcp.h> 54 #define DEFAULT_PORTNUM 11411 65 flags = fcntl(socket, F_GETFL, 0);
67 fcntl(socket, F_SETFL, flags | O_NONBLOCK);
72 struct termios options;
75 struct sockaddr_in serv_addr;
81 printf(
"Opening serial port %s\n", portName);
83 fd = open(portName, O_RDWR | O_NOCTTY | O_NDELAY);
88 perror(
"init(): Unable to open serial port - ");
94 fcntl(fd, F_SETFL, FNDELAY);
97 tcgetattr(fd, &options);
98 cfsetispeed(&options, B57600);
99 cfsetospeed(&options, B57600);
100 options.c_cflag |= (CLOCAL | CREAD);
101 options.c_cflag &= ~PARENB;
102 options.c_cflag &= ~CSTOPB;
103 options.c_cflag &= ~CSIZE;
104 options.c_cflag |= CS8;
105 options.c_cflag &= ~CRTSCTS;
106 options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
107 options.c_iflag &= ~(IXON | IXOFF | IXANY);
108 options.c_oflag &= ~OPOST;
111 tcsetattr(fd, TCSANOW, &options);
118 const char* tcpPortNumString = strchr(portName,
':');
121 if (!tcpPortNumString)
124 strncpy(ip, portName, 16);
128 tcpPortNum = strtol(tcpPortNumString + 1, NULL, 10);
129 strncpy(ip, portName, tcpPortNumString - portName);
132 printf(
"Connecting to TCP server at %s:%ld....\n", ip, tcpPortNum);
135 sockfd = socket(AF_INET, SOCK_STREAM, 0);
138 error(
"ERROR opening socket");
144 rv = setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (
char *)&flag,
sizeof(flag));
147 printf(
"Couldn't setsockopt(TCP_NODELAY)\n");
152 server = gethostbyname(ip);
155 fprintf(stderr,
"ERROR, no such host\n");
158 bzero((
char *) &serv_addr,
sizeof(serv_addr));
159 serv_addr.sin_family = AF_INET;
160 bcopy((
char *)server->h_addr,
161 (
char *)&serv_addr.sin_addr.s_addr,
163 serv_addr.sin_port = htons(tcpPortNum);
164 if (connect(sockfd, (
struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
165 error(
"ERROR connecting");
167 printf(
"connected to server\n");
178 rv = read(fd, &c, 1);
184 if ((errno != EAGAIN) && (errno != EWOULDBLOCK))
185 perror(
"elCommRead() error:");
197 while (totalsent < length)
199 rv = write(fd, data + totalsent, length);
201 perror(
"write(): error writing - trying again - ");
void error(const char *msg)
void set_nonblock(int socket)
int elCommWrite(int fd, uint8_t *data, int len)
ros::ServiceServer< Test::Request, Test::Response > server("test_srv",&svcCallback)
int elCommInit(const char *portName, int baud)