linux c++获取当前程序的运行路径

比如我的程序名为:aaa
存放路径是:/homo/code/
我在/home/ccc 目录执行shell文件。shell文件的内容为

#!/bin/bash
/homo/code/aaa

希望获取的路径是 /homo/code/ 而不是脚本的路径

给出完整接口代码

#include <iostream>
#include <string>
#include <string.h>
#include <unistd.h>

#define MAX_PATH_LEN  256

bool getCurrRunningPath(std::string &currPath)
{
    char path[MAX_PATH_LEN] = {0};
    char *p = NULL;
    ssize_t n = readlink("/proc/self/exe", path, MAX_PATH_LEN);
    if (n > 0) {
        p = strrchr(path, '/');
        *(p + 1) = '\0'; // 去掉最后的程序名称
        currPath.assign(path);
        std::cout << "get current running path:" << path << std::endl;
        return true;
    } else {
        std::cout << "get current running path failed, errno: " << errno << std::endl;
    }

    return false;
}

int main()
{
 std::string curpath;
 getCurrRunningPath(curpath);
 std::cout << "***: " << curpath << std::endl;

 return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要编写一个Linux C的串口接收代码,需要首先设置串口参数,然后进行数据的接收。 首先,我们可以使用`open()`函数打开串口设备文件,例如`/dev/ttyS0`。打开串口设备需要使用O_RDWR模式。 ```c int fd = open("/dev/ttyS0", O_RDWR); ``` 接下来,我们需要设置串口的属性,包括波特率、数据位、停止位和校验位等。我们可以使用`termios`结构和`tcsetattr()`函数来设置。 ```c struct termios options; tcgetattr(fd, &options); // 获取当前串口属性 cfsetispeed(&options, B9600); // 设置波特率为9600 cfsetospeed(&options, B9600); options.c_cflag &= ~CSIZE; // 清除数据位 options.c_cflag |= CS8; // 设置数据位为8位 options.c_cflag &= ~PARENB; // 不使用奇偶校验位 options.c_cflag &= ~CSTOPB; // 设置停止位为1位 tcsetattr(fd, TCSANOW, &options); // 设置属性 ``` 设置完串口属性后,我们可以使用`read()`函数从串口中读取数据。如果读取成功,`read()`函数将返回读取的字节数,我们可以将读取的数据存储在一个缓冲区中进行后续处理。 ```c char buffer[255]; int bytesRead = read(fd, buffer, sizeof(buffer)); if (bytesRead > 0) { // 处理读取到的数据 } ``` 最后,当不再需要使用串口时,需要使用`close()`函数关闭串口。 ```c close(fd); ``` 以上就是一个简单的Linux C串口接收代码示例。通过设置串口属性和使用`read()`函数,我们可以实现串口数据的接收。 ### 回答2: Linux C 串口接收代码如下: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <unistd.h> #include <termios.h> int main() { int fd; char buffer[255]; // 打开串口 fd = open("/dev/ttyS0", O_RDWR); if (fd == -1) { perror("无法打开串口"); return -1; } // 配置串口 struct termios serial; tcgetattr(fd, &serial); serial.c_cflag = B9600 | CS8 | CREAD | CLOCAL; serial.c_iflag = 0; serial.c_oflag = 0; serial.c_lflag = 0; tcflush(fd, TCIFLUSH); tcsetattr(fd, TCSANOW, &serial); // 读取并打印串口数据 while (1) { memset(buffer, 0, sizeof(buffer)); if (read(fd, buffer, sizeof(buffer)) > 0) { printf("接收到的数据:%s\n", buffer); } } // 关闭串口 close(fd); return 0; } ``` 上述代码首先打开了串口设备 "/dev/ttyS0",如果打开失败,则打印错误信息并退出。接下来,使用 `tcgetattr` 函数获取串口的属性配置,并对其进行修改,设置波特率为 9600bps,数据位为 8 位,启用接收功能。然后,使用 `tcsetattr` 函数将修改后的属性配置应用到串口设备上。 随后,通过一个无限循环,不断读取串口数据,并将其打印出来。在每次读取前,将 `buffer` 数组清零以保证接收到的数据不会受到上一次数据的干扰。 最后,关闭串口设备并返回 0 表示程序运行结束。 ### 回答3: Linux C串口接收代码通常需要使用Linux系统提供的串口设备驱动和相关函数库来实现,具体步骤如下: 1. 在程序中包含相关的头文件: ```c #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <termios.h> ``` 2. 打开串口设备: ```c int fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY); if (fd == -1) { perror("无法打开串口"); exit(1); } ``` 其中,`/dev/ttyS0`是串口设备文件路径,需要根据实际情况进行调整。 3. 配置串口参数: ```c struct termios options; tcgetattr(fd, &options); cfsetispeed(&options, B9600); // 设置波特率 cfsetospeed(&options, B9600); options.c_cflag &= ~CSIZE; // 设置数据位数为8位 options.c_cflag |= CS8; options.c_cflag &= ~PARENB; // 设置无校验 options.c_cflag &= ~CSTOPB; // 设置停止位为1个停止位 tcsetattr(fd, TCSANOW, &options); ``` 其中,`B9600`是波特率,`CS8`表示8位数据位,`PARENB`表示启用奇偶校验,`CSTOPB`表示2个停止位。可以根据串口的实际配置进行调整。 4. 接收数据: ```c char buffer[255]; int len = read(fd, buffer, sizeof(buffer)); if (len == -1) { perror("读取数据失败"); exit(1); } else if (len > 0) { buffer[len] = '\0'; printf("接收到的数据:%s\n", buffer); } ``` 使用read函数从串口设备中读取数据,并将数据存储在缓冲区buffer中。可以根据实际需求设置缓冲区的大小。 5. 关闭串口设备: ```c close(fd); ``` 处理完数据后,使用close函数关闭串口设备。 以上就是使用Linux C编写串口接收代码的主要步骤。根据实际需求,可能还需要添加异常处理、数据解析和业务逻辑等部分代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值