view DDS4xAD9959.c @ 183:791ca26fee6a

Rewrite data writing to file Fmt() uses by default truncation to conver double arguments to their text representation. Rounding must be used. Rewrite using standard C functions to get rid of this problem (and probably make it more efficient). Extend to handle arbitrary number of channels and to report errors on opening the data files.
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Fri, 21 Feb 2014 18:42:30 +0100
parents d9aae7d7f2c6
children
line wrap: on
line source


//==============================================================================
//
// Title:       DDS4xAD9959.c
// Purpose:     
//
// Created on:  by Volodimer Laurent, modified by YLC
// Copyright:   syrte. All Rights Reserved.
//
//==============================================================================

//==============================================================================
// Include files

#include "DDS4xAD9959.h"
#include <userint.h>
#include <tcpsupp.h>
#include <ansi_c.h>

//==============================================================================

// Global functions

static int OnTCPEvent(unsigned handle, int xType, int errCode, void *callbackData)
	{
	switch(xType)
		{
		case TCP_DISCONNECT :
			break;
		case TCP_DATAREADY :
			ClientTCPRead (handle, Response, sizeof(Response), 0);
			break;
		}
		return 0;
	}


//==============================================================================
// DDS4xAD9912_SendCmd
//
// FUNC   : envoie une chaine de caracteres sur un port du serveur TCP/IP de la foxboard
// PARAM  : DDSNum contient le numero de la DDS (1,2,3 ou 4)
// PARAM  : Buffer contenant la chaîne de caractères à envoyer
// RETURN : 0 si la connexion et l'ecriture sur le serveur TCP/IP sont OK
//==============================================================================


static int DDS4XAD9959_SendCmd(int DDSNum, char * Buffer)  // essentially a private function
	{
	int res_1,res_2;

	switch(DDSNum) 
		{
		case 1:
			res_1=ConnectToTCPServer (&hConv, Port1, Adress, OnTCPEvent, 0, 0);
			if(res_1 <0) MessagePopup("DDS4XAD9959_1 Serveur", "Connection to server failed !");	
			break;
		case 2:
			res_1=ConnectToTCPServer (&hConv, Port2, Adress, OnTCPEvent, 0, 0); 
			if(res_1 <0) MessagePopup("DDS4XAD9959_2 Serveur", "Connection to server failed !");
			break;
		
		 case 3:
			res_1=ConnectToTCPServer (&hConv, Port3, Adress, OnTCPEvent, 0, 0); 
			if(res_1 <0) MessagePopup("DDS4XAD9959_3 Serveur", "Connection to server failed !");
			break;
		
		 case 4:
			res_1=ConnectToTCPServer (&hConv, Port4, Adress, OnTCPEvent, 0, 0); 
			if(res_1 <0) MessagePopup("DDS4XAD9959_4 Serveur", "Connection to server failed !");
			break;
		
		}
	
	res_2=ClientTCPWrite (hConv, Buffer, strlen(Buffer)+1, 0);
	if(res_2 <0) MessagePopup("DDS Serveur", "Write to server failed !");
	DisconnectFromTCPServer (hConv);
	return (res_1+res_2) ;
	}


//==============================================================================
// DDS4xAD9912_Reset
//
// FUNC   : reset des 4 DDS
// PARAM  :
// RETURN : 0 si le reset de la DDS 1 , DDS 2, DDS3 et DDS4 est OK
//==============================================================================

 int DDS4XAD9959_Reset(DDS4XAD9959_Data * Instance)
 	{
	int res_1,res_2;
	
	sprintf(Request,"set;%i;%i",1,17);
	res_1 = DDS4XAD9959_SendCmd(1,Request) ;
	if(res_1 <0) {
		MessagePopup("DDS Serveur", "DDS4XAD9959_1 Reset failed !") ;
		}
	else Instance->Frequency1 = 0.0 ;
	
	sprintf(Request,"set;%i;%i",1,17);
	res_2 = DDS4XAD9959_SendCmd(2,Request) ; 	 
	if(res_2 <0) {
		MessagePopup("DDS Serveur", "DDS4XAD9959_2 Reset failed !") ;
		}
	else Instance->Frequency2 = 0.0;

	 sprintf(Request,"set;%i;%i",1,17);
	res_2 = DDS4XAD9959_SendCmd(3,Request) ; 	 
	if(res_2 <0) {
		MessagePopup("DDS Serveur", "DDS4XAD9959_3 Reset failed !") ;
		}
	else Instance->Frequency3 = 0.0;
	
	 sprintf(Request,"set;%i;%i",1,17);
	res_2 = DDS4XAD9959_SendCmd(4,Request) ; 	 
	if(res_2 <0) {
		MessagePopup("DDS Serveur", "DDS4XAD9959_4 Reset failed !") ;
		}
	else Instance->Frequency4 = 0.0;
	
	
	return (res_1+res_2);	 
 	}	 


//==============================================================================
// DDS4xAD9912_SetFreq
//
// FUNC   : sort une fréquence sur la DDS1 ,DDS2, DDS3 ou DDS4
// PARAM  : DDSNum contient le numero de la DDS (1, 2 , 3 ou 4)
// RETURN : 0 si la connexion et l'ecriture sur le serveur TCP/IP sont OK
//============================================================================== 
int DDS4XAD9959_SetFrequency(DDS4XAD9959_Data * Instance, int DDSNum, double Freq)
	{
		
	double Word=WORD(Freq,Clk) ;
	double reste ;
	int res_0 ;
	unsigned char b5,b4,b3,b2,b1,b0 ; 

	b5=(unsigned char)(Word/E48);		   // AD9858 is 32 bits, so this one is always zero. For future extension only...
	b4=(unsigned char)((Word - E48*b5)/E32);
	b3=(unsigned char)((Word - E48*b5 -E32*b4)/E24);
	b2=(unsigned char)((Word - E48*b5 -E32*b4 -E24*b3)/E16); 
	b1=(unsigned char)((Word - E48*b5 -E32*b4 -E24*b3 -E16*b2)/E8);
	b0=(unsigned char)((Word - E48*b5 -E32*b4 -E24*b3 -E16*b2 -E8*b1)/1.0);
	reste = (Word - E48*b5 -E32*b4 -E24*b3 -E16*b2 -E8*b1-1.0*b0) ;

	sprintf(Request,"set;%i;%i;%i;%i;%i;%i;%i;%i",0,10,b5,b4,b3,b2,b1,b0); 
	res_0 = DDS4XAD9959_SendCmd(DDSNum, Request) ;
	if (res_0 <0) {
		MessagePopup("DDS Serveur", "SetFrequency function failed !") ;
		}
	else {
		if (DDSNum==1) Instance->Frequency1 = Freq - reste*Clk/E48 ;
		if (DDSNum==2) Instance->Frequency2 = Freq - reste*Clk/E48 ;
		if (DDSNum==3) Instance->Frequency3 = Freq - reste*Clk/E48 ;
		if (DDSNum==4) Instance->Frequency4 = Freq - reste*Clk/E48 ;
		} ;
	  	
	sprintf(Request,"set;%i;%i",0,18); // IO_Update logiciel 
	res_0 = DDS4XAD9959_SendCmd(DDSNum, Request) ;
	if (res_0 <0) {
		MessagePopup("DDS Serveur", "IO_Update function failed !") ;
		}
	return(res_0) ;
	
	}