In shop floors dominated by programmable logic controllers (PLCs), the implementation of flexible control has bee held back due to difficulties in generation and modification of the PLC code. This paper presents a technique that not only automates the PLC code generation and modification, but also integrate it into a framework of process planning and scheduling. The purpose of this technique is to enable a specific set of software agents to automate the specification, generation, validation, and implementation of discrete shop-floor control systems. The first part of the paper presents a framework of five software agents that interact with each other to plan model and implement flexible manufacturing using current control equipment (i.e.PLCs). The framework specifies the roles and rough communications protocols of each agent. The five agents are: (1) Process planning agent, (2) Scheduling agent, (3) Modelling and simulation agent, (4) Validation and exception handling agent, and (5) PLC language translation agent. The second part of the paper addresses the operations details of each agent. For this purpose, it presents a new technique used to model, validate and generate the PLC code. We named this technique Three Levels Approach (TLA) to reflect the three levels of detail used to describe the manufacturing process.