Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface Settimino mbed-rtos mbed
main.cpp
00001 #include "mbed.h" 00002 #include "EthernetInterface.h" 00003 #include "Settimino.h" 00004 00005 // PLC INFO 00006 const char* PLC = "10.0.10.51"; 00007 const int RACK = 0; 00008 const int SLOT = 2; 00009 const int DB = 21; 00010 const int TOT_DATA = 10; 00011 const int OFFSET = 0; 00012 00013 int main() 00014 { 00015 EthernetInterface eth; // Ethernet interface for connection 00016 S7Client client; // Snap7 client 00017 Serial serial(USBTX, USBRX); // USB serial port 00018 00019 // Check EthernetInferface library handbook for details 00020 serial.printf("Connect to network...\r\n"); 00021 eth.init(); //Use DHCP 00022 eth.connect(); 00023 serial.printf("Connected: IP Address is %s\r\n", eth.getIPAddress()); 00024 wait(2); 00025 00026 serial.printf("Connecto to PLC at %s...\r\n", PLC); 00027 while(!client.Connected) 00028 { 00029 if(client.ConnectTo(PLC, RACK, SLOT)) 00030 wait_ms(500); 00031 } 00032 serial.printf("Connected: PDU Length = %d\r\n", client.GetPDULength()); 00033 wait(2); 00034 00035 char buffer[32]; 00036 int result; 00037 serial.printf("Read %d bytes from DB%d, starting at %d...\r\n", TOT_DATA, DB, OFFSET); 00038 result = client.ReadArea(S7AreaDB, // Area type 00039 DB, // DB address 00040 OFFSET, // Byte offset 00041 TOT_DATA, // Tot bytes to read 00042 buffer); // buffer that will store read data 00043 if(result != 0) 00044 { 00045 serial.printf("ERROR %d\r\n", result); 00046 } 00047 else 00048 { 00049 serial.printf("Read data:\r\n"); 00050 for(uint16_t i = 0; i < TOT_DATA; i++) 00051 serial.printf("%c", buffer[i]); 00052 serial.printf("\r\n"); 00053 } 00054 wait(2); 00055 00056 serial.printf("Disconnect from PLC...\r\n"); 00057 client.Disconnect(); 00058 00059 serial.printf("Disconnect from network...\r\n"); 00060 eth.disconnect(); 00061 00062 serial.printf("\r\n-- Test terminated --\r\n\r\n"); 00063 00064 while(1) {} 00065 }
Generated on Wed Jul 13 2022 05:27:58 by
