Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Telnet client
- This sketch connects to a a telnet server (http://www.google.com)
- using an Arduino Wiznet Ethernet shield. You'll need a telnet server
- to test this with.
- Processing's ChatServer example (part of the network library) works well,
- running on port 10002. It can be found as part of the examples
- in the Processing application, available at
- http://processing.org/
- Circuit:
- * Ethernet shield attached to pins 10, 11, 12, 13
- created 14 Sep 2010
- modified 9 Apr 2012
- by Tom Igoe
- */
- #include <SPI.h>
- #include <Ethernet.h>
- #include "avdweb_VirtualDelay.h"
- VirtualDelay delay1;
- VirtualDelay delay2;
- // Reset arduina
- #define SYSRESETREQ (1<<2)
- #define VECTKEY (0x05fa0000UL)
- #define VECTKEY_MASK (0x0000ffffUL)
- #define AIRCR (*(uint32_t*)0xe000ed0cUL) // fixed arch-defined address
- #define REQUEST_EXTERNAL_RESET (AIRCR=(AIRCR&VECTKEY_MASK)|VECTKEY|SYSRESETREQ)
- // Enter a MAC address and IP address for your controller below.
- // The IP address will be dependent on your local network:
- byte mac[] = {
- 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
- };
- IPAddress ip(192, 168, 0, 177);
- // Enter the IP address of the server you're connecting to:
- IPAddress server(192, 168, 0, 110);
- // Initialize the Ethernet client library
- EthernetClient client;
- /*
- 1_Reverser 11_RychlomerVyp 21_JisticF140 31_JisticF121 41_JisticF122 51_JisticF150 61_JisticF203
- 2_Regulator 12_Sander 22_JisticF182 32_JisticF180 42_JisticF123 52_JisticF151 62_JisticF202
- 3_EngineBrakeControl 13_RoletaPP 23_JisticF109 33_JisticF127 43_JisticF124 53_JisticF152 63_KohoutSberaceP
- 4_DynamicBrake 14_RoletaPL 24_JisticF116 34_JisticF128 44_JisticF125 54_JisticF130 64_KohoutSberaceZ
- 5_TrainBrakeControl 15_RoletaBocniPP 25_Jistic07neobsazeny 35_JisticF153 45_JisticF126 55_JisticF131 65_RucniBrzda
- 6_HandBrake 16_RoletaBocniPL 26_JisticF111 36_JisticF154 46_JisticF106 56_JisticF132 66_PrepinacBaterie
- 7_PantographControl 17_OknoP 27_Jistic09neobsazeny 37_JisticF134 47_JisticF191 57_JisticF129 67_PrepinacCZ
- 8_Headlights 18_OknoL 28_Jistic10neobsazeny 38_JisticF135 48_JisticF192 58_JisticF144 68_PrepinacOsvetleniPodvozku
- 9_SpeedometerKPH 19_JisticF104 29_JisticF107 39_JisticF136 49_Jistic31neobsazeny 59_Jistic41neobsazeny 69_PrepinacRizeni
- 10_Rychlomer 20_JisticF101 30_JisticF102 40_JisticF120 50_Jistic32neobsazeny 60_JisticF114 70_KlickaTopeni
- 71_TopeniVlaku 81_PrepinacPozickaZP 91_EDB 101_TlacitkoARR40 111_TlacitkoARRMinus 121_TlacitkoRuseniOchran
- 72_PrepinacNJ 82_PrepinacPozickaPL 92_TlacitkoBdelostiL 102_TlacitkoARR50 112_TlacitkoPistala 122_PrepinacSterace
- 73_VirtualDJNJ 83_PrepinacPozickaPP 93_TlacitkoBdelostiP 103_TlacitkoARR60 113_TlacitkoPJ 123_SteracP
- 74_PrepinacSystemu 84_PrepinacOsvetleniPristroju 94_PrepinacRezimRizeni 104_TlacitkoARR70 114_KontrolkaPJ 124_SteracL
- 75_VypinacHV 85_PrepinacOsvetleniKabiny 95_PrepinacRezimJizdy 105_TlacitkoARR80 115_PrepinacVolbaVlaku 125_PrepinacPoruchaMS
- 76_PrepinacVentilatory 86_OsvStTlumene 96_PrepinacPT 106_TlacitkoARR90 116_TlacitkoPiskovani 126_PrepinacDJ
- 77_PrepinacKompresor2 87_OsvStPlne 97_TlacitkoARR0 107_TlacitkoARR100 117_OvladacBP 127_PrepinacOdvodneni
- 78_PrepinacKompresor1 88_PrepinacReflektoru 98_TlacitkoARR10 108_TlacitkoARR120 118_OvladacBSE 128_PrepinacVentilatorKabiny
- 79_PrepinacSberace 89_Smer 99_TlacitkoARR20 109_TlacitkoARR140 119_TlacitkoPlus 129_VentilatorKabiny
- 80_PrepinacPozickaZL 90_Kontroler 100_TlacitkoARR30 110_TlacitkoARRPlus 120_TlacitkoMinus 130_VentilatorKabiny_moturek
- 131_VentilatorKabiny_drzak 141_DiagK141 151_DiagQ14A 161_DiagQ12L_Q14L_B 171_DiagK106 181_IndNapeti 191_KontrolkaZaver
- 132_HoukackaPomocnik 142_DiagK140 152_DiagQ13B 162_Diag1-2_XA 172_DiagNR 182_IndHVst 192_KontrolkaPrutok
- 133_DvirkaDiagnostiky 143_DiagK116 153_DiagQ14B 163_Diag3-4_XB 173_DiagDR 183_VmetrSS 193_PocitadloNaprav0XX
- 134_DiagK117_K138 144_DiagW 154_DiagQ13L_D1 164_DiagK101_J0 174_PrepinacLampaStolek 184_VmetrST 194_PocitadloNapravX0X
- 135_DiagA102_N 145_DiagG 155_DiagQ14L_D1 165_DiagQ11A_J 175_PrepinacLampaSJR 185_AmetrBuzeni 195_PocitadloNapravXX0
- 136_DiagS125_S126 146_DiagK36A_S0 156_DiagQ13L_R1 166_DiagQ12A_J 176_Vysilacka 186_Ammeter 196_TlakPotrubi
- 137_DiagS502_H280 147_DiagK36L_S1 157_DiagQ14L_R1 167_DiagQ11L_Q13L_J1 177_AmetrBaterie 187_AmetrMS1 197_BrakePipePressureBAR
- 138_DiagQ06_H280 148_DiagP 158_DiagQ11B_Q11C 168_DiagQ12L_Q14L_J1 178_VmetrBaterie 188_AmetrMS2 198_TlakVzduchojem
- 139_DiagK117 149_DiagZ 159_DiagQ12B_Q12C 169_DiagK40A_E0 179_IndTopeni 189_PomernyTah 199_TlakValce
- 140_DiagS407 150_DiagQ13A 160_DiagQ11L_Q13L_B 170_DiagK40L_E1 180_IndHVss 190_KontrolkaPorucha 200_LocoBrakeCylinderPressureBAR
- 201_TrainBrakeCylinderPressureBAR 211_PrepinacDiagCerpadlo1 221_PrepinacDiagNabijeni 231_DiagSkrinOchran 241_DiagVentilator1 251_ZvukVentilatoryTM
- 202_TlakPrevodnik 212_PrepinacDiagPolovodicovyPrvek 222_DiagMazaniKompresor1 232_DiagMazaniKompresor2 242_ZvukVentilatorKabiny 252_ZvukVZSignalizace
- 203_Opakovac 213_PrepinacDiagBuchholzovoRele 223_DiagNadproudUnipulsu 233_DiagVypnutiEDB 243_ZvukPomocnyKompresor 253_ZvukVZSignalizaceTlacitko
- 204_OpakovacModra 214_PrepinacDiagOlejTR 224_DiagPomocnyKompresor 234_DiagTopeniJimek 244_ZvukTrafo 254_ZvukVZStart
- 205_OpakovacNatoceni 215_PrepinacDiagSkrinOchran 225_DiagRegulace1 235_DiagRegulace2 245_ZvukUnipuls 255_ZvukZK
- 206_PrepinacLS90 216_PrepinacDiagVentilace1 226_DiagCerpadlo2 236_DiagNabijeni 246_ZvukKompresory 256_ZvukNakolku
- 207_KontrolkaVZzelena 217_PrepinacDiagVentilace2 227_DiagCerpadlo1 237_DiagKompresory 247_ZvukPistaly 257_ZvukOkno
- 208_KontrolkaVZcervena 218_PrepinacDiagSkluz 228_DiagPrvkyMenicu 238_DiagCentralniZdroj 248_ZvukHoukacky 258_ZvukMSvyp
- 209_UkazatelARR 219_PrepinacDiagCZ 229_DiagBuchholzovoRele 239_DiagSkluz 249_ZvukPiskovani 259_ZvukStanoviste
- 210_PrepinacDiagCerpadlo2 220_PrepinacDiagKompresory 230_DiagOlejTransformatoru 240_DiagVentilator2 250_ZvukOdkaleni 260_ZvukBlokaceStrojovny
- 261_NahozeniMasiny
- 262_Porucha
- 263_PoruchaTM
- 264_NapetiTV
- */
- // Provozní dignostika 1. a 2. panel
- String group1 = "SetGroup(1, DiagK117_K138,DiagA102_N,DiagS125_S126,DiagS502_H280,DiagQ06_H280,DiagK117,DiagS407,DiagK141,DiagK140,DiagK116,,DiagW,DiagG,DiagK36A_S0,DiagK36L_S1,DiagP,DiagZ,DiagQ13A,DiagQ14A,DiagQ13B,DiagQ14B)";
- String group2 = "SetGroup(2, DiagQ13L_D1,DiagQ14L_D1,DiagQ13L_R1,DiagQ14L_R1,DiagQ11B_Q11C,DiagQ12B_Q12C,DiagQ11L_Q13L_B,DiagQ12L_Q14L_B,Diag1-2_XA,Diag3-4_XB,DiagK101_J0,DiagQ11A_J,DiagQ12A_J,DiagQ11L_Q13L_J1,DiagQ12L_Q14L_J1,DiagK40A_E0,DiagK40L_E1,DiagK106,DiagNR,DiagDR)";
- // Diagnostika u stropu
- String group3 = "SetGroup(3, DiagMazaniKompresor1,DiagNadproudUnipulsu,DiagPomocnyKompresor,DiagRegulace1,DiagCerpadlo2,DiagCerpadlo1,DiagPrvkyMenicu,DiagBuchholzovoRele,DiagOlejTransformatoru,DiagSkrinOchran)";
- String group4 = "SetGroup(4, DiagMazaniKompresor2,DiagVypnutiEDB,DiagTopeniJimek,DiagRegulace2,DiagNabijeni,DiagKompresory,DiagCentralniZdroj,DiagSkluz,DiagVentilator2,DiagVentilator1)";
- int potrebneKontrolky[] = {9,114,129,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,196,198,199,202,203,204,207,208,209,252,253,254,406,407,408};
- int velikost;
- int hodnoty[410];
- int skupina1[20];
- int skupina2[20];
- int skupina3[10];
- int skupina4[10];
- int testovaci;
- int locoName;
- int rychlostMax;
- byte actualHourHEX;
- byte actualMinuteHEX;
- byte actualsecondHEX;
- int actualyearHEX;
- byte actualMonthHEX;
- byte actualdayHEX;
- byte DECtoHEX[] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,
- 0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,
- 0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x60};
- void setup() {
- // Open serial communications and wait for port to open:
- Serial.begin(115200);
- Serial2.begin(9600);
- // Inicializace ethernet klienta
- ClientInitialize();
- // Zjištění počtu aktuálně zadaných kontrolek
- velikost = sizeof(potrebneKontrolky) / sizeof(int);
- client.print(group1);
- String value1 = client.readStringUntil('\r\n');
- client.print(group2);
- String value2 = client.readStringUntil('\r\n');
- client.print(group3);
- String value3 = client.readStringUntil('\r\n');
- client.print(group4);
- String value4 = client.readStringUntil('\r\n');
- delay(1000);
- // První spuštění časovačů
- delay1.start(100);
- delay2.start(50);
- pinMode(13,OUTPUT);
- }
- void GetAll(int *seznam) {
- for(int i=0; i<velikost; i++) {
- String vypis = "GetCV(" + String(seznam[i]) + ",0)::";
- client.print(vypis);
- }
- }
- void Prijem() {
- while(client.available() > 0) {
- const char s[2] = "=";
- char str[200];
- char str2[80];
- char *a;
- char *b;
- String stringi = client.readStringUntil('\r\n');
- stringi.toCharArray(str, 200);
- a = strtok(str, s);
- int id = String(a).toInt();
- if(id < 500) {
- b = strtok(NULL, s);
- int valueFloat = 0 ;
- valueFloat = (String(b)).toInt();
- testovaci = valueFloat;
- hodnoty[(int)id] = testovaci;
- //Serial.println(String(id) + " = " + String((int)hodnoty[id]) + " = " + String(valueFloat));
- }
- else if(id == 501) {
- b = strtok(NULL, s);
- String(b).toCharArray(str2, 80);
- int j=0;
- for(int i=0; i<80; i++) {
- if(isDigit(str2[i]) == true) {
- if(str2[i] == '1') {
- skupina1[j] = 1;
- }
- else {
- skupina1[j] = 0;
- }
- j++;
- }
- }
- }
- else if(id == 502) {
- b = strtok(NULL, s);
- String(b).toCharArray(str2, 80);
- int j=0;
- for(int i=0; i<80; i++) {
- if(isDigit(str2[i]) == true) {
- if(str2[i] == '1') {
- skupina2[j] = 1;
- }
- else {
- skupina2[j] = 0;
- }
- j++;
- }
- }
- }
- else if(id == 503) {
- b = strtok(NULL, s);
- String(b).toCharArray(str2, 80);
- int j=0;
- for(int i=0; i<80; i++) {
- if(isDigit(str2[i]) == true) {
- if(str2[i] == '1') {
- skupina3[j] = 1;
- }
- else {
- skupina3[j] = 0;
- }
- j++;
- }
- }
- }
- else if(id == 504) {
- b = strtok(NULL, s);
- String(b).toCharArray(str2, 80);
- int j=0;
- for(int i=0; i<80; i++) {
- if(isDigit(str2[i]) == true) {
- if(str2[i] == '1') {
- skupina4[j] = 1;
- }
- else {
- skupina4[j] = 0;
- }
- j++;
- }
- }
- }
- else if(id == 600) {
- b = strtok(NULL, s);
- String cisloLoko = "";
- for(int i=0; i<12; i++) {
- if(isDigit(b[i]) == true) {
- cisloLoko += b[i];
- }
- }
- locoName = cisloLoko.toInt();
- }
- }
- }
- void ClientInitialize() {
- // You can use Ethernet.init(pin) to configure the CS pin
- Ethernet.init(10); // Most Arduino shields
- // start the Ethernet connection:
- Ethernet.begin(mac, ip);
- // Check for Ethernet hardware present
- if (Ethernet.hardwareStatus() == EthernetNoHardware) {
- Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
- while (true) {
- delay(1); // do nothing, no point running without Ethernet hardware
- }
- }
- while (Ethernet.linkStatus() == LinkOFF) {
- Serial.println("Ethernet cable is not connected.");
- delay(1000);
- }
- // give the Ethernet shield a second to initialize:
- delay(1000);
- Serial.println("connecting...");
- // if you get a connection, report back via serial:
- if (client.connect(server, 8888)) {
- Serial.println("connected");
- } else {
- // if you didn't get a connection to the server:
- Serial.println("connection failed");
- }
- client.setTimeout(100);
- }
- void ConnectClient() {
- // if the server's disconnected, restart the client:
- if (!client.connected()) {
- Serial.println();
- Serial.println("disconnecting.");
- client.stop();
- delay(1000);
- // do nothing:
- while (true) {
- if (client.connect(server, 8888)) {
- Serial.println("Server available again, resseting...");
- delay(100);
- REQUEST_EXTERNAL_RESET;
- break;
- }
- }
- }
- }
- int * calcCheckSum (int* pack, int packSize)
- {
- int sum = 0;
- for (int i=0; i < (packSize-1); i++) sum += pack[i];
- sum = sum & 0xFF;
- pack[packSize-1] = 0x100 - sum;
- return pack;
- }
- unsigned int pw [] = { 1, 10, 100, 1000, 10000 } ;
- int ExtractDigit(int V, int P)
- {
- return V / pw[P] % 10 ;
- }
- void loop() {
- int cas1 = millis();
- if(delay2.elapsed()) {
- //ConnectClient();
- GetAll(&potrebneKontrolky[0]);
- client.print("GetGroupValues(1)::");
- client.print("GetGroupValues(2)::");
- client.print("GetGroupValues(3)::");
- client.print("GetGroupValues(4)::");
- client.print("GetLocoName::");
- digitalWrite(13,hodnoty[190]);
- delay2.start(100);
- }
- Prijem();
- if(delay1.elapsed()) {
- int hodnota = ExtractDigit(locoName, 3);
- if(hodnota == 2) {
- rychlostMax = 140;
- }
- else if(hodnota == 3) {
- rychlostMax = 120;
- }
- else {
- rychlostMax = 160;
- }
- actualHourHEX = DECtoHEX[hodnoty[406]];
- actualMinuteHEX = DECtoHEX[hodnoty[407]];
- actualsecondHEX = DECtoHEX[hodnoty[408]];
- actualMonthHEX = DECtoHEX[2];
- actualdayHEX = DECtoHEX[3];
- actualyearHEX = DECtoHEX[19];
- byte rychlostHEX = hodnoty[9];
- byte ukazatelARRHEX = hodnoty[209];
- byte rychlostMaxHEX = rychlostMax;
- digitalWrite(3, 1);
- byte msg[27];
- msg[0] = 0x19; //počet bajtů ve zpráve
- msg[1] = 0x01; //kód povelu 01
- msg[2] = 0x00; // Stavové slovo zprávy
- msg[3] = actualsecondHEX; //Vteřiny
- msg[4] = actualMinuteHEX; //minuty
- msg[5] = actualHourHEX; // Hodiny
- msg[6] = actualdayHEX; //den
- msg[7] = actualMonthHEX; //Měsíc
- msg[8] = actualyearHEX; //Rok
- msg[9] = 0x12; //dráha //0x88
- msg[10] = 0x65; //0x03
- msg[11] = 0x49; //0x03
- msg[12] = 0x99; //0x02
- msg[13] = rychlostHEX; //Rychlost akt
- msg[14] = ukazatelARRHEX; //Rychlost nav
- msg[15] = rychlostMaxHEX; //Max V
- msg[16] = 0x00; //Tlak
- msg[17] = 0x00; //další analog
- msg[18] = 0x00; //1. čís
- msg[19] = 0x00; //2. čís
- msg[20] = 0x00; //3. čís
- msg[21] = 0x00; //4. čís
- msg[22] = 0x00; //5. čís
- msg[23] = 0x00; //Chyb bajt
- msg[24] = 0x00; //traťová značka
- msg[25] = 0x00; //Traťová značka
- uint8_t CRC = 0;
- for(int8_t i = 0; i< 25; i++){
- CRC -= msg[i];
- }
- msg[26] = CRC;
- Serial2.write(msg,27);
- digitalWrite(3, 0);
- delay1.start(90);
- }
- int cas2 = millis();
- if(cas2-cas1 > 5) {
- Serial.println(cas2-cas1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment