0

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.. enter image description here

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??

Rabindra
  • 19
  • 5
  • These are many steps in one without any intermediate output. What data type does the const output? What data type the pack (should be uint8 to match your python code)? Did you try adding a display to the output of the pack, does it show reasonable data? When you print "data" in python, it should match the content you see in the display. – Daniel Jan 30 '20 at 21:39
  • @Daniel, thank you for your suggestions. Adding a display to the output of the pack shows reasonable data (in my case constant 1). On each block, my data type is 'uint8'. When I print 'data' in python, all I am getting now is 'ddddddddddddddddddddddd'. – Rabindra Jan 30 '20 at 23:21

0 Answers0