多个TCP到串行连接的python尝试。

时间:2018-06-29 20:28:58

标签: python tcp serial-port raspberry-pi3

因此,我正在尝试创建一个python脚本,该脚本将允许来自不同计算机的多个TCP连接,并允许通过TCP客户端进行串行通信。这是我在玩的

import sys
import os
import time
import fileinput
import socket  
import serial
import thread
serialData = serial.Serial('/dev/ttyS0',9600)

import argparse


def on_new_client(clientsocket,addr):
        while True:
            msg = clientsocket.recv(1024) 
            #do some checks and if msg == someWeirdSignal: break:
            print addr, ' >> ', msg
            serialData.write(msg)
            #msg = raw_input('SERVER >> ') 
            #Maybe some code to compute the last digit of PI, play game or 
anything else can go here and when you are done.
            clientsocket.send(msg) 
        clientsocket.close()


def on_Send_client(clientsocket,addr):
        while True:
            x = serialData.readline()
            clientsocket.send(x) 
        clientsocket.close()


s = socket.socket()
host = '' #ip of raspberry pi
port = 12345
s.bind((host, port))
s.listen(5)



serialData.isOpen()

serialData.write("This is a test")




while True:


  c, addr = s.accept()

  thread.start_new_thread(on_new_client,(c,addr))
  #thread.start_new_thread(on_Send_client,(c,addr))

s.close()  

现在,我可以连接多个TCP客户端并保存其数据。他们的数据也从串口发送出去。我应该如何缓冲串行数据并将其发送回活动的所有连接?似乎我在网上找不到的东西都可以正确地执行此操作,并且Socat命令甚至无法进行多个串行连接。

0 个答案:

没有答案