Receives and outputs standard output of Python 2 and Python 3> C implementations

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

Overview

--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.

Implementation

C> Standard output every 1 second

-Raspberry Pi> C> Get up to msec (milliseconds) of local time and output standard> gettimeofday () used version and clock_gettime () used version

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);
	}
}

Python 2> Receives standard output and outputs standard output

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

Compile and run

-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 **

Remarks

--sys.stdin did not work as expected

Python 3> Receives standard output and outputs standard output

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

Receives and outputs standard output of Python 2 and Python 3> C implementations
UnicodeEncodeError struggle with standard output of python3
Python version check V outputs standard error output
python input and output
Basics of python: Output
Calculation of standard deviation and correlation coefficient in Python
Fibonacci and prime implementations (python)
Source installation and installation of Python
[Python of Hikari-] Chapter 08-03 Module (Import and use of standard library)
Coordination of each process in MPI and buffering of standard output
Environment construction of python and opencv
The story of Python and the story of NaN
Installation of SciPy and matplotlib (Python)
[Understanding / application of with syntax] Flexible switching of Python standard output destination
[Introduction to Python] I compared the naming conventions of C # and Python.
Read the standard output of a subprocess line by line in Python
Make standard output non-blocking in Python
[Code] Module and Python version output
This and that of python properties
Export and output files in Python
Coexistence of Python2 and 3 with CircleCI (1.0)
Summary of Python indexes and slices
Reputation of Python books and reference books
Paiza Skill Check List of Frequently Used D and C Ranks ~ Python ~
Python Paiza-Various skill checks and standard input
Installation of Visual studio code and installation of python
Calculate and display standard weight with python
I compared python3 standard argparse and python-fire
Output tree structure of files in Python
Extraction of tweet.js (json.loads and eval) (Python)
Implement FIR filters in Python and C
Connect a lot of Python or and and
Write O_SYNC file in C and Python
Socket communication by C language and Python
[python] Difference between rand and randn output
Easy introduction of python3 series and OpenCV3
[Python] Various combinations of strings and values
Idempotent automation of Python and PyPI setup
Full understanding of Python threading and multiprocessing
RaspberryPi L Chika with Python and C #
[Python] Chapter 02-03 Basics of Python programs (input / output)
Project Euler # 1 "Multiples of 3 and 5" in Python
Exchange encrypted data between Python and C #
[Python] From morphological analysis of CSV data to CSV output and graph display [GiNZA]
Competitive Pro with Python and VSCode-Simplification of standard input and automation of sample case judgment-
Output formatted output in Python, such as C / C ++ printf.
How to generate permutations in Python and C ++
Summary of the differences between PHP and Python
Read JSON with Python and output as CSV
Specifying the range of ruby and python arrays
Installation of Python3 and Flask [Environment construction summary]
Compare the speed of Python append and map
[Python] Chapter 02-01 Basics of Python programs (operations and variables)
Output python log to both console and file
Implementation of TRIE tree with Python and LOUDS
python development environment -use of pyenv and virtualenv-
Links and memos of Python character code strings
Output in the form of a python array
Comparison of R and Python writing (Euclidean algorithm)
How to output "Ketsumaimo" as standard output in Python
I / O related summary of python and fortran