Search code examples
pythonmatlabcsvsimulink

Reading data from Raspberry pi using TCP/IP block


I am trying to send data to the raspberry pi using TCP/IP Send block from Simulink. Can anyone suggest how I can read data from raspberry pi and write it to the CSV format ??


Solution

  • You can try scripting a program to listen for the TCP message, and dump it to a CSV file in the same program.

    For example, using python (copy-pasted with some modifications from https://wiki.python.org/moin/TcpCommunication) with socket library, you can write a program running in raspi that listens for the csv messages:

    import socket
    
    
    TCP_IP = '127.0.0.1'
    TCP_PORT = 5005
    BUFFER_SIZE = 1024
    
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((TCP_IP, TCP_PORT))
    s.listen(1)
    
    data_rcv = ''
    conn, addr = s.accept()
    print 'Connection address:', addr
    while 1:
        data = conn.recv(BUFFER_SIZE)
        if not data: break
        data_rcv += data
    conn.close()
    
    with open('/path/to/csv', 'w') as csvfile:
      csvfile.write(data_rcv)
    

    I believe python comes with Raspbian by default.