This work presents an optimal distributed energy resource management system for a given smart grid connected to photovoltaics, battery energy storage, and an electric vehicle aggregator. These management systems are one of the crucial factors to control the power converters connected to the grid to optimize the power generated by the RES. The proposed management system includes the communication architecture necessary for acknowledging the information flow between the individual control of the distributed generators and the master supervisory control algorithm. The work was carried out on two levels. the first is to design a control strategy for energy management and validate it with the grid in real-time hardware-in-the-loop simulation integrating the IEC61850 communication layer and physically intelligent electronic devices. The second is to analyze the vulnerabilities of the designed methodology for cybersecurity threats explicitly with the extension of IEC61850 to electric vehicle aggregators for communication with the master energy management.