Communicate over TCP/IP between Pi3B+ and PC

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 8.

Moderator: Benj

Post Reply
kamorn
Posts: 4
Joined: Mon Jan 21, 2019 8:22 pm
Contact:

Communicate over TCP/IP between Pi3B+ and PC

Post by kamorn »

Hi every one, could you give me some example of flowcode about how to communicate between Pi3B+ and PC. Actually, I tried to use flowcode make Pi to Server and I monitor package over python on desktop mode of pi but It fail to read package when I sent from pc over python.

Moreover, I would like to know about function Comms:Networking. what is difference between Network Communications and TCP/IP (Raspberry Pi). That I confuse, can I use TCP/IP (Raspberry Pi) communicate between Pi and PC? or just use only Pi to Pi. Addition, can I use Network Communication function over PI. Please, explain to me.

Thank.

In below is python code that I search form internet.

PC_Client.py

Code: Select all

import socket
import sys

while(1):
    HOST, PORT = "192.168.2.45", 5052
    data ="Test connection from Radio 3"
    data=input()

    #Create socket (SOCK_STREAM means a TCP socket)
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
        #connect to server and send data
        sock.connect((HOST,PORT))
        sock.sendall(bytes(data+"\n","utf-8"))

        #receive data from the server and shut down
        received = str(sock.recv(1024),"utf-8")
    print("Sent:    {}".format(data))
    print("Received:{}".format(received))
Mater.py

Code: Select all

import socketserver
import sys

class MyTCPHandler(socketserver.BaseRequestHandler):
 
     def handle(self):
        # self.request is the TCP socket connected to the client
        self.data = self.request.recv(1024).strip()
        print("{} wrote:".format(self.client_address[0]))
        #print(self.data)
        #print(str(self.data),"utf-8")
        print("{}".format(self.data))
        # just send back the same data, but upper-cased
        #self.request.sendall(self.data.upper())
        self.request.sendall(bytes("Accept from server!!","utf-8"))


        ####Send to another instrumentd
        
        
if __name__ == "__main__":
    HOST, PORT = "192.168.2.45", 5052

    # Create the server, binding to localhost on port 9999
    server = socketserver.TCPServer((HOST, PORT), MyTCPHandler)

    # Activate the server; this will keep running until you
    # interrupt the program with Ctrl-C
    server.serve_forever()
Attachments
Pi3B+.fcfx
(14.65 KiB) Downloaded 128 times

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: Communicate over TCP/IP between Pi3B+ and PC

Post by LeighM »

Hi,
Your code is very close to working.
It's just that the sequence is: Open, Listen, then call AcceptOpen in the loop.
AcceptOpen will return true if there is an incoming connection, in which case then receive the data then call AcceptClose when the transactions are complete. Ready to go back into the AcceptOpen loop waiting for a new incoming connection.
I've attached a similar example that I hope will help.

ps. The Pi TCPIP component will work as a connection to another Pi or PC.
The NetworkComms component is intended as a wrapper for our Flowcode network components in an attempt to give them some common functionality and PC simulation capability.
Attachments
BL0036_TCP_SERVER.fcfx
(12.99 KiB) Downloaded 127 times

Post Reply