In 1842, Barnes was assigned to Fort Jesup, Louisiana, where he remained until 1846 when with the beginning of the Mexican–American War, he joined the 2nd U.S. Dragoons en route to Corpus Christi to join the army being mobilized for the invasion of Mexico from the north. He served with the cavalry column of Major General Zachary Taylor's army during its advance to Monterey. Later transferred to Lieutenant General Winfield Scott's forces before Veracruz, he served with Major General William J. Worth's division during the siege and capture of that city. During the advance upon Mexico City, he was chief surgeon of the cavalry brigade and participated in the battles of Cerro Gordo, Contreras, Churubusco, and Molina del Rey, in the storming of Chapultepec and the capture of the capital.