function r = ReadSonic(port_name)
	global port data datfile
	if nargin==0
		port_name = 'COM1';
	end
	port = serial(port_name, 'Baud', 4800);
	fopen(port);
	chk = set_analog_channels();
	if chk ~= 0
		fclose(port);
		delete(port);
		clear port;
		r = chk;
		return
	end
%	[filename, expires] = make_datfile_name();
%	datfile = fopen(filename, 'a');
	data = zeros(50, 7);
	read_sonic_data();
%	fclose(datfile);
	fclose(port);
	delete(port);
	clear port;
  r = 0;

function [name, expires] = make_datfile_name()
	clk = clock;
	jul = floor(datenum(clk) - datenum([2005 12 31]));
	ext = floor(48 * (datenum(clk) - floor(datenum(clk))));
	name = sprintf('c:\\sonic\\ec_%03d_%02d.dat', jul, ext);
	expires = jul + (ext + 1) / 48.0;
	
function chk = set_analog_channels()
	global port
	done = 0;
	ii = 0;
	while done == 0 &&  ii < 20
		port.requesttosend = 'off';
		port.requesttosend = 'on';
		fwrite(port, '666666', 'char');
		fwrite(port, '666666', 'char');
		fwrite(port, '666666', 'char');
		port.requesttosend = 'off';
		buff = fread(port, 80 ,'uchar');
		pairs = find(buff == 129);
		if pairs(3) + 12 > 80
				buff
				pairs
				ii = 21;
		else
			if buff(pairs(3) + 12) < 129
				done = 1;
			end
		end
		ii = ii + 1;
	end
	if done == 0 
			chk = 1;
	else
			chk = 0;
	end

function read_sonic_data()
	global port data datfile
	[filename, expires] = make_datfile_name();
	datfile = fopen(filename, 'a');
	data_idx = 0;
	oldbuff = [];

	plt = plot([0, data(1, 3)], [0, data(1, 2)], '-', 'LineWidth', 2);
	axis([-5.0 5.0 -5.0 5.0])
	axis square
	grid off
	set(plt,'EraseMode','xor','MarkerSize',15)
	col = [0.8 0.8 0.8];
	
%	for i = 1:20
	while 1
		buff = fread(port, 300 ,'uchar');
		buff = [oldbuff; buff];
		pairs = find(buff == 129);
		pairs(1) = [];
		temp = find(buff(pairs - 1) == 129);
		start = pairs(temp);
		for ii = 1:length(start) - 1
			data_idx = data_idx + 1;
			lobyte = [2 4 6 8 10 12 14] + start(ii);
			hibyte = [1 3 5 7 9 11 13] + start(ii);
			neg = find(buff(hibyte) >= 128);
			buff(hibyte(neg)) = buff(hibyte(neg)) - 256;
			data(data_idx, 1:7) = buff(hibyte) * 256 + buff(lobyte);
		end
		oldbuff = buff(start(length(start)) - 3:length(buff));
		data(1:data_idx, 2:4) = data(1:data_idx, 2:4) / 100.0;
		data(1:data_idx, 5) = data(1:data_idx, 5) / 50.0;
		data(1:data_idx, 6:7) = data(1:data_idx, 6:7) / 50.0;
		for ii = 1:data_idx
			fprintf(datfile,  '%6.0d %8.2f %8.2f %8.2f %8.2f %8.3f %8.3f \r\n', data(ii, 1:7));
		end
    set(plt, 'xdata', [0, data(data_idx, 3)], 'ydata', [0, data(data_idx, 2)]);
		text(5.5, 4.5, sprintf('u: %5.1f', data(data_idx, 2)), 'fontname', 'fixedwidth', 'background', col);
		text(5.5, 4.0, sprintf('v: %5.1f', data(data_idx, 3)), 'fontname', 'fixedwidth', 'backgroundcolo', col);
		text(5.5, 3.5, sprintf('w: %5.1f', data(data_idx, 4)), 'fontname', 'fixedwidth', 'backgroundcolo', col);
		text(5.5, 3.0, sprintf('c: %5.1f', data(data_idx, 5)), 'fontname', 'fixedwidth', 'backgroundcolo', col);
		text(5.5, 2.5, sprintf('1: %5.1f', data(data_idx, 6)), 'fontname', 'fixedwidth', 'backgroundcolo', col);
		text(5.5, 2.0, sprintf('2: %5.1f', data(data_idx, 7)), 'fontname', 'fixedwidth', 'backgroundcolo', col);
		drawnow;
		data_idx = 0;
		if datenum(clock) - datenum([2005 12 31]) > expires
			fclose(datfile)
			[filename, expires] = make_datfile_name();
			datfile = fopen(filename, 'a');
		end
	end
	fclose(datfile);


