// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %r14
pushq %rbx
subq $0xe8, %rsp
movl %edi, %ebx
leaq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x2250
movq (%r14), %rax
subq 0x34817(%rip), %rax     # 0x3ad98
cmpq $0x6, %rax
jl 0x670f
cmpl $0x0, 0x32d8e(%rip)     # 0x39320
jns 0x65e6
movabsq $0x302e302e302e30, %rcx # imm = 0x302E302E302E30
cmpq %rcx, 0x33367(%rip)     # 0x3990c
sete %cl
testl %ebx, %ebx
setle %dl
cmpq $0xb, %rax
setl %al
orb %cl, %dl
orb %al, %dl
jne 0x65e6
movl 0x3344c(%rip), %esi     # 0x39a0c
leaq 0x33345(%rip), %rdi     # 0x3990c
callq 0x20786
movl %eax, 0x32d4e(%rip)     # 0x39320
testl %eax, %eax
jns 0x65e6
movq 0x8(%rsp), %rax
addq $0x3c, %rax
movq %rax, 0x347b2(%rip)     # 0x3ad98
movl 0x32d34(%rip), %eax     # 0x39320
testl %eax, %eax
jle 0x6709
movq 0x8(%rsp), %rax
movq %rax, 0x34798(%rip)     # 0x3ad98
leaq 0x34639(%rip), %rdi     # 0x3ac40
movl $0xc, %esi
callq 0x152b0
leaq 0x34640(%rip), %rdi     # 0x3ac58
movl $0xf, %esi
callq 0x152b0
cmpl $0x0, 0x32cfb(%rip)     # 0x39324
jle 0x6720
addl 0x3476b(%rip), %ebx     # 0x3ada0
xorl %eax, %eax
testl %ebx, %ebx
cmovgl %ebx, %eax
movl %eax, 0x3475e(%rip)     # 0x3ada0
cmpl $0xbb7, 0x32cd8(%rip)   # imm = 0xBB7
jg 0x6658
movl $0xbb8, 0x32ccc(%rip)   # imm = 0xBB8
movl 0x32cc6(%rip), %ecx     # 0x39324
cmpl %ecx, %eax
jle 0x6668
movl %ecx, 0x34738(%rip)     # 0x3ada0
movabsq $0x601060000000100, %rax # imm = 0x601060000000100
leaq 0x16(%rsp), %rsi
movq %rax, (%rsi)
movw $0xe803, 0x8(%rsi)      # imm = 0xE803
movl 0x3471a(%rip), %eax     # 0x3ada0
movl %eax, %ecx
shll $0x8, %ecx
leal 0xff(%rax), %edx
testl %eax, %eax
cmovnsl %eax, %edx
shrl $0x8, %edx
addl %ecx, %edx
movw %dx, 0xa(%rsi)
movq 0x3459a(%rip), %rdi     # 0x3ac40
movq 0x3459b(%rip), %rdx     # 0x3ac48
subq %rdi, %rdx
callq 0x21f0
movl 0x32c65(%rip), %edi     # 0x39320
movq 0x3457e(%rip), %rsi     # 0x3ac40
movl 0x34580(%rip), %edx     # 0x3ac48
subl %esi, %edx
callq 0x20a84
cltq
movq %rax, 0x34548(%rip)     # 0x3ac20
testl %eax, %eax
jle 0x66e5
cmpl $0x0, 0x346bd(%rip)     # 0x3ada0
jne 0x670f
movl 0x32c35(%rip), %edi     # 0x39320
callq 0x20a6d
cmpl $0x0, 0x32c29(%rip)     # 0x39320
js 0x6703
movl $0xfffffffe, 0x32c1d(%rip) # imm = 0xFFFFFFFE
movl 0x32c17(%rip), %eax     # 0x39320
movl %eax, 0x34691(%rip)     # 0x3ada0
movl 0x3468b(%rip), %eax     # 0x3ada0
addq $0xe8, %rsp
popq %rbx
popq %r14
retq
movabsq $0x301060000000100, %rax # imm = 0x301060000000100
leaq 0x16(%rsp), %rsi
movq %rax, (%rsi)
movl $0x100f603, 0x8(%rsi)   # imm = 0x100F603
movq 0x34500(%rip), %rdi     # 0x3ac40
movq 0x34501(%rip), %rdx     # 0x3ac48
subq %rdi, %rdx
callq 0x21f0
movl 0x32bcb(%rip), %edi     # 0x39320
movq 0x344e4(%rip), %rsi     # 0x3ac40
movl 0x344e6(%rip), %edx     # 0x3ac48
subl %esi, %edx
callq 0x20a84
xorl %edi, %edi
callq 0x21b0
movl 0x32baa(%rip), %edi     # 0x39320
movq 0x344db(%rip), %rsi     # 0x3ac58
movl 0x344dd(%rip), %edx     # 0x3ac60
subl %esi, %edx
callq 0x20adc
cltq
movq %rax, 0x3448d(%rip)     # 0x3ac20
testl %eax, %eax
jle 0x662f
movq 0x344b6(%rip), %rax     # 0x3ac58
cmpb $0x3, 0x7(%rax)
jne 0x662f
movzwl 0x9(%rax), %eax
rolw $0x8, %ax
movzwl %ax, %eax
movl %eax, 0x32b67(%rip)     # 0x39324
jmp 0x662f