0

I as a tcp server need to connect a PLC as a tcp client what never send any messages to me and meantime i need to send data to tell the plc to do some commands. Now I have problem about how to connect the tcp client when it closed the connection. Now my thinking is to create a main thread what to send some important data to the plc and a sub thread what to repeat keep connection for the plc. Everytime the tcp server must send data in 0.5 seconds. The plc have 0.5 seconds to get these data in industry and then it will into a next loop. The main thread is a image classification model and only haves.0.5 seconds to predict a image. For the sub thread,how to keep the tcp connection when the plc close connection actively? What is a PLC ? That is the working environment with the PLC My code is here

main thread code

    def send_data(self, data, tcpCilSock):
        self.setting.send_data = data.encode('utf-8')
        respMsg = self.setting.send_data
        tcpCilSock.sendall((bytes(respMsg)))
        log.info(data)
def run_thread(self):
      while True:
          try:
          if setting.is_tcp_client:
             tcpSvr.send_data(send_data, setting.tcpCilSock)
             break
          except:
              log.info('main thread is failed')

a sub thread

    def run(self):
        while True:
            try:                    
               self.tcpSvr.send_data(content, setting.tcpcilsock)
            except:
               log.info('sub thread is failed')


TT Aano
  • 1
  • 1
  • Wait, so is the connection lost because the client closed it or due to inactivity? – Homer512 Feb 23 '23 at 09:05
  • If it is being closed from inactivity, you can consider [the answer in this post](https://stackoverflow.com/questions/12248132/how-to-change-tcp-keepalive-timer-using-python-script) – Shorn Feb 23 '23 at 09:11
  • Today i has known the keepalive function, i need send data in 0.5 seconds to the plc, but the keepalive function only surpport minimum time is a second. – TT Aano Feb 23 '23 at 09:34
  • A _tcp server_ does not _connect_, a _tcp client_ does, while the server does _accept_. – Armali Feb 23 '23 at 14:14
  • sorry I don't understand what do you mean. why a tcp server can accept ,but the tcp server does not connect. – TT Aano Feb 23 '23 at 15:15
  • That's the way it is - or do you claim that your so-called _tcp server_ calls `connect`? – Armali Feb 23 '23 at 15:29
  • Since the client initiates a connection, the client is as well responsible for re-initiating it after it has been closed. – Armali Feb 23 '23 at 15:33
  • My question is when the tcp client closed the connection. the server need quickly detect the connection has been closed and then must reconnection and meantime the server need send data in per 0.5 seconds. Do you have any way to solve the problem about the server – TT Aano Feb 23 '23 at 16:10
  • Where do these 0.5 seconds come from? Is that your software requirement, that you have to detect as disconnection in 0.5s? If so, I'm not sure this is even feasible with TCP/IP. What if the PLC loses power? All timeouts will be considerably longer and/or less predictable. – Homer512 Feb 23 '23 at 20:43
  • the plc have 0.5 seconds to get these data in industry and then it will into a next loop. The main thread is a image classification model and only haves.0.5 seconds to predict a image. for the sub thread, how to keep the tcp connection when the plc close connection actively? – TT Aano Feb 24 '23 at 00:45
  • A TCP connection is a lot like a phone call -- either party can disconnect, and if they do, there is nothing the other party can do about it (except maybe call back?). So there's nothing you can do to prevent the PLC from closing the TCP connection if it wants to. – Jeremy Friesner Feb 24 '23 at 01:07
  • That's the next thing: What do you plan to when the PLC dropped the connection? The server cannot reopen it. I think you have to fix your PLC to not drop the connection or immediately reopen it. All the server can do is give an error message when it doesn't have a connection at the required time – Homer512 Feb 24 '23 at 08:44

0 Answers0