Central exclusive and semiexclusive production of pairs is measured with the CMS detector in proton-proton collisions at the LHC at center-of-mass energies of 5.02 and 13TeV. The theoretical description of these nonperturbative processes, which have not yet been measured in detail at the LHC, poses a significant challenge to models. The two pions are measured and identified in the CMS silicon tracker based on specific energy loss, whereas the absence of other particles is ensured by calorimeter information. The total and differential cross sections of exclusive and semiexclusive central production are measured as functions of invariant mass, transverse momentum, and rapidity of the system in the fiducial region defined as transverse momentum and pseudorapidity . The production cross sections for the four resonant channels , , , and are extracted using a simple model. These results represent the first measurement of this process at the LHC collision energies of 5.02 and 13TeV.