In 2016 — three years after Mr. Mottor was pulled over — the state’s highest court ruled that Massachusetts had to hand over two Alcotest machines. The defense lawyers would be allowed to hire experts to test them.

The decision caused paralysis. Prosecutors froze thousands of cases until the review was finished.

The software experts and scientists who inspected the Alcotest 9510 machines found troubling mistakes, according to their reports to the court. In some circumstances — when the devices’ two testing methods produced substantially different results, for example — the machines were supposed to generate error messages and terminate the test. Instead, the devices printed a result. (Dräger blamed an error by its computer programmers, which it said has now been fixed.)

But the machines weren’t the only problem. The Massachusetts forensic lab, which for years had been plagued by scandals over faked drug test results and tampered evidence, lacked a written procedure to set up and test machines, the lab’s technical director testified.

The justice hearing the case, Robert A. Brennan, said the lab could not prove that it had followed a “scientifically sound methodology,” and in 2017 he threw out all of its breath test results from 2012 through 2014.

That was only the beginning. Lawyers soon discovered that the lab had hidden records of hundreds of failed calibrations. The discovery provoked a state investigation that blasted the lab’s leadership for “serious errors of judgment.”

Justice Brennan later expanded his previous order: No tests from the lab were admissible until it was accredited by a national board that oversees forensic labs. Eight years of tests — more than 36,000, according to defense lawyers — were suddenly off-limits.

Mr. Mottor’s trial finally got underway on Jan. 10, 2018. He and his lawyer didn’t realize that his breath test was among those that had been invalidated. It remained the state’s crucial piece of evidence.