我想从我的 c++ 应用程序连接到 Cisco 路由器。需要它才能获得接口状态。我的 linux 站(Ubuntu)和路由器通过串行接口连接。
从 puty 或 minicom 或控制台连接工作连接。
例如:
root@test:/etc/minicom# cu -l /dev/ttyS0 -s 9600
Connected.
Router#show int summary
*: interface is up
IHQ: pkts in input hold queue IQD: pkts dropped from input queue
OHQ: pkts in output hold queue OQD: pkts dropped from output queue
RXBS: rx rate (bits/sec) RXPS: rx rate (pkts/sec)
TXBS: tx rate (bits/sec) TXPS: tx rate (pkts/sec)
TRTL: throttle count
现在我尝试对 C++ (或 C) 做同样的事情,但读挂起。
我的c代码:
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>
#include <iostream>
#include<stdio.h>
#include<sys/ioctl.h>
#include<unistd.h>
#include<fcntl.h>
using namespace std;
int fd1;
int fd2;
char *buff, *buffer, *bufptr;
int wr, rd, nbytes, tries;
int configure_port(int fd) // configure the port
{
struct termios port_settings; // structure to store the port settings in
bzero(&port_settings, sizeof(port_settings));
cfsetispeed(&port_settings, B9600); // set baud rates
cfsetospeed(&port_settings, B9600);
port_settings.c_cflag &= ~PARENB; // set no parity, stop bits, data bits
port_settings.c_cflag &= ~CSTOPB;
port_settings.c_cflag &= ~CSIZE;
port_settings.c_cflag |= CS8;
tcsetattr(fd, TCSANOW, &port_settings); // apply the settings to the port
return (fd);
}
int main() {
fd1 = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd1 == -1) {
perror("open_port: Unable to open /dev/ttyS0 – ");
} else {
fcntl(fd1, F_SETFL, 0);
}
printf("Port 1 has been sucessfully opened and %d is the file description\n",fd1);
configure_port(fd1);
wr = write(fd1, "\r", 1);
cout << " wr status " << wr << endl;
wr = write(fd1, "\r", 1);
cout << " wr status " << wr << endl;
wr = write(fd1, "ena\r", 4);
cout << " wr status " << wr << endl;
wr = write(fd1, "show int sum\r", 13);
cout << " wr status " << wr << endl;
rd = read(fd1, buff, 50);
cout << " rd status " << rd << endl;
cout << rd << endl;
return 0;
}