Raspberry Pi 2 Model B (Unterhalb von RPi)
Raspbian Jessie
Python 2.7.9
Python 3.4.2
gcc (Raspbian 4.9.2-10) 4.9.2
Ich denke darüber nach, die I2C-Kommunikation in C zu implementieren und das Ergebnis mit pySerial an RS-232C in Python zu senden. Voruntersuchung dazu.
tickTime_180801.c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <time.h>
static void printTime_clock_gettime(bool addLF)
{
struct timespec tvToday; // for msec
struct tm *ptm; // for date and time
clock_gettime(CLOCK_REALTIME_COARSE, &tvToday);
ptm = localtime(&tvToday.tv_sec);
// date and time
printf("%04d/%02d/%02d,%02d:%02d:%02d,",
ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday,
ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
// msec
printf("%03d,", (uint16_t)(tvToday.tv_nsec / 1000000));
if (addLF) {
printf("\n");
}
}
int main(void)
{
struct timespec req;
int loop;
req.tv_sec = 1;
req.tv_nsec = 0;
for(loop=0; loop<5; loop++) {
printTime_clock_gettime(/*addLF=*/true);
fflush(stdout);
nanosleep(&req, NULL);
}
}
redirect_180801.py
# v0.1 Aug. 01, 2018
# - break when [EOFError]
# - read using raw_input()
# on Python 2.7.9
while True:
try:
res = raw_input()
print(res)
except EOFError:
break
python
$ gcc tickTime_180801.c -o tickTime_180801
$ ./tickTime_180801 | python2 -u redirect_180801.py
2018/08/01,18:06:11,972,
2018/08/01,18:06:12,982,
2018/08/01,18:06:13,982,
2018/08/01,18:06:14,982,
2018/08/01,18:06:15,982,
Die Standardausgabe erfolgt jede Sekunde.
** Hinweis: raw_input () wird in Python 2 verwendet **
--sys.stdin
hat nicht wie erwartet funktioniert
Python 3 verwendet input () anstelle von raw_input ().
redirect_py3_180801.py
# v0.1 Aug. 01, 2018
# - break when [EOFError]
# - read using raw_input()
# on Python 3.4.2
while True:
try:
res = input()
print(res)
except EOFError:
break
run
$ ./tickTime_180801 | python3 -u redirect_py3_180801.py
2018/08/01,18:37:13,143,
2018/08/01,18:37:14,153,
2018/08/01,18:37:15,153,
2018/08/01,18:37:16,153,
2018/08/01,18:37:17,163,
Recommended Posts