gpt4 book ai didi

通过串行接口(interface)用c++连接到cisco路由器

转载 作者:行者123 更新时间:2023-11-30 17:56:54 26 4
gpt4 key购买 nike

我想从我的 C++ 应用程序连接到 Cisco 路由器。需要它才能获取接口(interface)状态。我的linux站(Ubuntu)和路由器通过串行接口(interface)连接。

从 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)做同样的事情,但是读到了hang。

我的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;
}

最佳答案

在修改 port_settings 结构之前,您没有先将其清零。这肯定是一个错误,尽管它可能不是问题的根源。您是否尝试过构建互联网上可用的数十个“termios 示例程序”之一以进行比较?

关于通过串行接口(interface)用c++连接到cisco路由器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13125526/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com