As the core of intelligent manufacturing, cyber-physical systems (CPS) have serious security issues, especially for the communication security of their terminal machine-to-machine (M2M) communications. In this paper, blockchain technology is introduced to address such a security problem of communications between different types of machines in the CPS. According to the principles of blockchain technology, we designed a blockchain for secure M2M communications. As a communication system, M2M consists of public network areas, device areas, and private areas, and we designed a sophisticated blockchain structure between the public area and private area. For validating our design, we took cotton spinning production as a case study to demonstrate our solution to M2M communication problems under the CPS framework. We have demonstrated that the blockchain technology can effectively solve the safety of expansion of machines in the production process and the communication data between the machines cannot be tampered with.