【发布时间】:2025-09-16 18:15:02
【问题描述】:
所以我在我的 beaglebone 上连接了一个 RTC,它可以通过 cat'ing /sys/class/rtc/rtx(x)/time file 正常工作,但是我用于监控时间的 C 代码有一个我似乎无法解决的错误。
if ((rtc_fd = open(RTC, O_RDONLY, 444)) < 0)
REPORT_ERROR("open(RTC)");
其中 RTC 是/dev/rtc1 的路径。 REPORT_ERROR 是一个用于报告自定义错误的宏函数。
无论如何,我在 for 循环之前运行此代码,循环 10 次,它正在输出到日志文件。我总是收到strerror(perror) 消息:
设备或资源繁忙
但它仍然继续给我 10 个正确的输出。
我最后也使用close()。
什么给了?
编辑:也许我应该补充一点,这是在一个守护进程中运行,我在循环期间使用iocotl() 和RTC_RD_TIME。
#define REPORT_ERROR(X) do {\
fprintf(log,"err@ "X": %s@ %s:%d\n",\
strerror(errno), __FILE__, __LINE__ - 1);\
exit(EXIT_FAILURE);\
} while(0)
#define RTC "/dev/rtc1"
int main(void)
{
int rtc_fd;
FILE *log;
struct rtc_time tm;
if ((log = fopen(LOG_FILE, "a+")) == NULL)
exit(EXIT_FAILURE);
if ((dup2(fileno(log), STDERR_FILENO)) < 0)
REPORT_ERROR("dup2()");
if ((rtc_fd = open(RTC, O_RDONLY, 444)) < 0)
REPORT_ERROR("open(RTC)");
/* Main loop */
for (int i = 0; i < 10; ++i)
{
if ((ioctl(rtc_fd, RTC_RD_TIME, &rtctime)) != 0)
REPORT_ERROR("ioctl(rtc_fd)");
fprintf(log, "%02d:%02d:%02.lf %d-%d-%d\n", tm.hour, tm.minute,
tm.second, tm.mon + 1, tm.mday, tm.year + 1900);
sleep(1);
}
close(rtc_fd);
return 0;
}
输出:
err@ open(RTC): Device or resource busy@ ha-daemon.c:80
05:06:09 2-12-2015
05:06:10 2-12-2015
05:06:11 2-12-2015
05:06:12 2-12-2015
05:06:13 2-12-2015
05:06:14 2-12-2015
05:06:15 2-12-2015
05:06:16 2-12-2015
05:06:17 2-12-2015
05:06:18 2-12-2015
【问题讨论】:
-
请创建一个Minimal, Complete, and Verifiable Example 并展示给我们。
-
您是否尝试过全局打开您的设备?
-
至于您的实际问题(打开设备),您是否确定没有其他进程已经打开完全相同的设备?
-
@JoachimPileborg,文件 *log 不是未定义的,我只是没有剪掉那个代码,现在是。我想有人可能会在没有看到这么多代码的情况下知道这个问题的原因,毕竟我在我的问题中确实说过在初始错误消息之后输出是正确的..
-
@Shehryar 我不确定您在全球范围内打开设备是什么意思?如果我 su 比 cat /sys/class/rtc/rtc1/time 我可以得到时间。
标签: c linux embedded-linux beagleboneblack beagleboard