import Net.*
%建立连接
net = Net();
net = net.connectNet('127.0.0.1', 9515);
data = zeros(4, 6);
for i = 1 : 4
for j = 1: 6
data(i, j) = i + j;
end
end
%发送数据
net = net.sendWarnTarget( 123, 1, 38.123, 128.34, 1234567, 890, 1000, data);
%关闭TCP连接
net = net.closeNet();
下面是网络通信模块,文件名为 Net.m,以类的形式封装提供调用,与其它语言编写的应用程序时注意设置大小端,matlab语言所有的类型都是double类型,因此fwrite时注意标注希望转换的数据类型,matlab网络数据默认以大端法发送,而x86机器一般都是小端法,因此在tcpip()函数调用时指定好数据发送的大小端法。
classdef Net
%NET 此处显示有关此类的摘要
% 此处显示详细说明
properties
%连接对象
m_tcp_net
end
methods
%与服务端建立连接
function obj = connectNet( obj, ip, port)
obj.m_tcp_net = tcpip(ip, port,'NetworkRole', 'client', 'ByteOrder','littleEndian', 'OutputBufferSize', 4 * 1024 * 1024);
fopen(obj.m_tcp_net );
end
%关闭与服务端的连接
function obj = closeNet(obj)
fclose(obj.m_tcp_net);
end
%发送包头数据,内部使用
function obj = sendPkHead(obj, pkSize, pkType, pkExtern, pkReserve)
fwrite( obj.m_tcp_net, pkSize, 'uint32' );
fwrite( obj.m_tcp_net, pkType, 'uint8');
fwrite( obj.m_tcp_net, pkExtern, 'uint8' );
fwrite( obj.m_tcp_net, pkReserve, 'uint16' );
end
function obj = sendWarnTarget( obj, eventID, eventType, tarLati, tarLng, utcSec, utcMSec, fs, data)
%发送包头数据
%|--事件ID(uint32)---||--事件类型(uint32)--||--纬度(float32)--||--经度(float32)--||--utc时间(uint32)--||--utc毫秒(uint32)--||--采样频率(uint32)--|
pkSize = 4 *7 + size(data, 1) * size( data, 2) * 8;
pkType = 1;
pkExtern = size(data, 1); %数组行数
pkReserve = size( data, 2);%数组列数
obj.sendPkHead( pkSize, pkType, pkExtern, pkReserve);
%发送包体数据
fwrite(obj.m_tcp_net, eventID, 'uint32' );
fwrite( obj.m_tcp_net, eventType, 'uint32' );
fwrite( obj.m_tcp_net, tarLati, 'float32' );
fwrite( obj.m_tcp_net, tarLng, 'float32' );
fwrite( obj.m_tcp_net, utcSec, 'uint32' );
fwrite( obj.m_tcp_net, utcMSec, 'uint32' );
fwrite( obj.m_tcp_net, fs, 'uint32' );
%包体的波形数据,因为matlab的数据内存是以列为主序,因此将数据数据置后按内存发送,对多行的数据必须转化成一行发送
data2 = reshape(data', 1, 1 * size(data, 1) * size(data,2));
fwrite( obj.m_tcp_net, data2, 'double');
end
end
end
下面是服务端数据接收测试程序,python3.0或以上版本可正常运行
#coding=gbk
from socket import *
from time import ctime
import struct
import array
def recvData( client, dataSize ):
data = b''
while dataSize > 0:
datat = tctimeClient.recv(dataSize);
if not datat:
data = None
break
data += datat
dataSize -= len(datat);
return data;
host = ''
port = 9515
ADDR = (host,port)
tctime = socket(AF_INET,SOCK_STREAM)
tctime.bind(ADDR)
tctime.listen(3)
while True:
print('Wait for connection ...')
tctimeClient,addr = tctime.accept()
print("Connection from :",addr)
while True:
#接收8字节的包头
data = recvData( tctimeClient, 8);
if not data:
break;
pkSize, pkType, pkExtern, pkReserve = struct.unpack('IBBH', data);
print( '包大小:', pkSize);
print( '包类型:', int(pkType));
print( '数据行:', int(pkExtern));
print( '数据列:', pkReserve);
#接收包体数据
data = recvData(tctimeClient, pkSize);
if not data:
break;
eventID, eventType, tarLati, tarLng, utcSec, utcMSec, fs = struct.unpack('IIffIII', data[0: 28])
print( '事件ID:', eventID);
print( '事件类型:', eventType);
print( '纬度:', tarLati);
print( '经度:', tarLng);
print( 'utc秒:', utcSec);
print( 'utc毫秒:', utcMSec);
print( '频率:', fs);
#wave = array.array( 'd', data[28:]);
#for i in range( int(pkExtern)):
# for j in range( int(pkReserve)):
# print( '%0.1f' % wave[i * pkReserve + j ], end = ', ')
# print('\n');
tctimeClient.close()
tctimeClient.close()