Differences
This shows you the differences between two versions of the page.
— |
de:project_s7:s7_arduino_modbus_master [2022/04/21 15:01] () |
||
---|---|---|---|
1: | 1: | ||
+ | ====== S7-1500 und Arduino Modbus Verbindung (Arduino Server) ====== | ||
+ | {{ : | ||
+ | {{: | ||
+ | {{: | ||
+ | |||
+ | {{: | ||
+ | |||
+ | |{{: | ||
+ | |Simatic-Arduino Verbindung|TIA Portal Sicht \\ Klicken Sie auf das Bild, um es zu vergrößern.|Arduino Serial Monitor \\ Klicken Sie auf das Bild, um es zu vergrößern| | ||
+ | ===== Simatic S7-1500, als Client (master) ===== | ||
+ | |||
+ | {{: | ||
+ | |||
+ | {{: | ||
+ | |||
+ | {{: | ||
+ | |||
+ | <WRAP center round download 80%> | ||
+ | **SCL-Programm download : ** {{: | ||
+ | Der Quellcode enthält eine Beschreibung eines scl-Codes (siehe Quellcode unten). Der Code kann im TIA-Portal geöffnet und übersetzt werden. | ||
+ | </ | ||
+ | |||
+ | From file (from 2020.10.30) **{{dlcounter> | ||
+ | |||
+ | <hidden Klicken Sie hier, um den Code anzuzeigen - Click here to view the code> | ||
+ | <code pascal> | ||
+ | FUNCTION_BLOCK " | ||
+ | { S7_Optimized_Access := ' | ||
+ | VERSION : 0.1 | ||
+ | | ||
+ | modbusParameter {InstructionName := ' | ||
+ | modbusClient {InstructionName := ' | ||
+ | modbusTime {InstructionName := ' | ||
+ | modbusTrig { S7_SetPoint := ' | ||
+ | timeOn : Bool; | ||
+ | rec : Array[0..9] of Word; | ||
+ | done : Bool; | ||
+ | busy : Bool; | ||
+ | error : Bool; | ||
+ | status : Word; | ||
+ | temp : Word; | ||
+ | | ||
+ | |||
+ | |||
+ | BEGIN | ||
+ | // communication between Simatic S7-1500 and Arduino Mega over Modbus (holding registers) | ||
+ | // Server-slave: | ||
+ | // client-master: | ||
+ | // code: Vamos, 2020 -- ob121.com | ||
+ | |||
+ | |||
+ | // read in sec raster | ||
+ | # | ||
+ | PT := t#500ms, | ||
+ | Q => # | ||
+ | |||
+ | // trigger for modbus read | ||
+ | IF #modbusTrig THEN | ||
+ | IF #timeOn THEN | ||
+ | #timeOn := false; | ||
+ | ELSE | ||
+ | #timeOn := true; | ||
+ | END_IF; | ||
+ | END_IF; | ||
+ | |||
+ | // call modbus client | ||
+ | # | ||
+ | DISCONNECT: | ||
+ | MB_MODE:=0, | ||
+ | MB_DATA_ADDR: | ||
+ | MB_DATA_LEN: | ||
+ | DONE=># | ||
+ | BUSY=># | ||
+ | ERROR=># | ||
+ | STATUS=># | ||
+ | MB_DATA_PTR: | ||
+ | CONNECT: | ||
+ | |||
+ | // monitor for Arduino informations | ||
+ | #temp := # | ||
+ | #temp := # | ||
+ | #temp := # | ||
+ | #temp := # | ||
+ | #temp := # | ||
+ | #temp := #rec[5]; | ||
+ | |||
+ | // 0000: Instruction executed without errors. | ||
+ | // 0001: Connection established. | ||
+ | // 0003: Connection terminated. | ||
+ | // 7000: #no job active AND no connection established (REQ=0, DISCONNECT=1). | ||
+ | // 7001: Connection establishment triggered. | ||
+ | // 7002: Intermediate call. Connection is being established. | ||
+ | // 7003: Connection is being terminated. | ||
+ | // 7004: Connection established AND monitored. No job processing active. | ||
+ | // 7005: data is being sent. | ||
+ | // 7006: data is being received. | ||
+ | // 80BB: Invalid value at ActiveEstablished parameter | ||
+ | // 80C8: #no response OF the server in the defined period. | ||
+ | // 8187: the MB_HOLD_REG parameter has an Invalid Pointer. Data area is too small. | ||
+ | // 8380: received Modbus frame has incorrect format OR too few bytes were received. | ||
+ | // 8381: Function code is NOT supported. | ||
+ | // 8382: the length OF the Modbus frame in the frame header does NOT match the number OF received bytes. | ||
+ | // 8383: #error reading OR writing data OR access outside the address area | ||
+ | // 8384: Invalid exception code received. | ||
+ | // 8385: Diagnostics code NOT supported. | ||
+ | // 8386: received function code does NOT match the one sent originally. | ||
+ | // 8387: the protocol ID OF the Modbus TCP frame received BY the server is NOT " | ||
+ | // 8388: the Modbus server sent a different data length than was requested. | ||
+ | // 8389: Invalid data area definition | ||
+ | // 80B6: Invalid connection type, only TCP connections are supported. | ||
+ | // 80BB: Invalid value at ActiveEstablished parameter | ||
+ | // 8188: the MB_MODE parameter has an Invalid value. | ||
+ | // 8189: Invalid addressing OF data at the MB_DATA_ADDR parameter. | ||
+ | // 818A: Invalid data length at the MB_DATA_LEN parameter. | ||
+ | // 818B: the MB_DATA_PTR parameter has an Invalid Pointer. | ||
+ | // 818C: Timeout at parameter BLOCKED_PROC_TIMEOUT OR RCV_TIMEOUT | ||
+ | // 8200: A different Modbus request is currently being processed via the PORT. | ||
+ | |||
+ | END_FUNCTION_BLOCK | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ===== Arduino Mega, als Server (slave) ===== | ||
+ | {{: | ||
+ | |||
+ | {{: | ||
+ | |||
+ | {{: | ||
+ | |||
+ | - https:// | ||
+ | - https:// | ||
+ | |||
+ | {{ : | ||
+ | ==== Arduino-Messungen ==== | ||
+ | {{: | ||
+ | |||
+ | * Temperatur: DHT22-Sensormessung (-40..80 ° C, 0,5% Genauigkeit, | ||
+ | * Luftfeuchtigkeit (0..100%, 2..5% Genauigkeit, | ||
+ | * Wärmeempfindungsindex (abgeleitet von Temperatur und Luftfeuchtigkeit, | ||
+ | * Lichtintensität (0..1024, Fotosensor GL5506) | ||
+ | * Anwesenheitssignal (heart-beat, | ||
+ | |||
+ | Anschlussbelegung der Messungen: DHT22: Pin 9, Photosensor: | ||
+ | |||
+ | {{: | ||
+ | |||
+ | * temperature: | ||
+ | * humidity (0..100%, 2..5% accuracy, divisible by 100) | ||
+ | * heat sensation index (derived from temperature and humidity, divided by 100) | ||
+ | * light intensity (0..1024, GL5506 photo sensor) | ||
+ | * presence signal (heart-beat, | ||
+ | |||
+ | Port assignment of measurements: | ||
+ | |||
+ | {{: | ||
+ | |||
+ | * hőmérséklet: | ||
+ | * páratartalom (0..100%, 2..5% pontosság, 100-zal osztandó) | ||
+ | * hőérzet index (a hőmérsékletből és páratartalomból származik, 100-zal osztandó) | ||
+ | * világítás-intenzitás (0..1024, GL5506 fotoszenzor) | ||
+ | * jelenlét-jel (heart-beat, | ||
+ | |||
+ | Mérések port-kiosztása: | ||
+ | |||
+ | ==== Arduino Software ==== | ||
+ | |||
+ | Libraries: \\ | ||
+ | - https:// | ||
+ | - https:// | ||
+ | - https:// | ||
+ | - https:// | ||
+ | |||
+ | <WRAP center round download 80%> | ||
+ | |||
+ | **Arduino-Programm download : ** {{: | ||
+ | Der Quellcode enthält eine Beschreibung eines Arduino-Codefile (siehe Quellcode unten). Der Code kann im Arduino geöffnet und übersetzt werden. | ||
+ | </ | ||
+ | |||
+ | <hidden Klicken Sie hier, um den Code anzuzeigen - Click here to view the code> | ||
+ | <code c> | ||
+ | // Modbus Server Mega board | ||
+ | // Test for Simatic s7-1500 Modbus connections | ||
+ | // Vamos Sandor - 2020 - ob121.com | ||
+ | // https:// | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #define DHTPIN 9 | ||
+ | |||
+ | // Uncomment whatever type you're using! | ||
+ | //#define DHTTYPE DHT11 // DHT 11 | ||
+ | #define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 | ||
+ | //#define DHTTYPE DHT21 // DHT 21 (AM2301) | ||
+ | |||
+ | DHT dht(DHTPIN, DHTTYPE); | ||
+ | |||
+ | word hum, temp, heatidx, lightint; | ||
+ | bool _2s; | ||
+ | unsigned long TimeAct, TimePrev, HoldingResult, | ||
+ | long Cmd; | ||
+ | |||
+ | EthernetServer EthServer(502); | ||
+ | ModbusTCPServer modbusTCPServer; | ||
+ | |||
+ | void setup() { | ||
+ | // Ethernet Settings | ||
+ | byte mac[] = { 0x4E, 0xA0, 0xBE, 0x3F, 0xFE, 0x0F }; // Define MAc address | ||
+ | byte ip[] = { 192, 168, 0, 42 }; // Define IP address | ||
+ | byte subnet[] = { 255, 255, 255, 0 }; // Define SubNEt mask | ||
+ | |||
+ | // initialize the ethernet device | ||
+ | Ethernet.begin(mac, | ||
+ | Serial.begin(9600); | ||
+ | EthServer.begin(); | ||
+ | modbusTCPServer.begin(); | ||
+ | dht.begin(); | ||
+ | | ||
+ | // Define Holding register: | ||
+ | HoldingResult = modbusTCPServer.configureHoldingRegisters(0, | ||
+ | InputResult = modbusTCPServer.configureInputRegisters(0, | ||
+ | |||
+ | Serial.print(" | ||
+ | Serial.print(HoldingResult); | ||
+ | Serial.print(" | ||
+ | |||
+ | Serial.print(" | ||
+ | Serial.print(InputResult); | ||
+ | Serial.print(" | ||
+ | |||
+ | Serial.print(" | ||
+ | Serial.println(Ethernet.localIP()); | ||
+ | Serial.print(" | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | // Modbus server accept incoming connections | ||
+ | EthernetClient client = EthServer.available(); | ||
+ | |||
+ | if (client.connected()) { | ||
+ | modbusTCPServer.accept(client); | ||
+ | // poll for Modbus TCP requests, while client connected | ||
+ | modbusTCPServer.poll(); | ||
+ | // Serial.print(" | ||
+ | } | ||
+ | |||
+ | // | ||
+ | // TIME clock 2s | ||
+ | TimeAct = millis(); | ||
+ | |||
+ | _2s = false; | ||
+ | if ( ( (TimeAct > TimePrev) and (TimeAct - TimePrev) > 2000) or ((TimeAct < TimePrev) and (65535 - TimePrev + TimeAct) > 2000 ) ) { | ||
+ | _2s = true; | ||
+ | TimePrev = TimeAct; | ||
+ | } | ||
+ | // | ||
+ | // HeartBeat | ||
+ | if (_2s) { | ||
+ | HeartBeat++; | ||
+ | if (HeartBeat > 255) { | ||
+ | HeartBeat = 0; | ||
+ | } | ||
+ | Serial.print(" | ||
+ | Serial.println(HeartBeat); | ||
+ | Serial.print(" | ||
+ | dht22(); | ||
+ | light(); | ||
+ | } | ||
+ | // | ||
+ | // Modbus server | ||
+ | |||
+ | // holding register 40001: heartbeat (FC3) | ||
+ | modbusTCPServer.holdingRegisterWrite(0x00, | ||
+ | modbusTCPServer.holdingRegisterWrite(0x01, | ||
+ | modbusTCPServer.holdingRegisterWrite(0x02, | ||
+ | modbusTCPServer.holdingRegisterWrite(0x03, | ||
+ | modbusTCPServer.holdingRegisterWrite(0x04, | ||
+ | modbusTCPServer.holdingRegisterWrite(0x05, | ||
+ | modbusTCPServer.holdingRegisterWrite(0x06, | ||
+ | |||
+ | } | ||
+ | |||
+ | void dht22() { | ||
+ | float h = dht.readHumidity(); | ||
+ | float t = dht.readTemperature(); | ||
+ | float f = dht.readTemperature(true); | ||
+ | // Check if any reads failed and exit early (to try again). | ||
+ | if (isnan(h) || isnan(t) || isnan(f)) { | ||
+ | Serial.println(F(" | ||
+ | hum = 0; // in errorcase: 0 | ||
+ | temp = 0; // in errorcase: 0 | ||
+ | heatidx = 0; // in errorcase: 0 | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | // Compute heat index in Fahrenheit (the default) | ||
+ | float hif = dht.computeHeatIndex(f, | ||
+ | // Compute heat index in Celsius (isFahreheit = false) | ||
+ | float hic = dht.computeHeatIndex(t, | ||
+ | |||
+ | Serial.print(F(" | ||
+ | Serial.print(h); | ||
+ | Serial.print(F(" | ||
+ | Serial.print(t); | ||
+ | Serial.print(F(" | ||
+ | Serial.print(f); | ||
+ | Serial.print(F(" | ||
+ | Serial.print(hic); | ||
+ | Serial.print(F(" | ||
+ | Serial.print(hif); | ||
+ | Serial.println(F(" | ||
+ | |||
+ | h = h * 100.0; | ||
+ | t = t * 100.0; | ||
+ | hic = hic * 100.0; | ||
+ | |||
+ | hum = (word) h; | ||
+ | temp = (word) t; | ||
+ | heatidx = (word) hic; | ||
+ | } | ||
+ | |||
+ | void light() { | ||
+ | // light sensor (photosensor): | ||
+ | // dark resistance 0.15 MOhm | ||
+ | | ||
+ | int val = analogRead(5); | ||
+ | Serial.print(" | ||
+ | Serial.println(val); | ||
+ | lightint = (word) val; | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | </ | ||
+ | |||
+ | {{page> |