// File: Eba-M[P]E3DC-Control/RscpExampleMain.cpp
// Function: iModbusTCP_Heizstab(int)
int iModbusTCP_Heizstab(int ireq_power) // angeforderte Leistung
{
// Alle 10 sec wird der neue wert an den heizstab geschickt
    static int isocket = -1;
    static bool brequest = false;
    static time_t tlast = 0;
    static int maxpower = -1;
    time_t now;
    time(&now);
    char server_ip[16];
    Modbus_send Msend;
    static int iPower_Heizstab = 0;
        
    if ((now-tlast)>5)
    {
        if ((isocket < 0)&&(strcmp(e3dc_config.heizstab_ip, "0.0.0.0") != 0)&&ireq_power>0&&(now-tlast>10))
            
        {
            isocket = SocketConnect(e3dc_config.heizstab_ip, e3dc_config.heizstab_port);
            if (isocket < 0) // wenn der socket nicht verfügbar, eine Stunde warten
            tlast = now + 60;
        }
        if (isocket > 0)
        {
            //                brequest = true;
            tlast = now;
            send.resize(12);
            receive.resize(15);
            
            if (maxpower <= 0) // ermitteln maximale Leistung
            {
                Msend.Tid = 1*256;
                Msend.Pid = 0;
                Msend.Mlen = 6*256;
                Msend.Dev = 1;
                Msend.Fcd = 3; // Funktioncode auslesen
//                Msend.Fcd = 6; // Funktioncode
                Msend.Reg =  (1014%256)*256 + (1014/256);  // Adresse Register Leistung heizstab
                Msend.Count = 1*256; // Anzahl Register // 22.6° setzen
                memcpy(&send[0],&Msend,send.size());
                SocketSendData(isocket,&send[0],send.size());
                sleep(0.1);
// Auslesen der maximalen Leistung
            
                iLength = SocketRecvData(isocket,&receive[0],receive.size());
                if (iLength > 0)
                {
                    if (receive[7]==3)
                    {
                        maxpower = receive[9]*256+receive[10];
                    }
                }
            }

            
            iPower_Heizstab = iPower_Heizstab + ireq_power;
            if (iPower_Heizstab < 0) iPower_Heizstab = 0;
            if (maxpower < 3000) maxpower = 3000;
            if (iPower_Heizstab > maxpower) iPower_Heizstab = maxpower;
//            if (iPower_Heizstab > 10000) iPower_Heizstab = ireq_power;
            Msend.Tid = 1*256;
            Msend.Pid = 0;
            Msend.Mlen = 6*256;
            Msend.Dev = 1;
            Msend.Fcd = 6; // Funktioncode
            Msend.Reg =  (1000%256)*256 + (1000/256);  // Adresse Register Leistung heizstab
            //            Msend.Count = 1*256; // Anzahl Register // 22.6° setzen
            Msend.Count = (iPower_Heizstab%256)*256+ (iPower_Heizstab/256); // Leistung setzen
            memcpy(&send[0],&Msend,send.size());
            iLength = (SocketSendData(isocket,&send[0],send.size()));
            if (iLength <=0||(iPower_Heizstab==0))
            {
                SocketClose(isocket);
                if (isocket >= 0) isocket = -2;
                iPower_Heizstab = isocket;
            }
        }
        
        else
            iPower_Heizstab  = isocket;
    }
        return iPower_Heizstab;
}

// Assembly:
subq $0x158, %rsp            # imm = 0x158
movl %edi, 0x154(%rsp)
leaq 0x148(%rsp), %rdi
callq 0x2280
movq 0x148(%rsp), %rax
subq 0x68d3e(%rip), %rax     # 0x6fdd8
cmpq $0x5, %rax
jle 0x7424
cmpl $0x0, 0x67295(%rip)     # 0x6e340
jge 0x711e
leaq 0x67894(%rip), %rdi     # 0x6e948
leaq 0x5925c(%rip), %rsi     # 0x60317
callq 0x23c0
cmpl $0x0, %eax
je 0x711e
cmpl $0x0, 0x154(%rsp)
jle 0x711e
movq 0x148(%rsp), %rax
subq 0x68cfa(%rip), %rax     # 0x6fdd8
cmpq $0xa, %rax
jle 0x711e
movl 0x6795e(%rip), %esi     # 0x6ea48
leaq 0x67843(%rip), %rdi     # 0x6e934
addq $0x14, %rdi
callq 0x360d0
movl %eax, 0x67240(%rip)     # 0x6e340
cmpl $0x0, 0x67239(%rip)     # 0x6e340
jge 0x711c
movq 0x148(%rsp), %rax
addq $0x3c, %rax
movq %rax, 0x68cbc(%rip)     # 0x6fdd8
jmp 0x711e
cmpl $0x0, 0x6721b(%rip)     # 0x6e340
jle 0x7416
movq 0x148(%rsp), %rax
movq %rax, 0x68c9e(%rip)     # 0x6fdd8
leaq 0x68b3f(%rip), %rdi     # 0x6fc80
movl $0xc, %esi
callq 0x1cb60
leaq 0x68b46(%rip), %rdi     # 0x6fc98
movl $0xf, %esi
callq 0x1cb60
cmpl $0x0, 0x671e1(%rip)     # 0x6e344
jg 0x72b4
movw $0x100, 0x5e(%rsp)      # imm = 0x100
movw $0x0, 0x60(%rsp)
movw $0x600, 0x62(%rsp)      # imm = 0x600
movb $0x1, 0x64(%rsp)
movb $0x3, 0x65(%rsp)
movw $0xf603, 0x66(%rsp)     # imm = 0xF603
movw $0x100, 0x68(%rsp)      # imm = 0x100
leaq 0x68ae3(%rip), %rdi     # 0x6fc80
xorl %eax, %eax
movl %eax, %esi
callq 0x1cc00
movq %rax, 0x30(%rsp)
leaq 0x68ace(%rip), %rdi     # 0x6fc80
callq 0x1cc20
movq 0x30(%rsp), %rdi
movq %rax, %rdx
leaq 0x5e(%rsp), %rsi
callq 0x2220
movl 0x67171(%rip), %eax     # 0x6e340
movl %eax, 0x3c(%rsp)
leaq 0x68aa6(%rip), %rdi     # 0x6fc80
xorl %eax, %eax
movl %eax, %esi
callq 0x1cc00
movq %rax, 0x40(%rsp)
leaq 0x68a91(%rip), %rdi     # 0x6fc80
callq 0x1cc20
movl 0x3c(%rsp), %edi
movq 0x40(%rsp), %rsi
movl %eax, %edx
callq 0x364e0
xorl %edi, %edi
callq 0x21e0
movl 0x6712f(%rip), %eax     # 0x6e340
movl %eax, 0x4c(%rsp)
leaq 0x68a7c(%rip), %rdi     # 0x6fc98
xorl %eax, %eax
movl %eax, %esi
callq 0x1cc00
movq %rax, 0x50(%rsp)
leaq 0x68a67(%rip), %rdi     # 0x6fc98
callq 0x1cc20
movl 0x4c(%rsp), %edi
movq 0x50(%rsp), %rsi
movl %eax, %edx
callq 0x36590
cltq
movq %rax, 0x68a11(%rip)     # 0x6fc60
cmpq $0x0, 0x68a09(%rip)     # 0x6fc60
jle 0x72b2
leaq 0x68a38(%rip), %rdi     # 0x6fc98
movl $0x7, %esi
callq 0x1cc00
movzbl (%rax), %eax
cmpl $0x3, %eax
jne 0x72b0
leaq 0x68a1f(%rip), %rdi     # 0x6fc98
movl $0x9, %esi
callq 0x1cc00
movzbl (%rax), %eax
shll $0x8, %eax
movl %eax, 0x2c(%rsp)
leaq 0x68a04(%rip), %rdi     # 0x6fc98
movl $0xa, %esi
callq 0x1cc00
movq %rax, %rcx
movl 0x2c(%rsp), %eax
movzbl (%rcx), %ecx
addl %ecx, %eax
movl %eax, 0x67094(%rip)     # 0x6e344
jmp 0x72b2
jmp 0x72b4
movl 0x68b26(%rip), %eax     # 0x6fde0
addl 0x154(%rsp), %eax
movl %eax, 0x68b19(%rip)     # 0x6fde0
cmpl $0x0, 0x68b12(%rip)     # 0x6fde0
jge 0x72da
movl $0x0, 0x68b06(%rip)     # 0x6fde0
cmpl $0xbb8, 0x67060(%rip)   # imm = 0xBB8
jge 0x72f0
movl $0xbb8, 0x67054(%rip)   # imm = 0xBB8
movl 0x68aea(%rip), %eax     # 0x6fde0
cmpl 0x67048(%rip), %eax     # 0x6e344
jle 0x730a
movl 0x67040(%rip), %eax     # 0x6e344
movl %eax, 0x68ad6(%rip)     # 0x6fde0
movw $0x100, 0x5e(%rsp)      # imm = 0x100
movw $0x0, 0x60(%rsp)
movw $0x600, 0x62(%rsp)      # imm = 0x600
movb $0x1, 0x64(%rsp)
movb $0x6, 0x65(%rsp)
movw $0xe803, 0x66(%rsp)     # imm = 0xE803
movl 0x68aaa(%rip), %eax     # 0x6fde0
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
shll $0x8, %edx
movl %edx, 0xc(%rsp)
movl 0x68a95(%rip), %eax     # 0x6fde0
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
movl %eax, %ecx
movl 0xc(%rsp), %eax
addl %ecx, %eax
movw %ax, 0x68(%rsp)
leaq 0x68919(%rip), %rdi     # 0x6fc80
xorl %eax, %eax
movl %eax, %esi
callq 0x1cc00
movq %rax, 0x10(%rsp)
leaq 0x68904(%rip), %rdi     # 0x6fc80
callq 0x1cc20
movq 0x10(%rsp), %rdi
movq %rax, %rdx
leaq 0x5e(%rsp), %rsi
callq 0x2220
movl 0x66fa7(%rip), %eax     # 0x6e340
movl %eax, 0x1c(%rsp)
leaq 0x688dc(%rip), %rdi     # 0x6fc80
xorl %eax, %eax
movl %eax, %esi
callq 0x1cc00
movq %rax, 0x20(%rsp)
leaq 0x688c7(%rip), %rdi     # 0x6fc80
callq 0x1cc20
movl 0x1c(%rsp), %edi
movq 0x20(%rsp), %rsi
movl %eax, %edx
callq 0x364e0
cltq
movq %rax, 0x68889(%rip)     # 0x6fc60
cmpq $0x0, 0x68881(%rip)     # 0x6fc60
jle 0x73ea
cmpl $0x0, 0x689f8(%rip)     # 0x6fde0
jne 0x7414
movl 0x66f50(%rip), %edi     # 0x6e340
callq 0x364b0
cmpl $0x0, 0x66f44(%rip)     # 0x6e340
jl 0x7408
movl $0xfffffffe, 0x66f38(%rip) # imm = 0xFFFFFFFE
movl 0x66f32(%rip), %eax     # 0x6e340
movl %eax, 0x689cc(%rip)     # 0x6fde0
jmp 0x7422
movl 0x66f24(%rip), %eax     # 0x6e340
movl %eax, 0x689be(%rip)     # 0x6fde0
jmp 0x7424
movl 0x689b6(%rip), %eax     # 0x6fde0
addq $0x158, %rsp            # imm = 0x158
retq
nopw %cs:(%rax,%rax)