Raspberry Pi 2 Model B (Below RPi)
Raspbian Jessie
Python 2.7.9
Python 3.4.2
gcc (Raspbian 4.9.2-10) 4.9.2
--Standard output every second with C implementation --Receive the standard output and output it as standard in Python
I am planning to implement I2C communication in C and send the result to RS-232C in Python using pySerial. Preliminary investigation for that.
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> print> Redirected only at the end of processing?> Execute with -u
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,
Standard output is performed every second.
** Note: raw_input () is used in Python 2 **
--sys.stdin
did not work as expected
Python 3 uses input () instead of raw_input ().
--Reference: Introduction to Python for Science and Technology Calculation by Kenji Nakaku --Table 1.4 on p14 Example of differences between Python 2 series and Python 3 series
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