port_close waits for the port to be idle before it closes,
and I think thats the problem...
Try changing the port_close step to this...
...
}
port_close(hPort);
while(proc_status(port_close)) wait(1);
//
//
sys_exit("");
}
Let me know how it goes... any changes in behaviour.
Then if its fixed I'll explain what was happening...