Simulink TCP/IP communication with Raspberry PI

半城伤御伤魂 提交于 2020-03-04 18:46:50

问题


I am trying to send data from my Simulink simulation to the Raspberry Pi on the same network and write the data on CSV format. I am using the following python code to read data and write it to CSV file.

import csv
import socket

TCP_IP = '192.168.1.8'
TCP_PORT = 47899
BUFFER_SIZE = 20

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(4)

data_rcv=''
conn, addr = s.accept()

with open('mycsv.csv','w') as csvfile:
#        csvfile.write(u'\ufeff'.encode('utf8'))
        writer = csv.writer(csvfile)
        writer.writerow(["Value"])

print 'Connection address:',addr
while 1:
    data = conn.recv(BUFFER_SIZE)
    if not data: break
    data_rcv += data

with open('mycsv.csv','a') as csvfile:
#        csvfile.read(u'\ufeff'.encode('utf8'))
        reader = csv.writer(csvfile)
        reader.writerow(data_rcv)

conn.close()

This is my simple simulink simulation to test the connection..

The connection is fine and working. Every time I run the simulation, some random Chinese characters are written on my CSV file instead of numbers that I have actually sent. How can I change the data format so that it's actually readable?? And, how can I configure my program to read and write the real-time simulated data continuously on CSV file??

来源:https://stackoverflow.com/questions/59957624/simulink-tcp-ip-communication-with-raspberry-pi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!