⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demo_rt.c

📁 实现vxworks下的1553b通讯功能
💻 C
字号:
#include "vxWorks.h"
#include <stdio.h>
#include <timers.h>  
#include "proto.h"
#include "msgQlib.h"
char errstr[255];

#define Print_n_Exit(title,error )        \
{                                         \
	if (error < 0)                         \
	{  \
		Get_Error_String_MCH(error, 255, errstr);   	                     \
		printf("%s: %s\n",title,errstr);         \
		Release_Card_MCH();                     \
		return(0);                            \
	}                                   \
}



struct st_rtMsgInfo
{
    unsigned short subAddress;
};

MSG_Q_ID g_RtRecvTaskMsgID = NULL;


void MyInterruptHandler1(void);
void MyInterruptHandler2(void);
void RtRecvTsk();
unsigned int numints1, numints2, oldnumints1;
extern pingpong;
extern curchan;


int demo_rt (void)
{
	int error, devnum, ioportnum, module1, module2, intlev;
        int rlt = -10;
	usint device, ioport;
	struct timespec tp1,tp2;
	unsigned char it;

	/*
	* This sample program shows how to set up two channels as two
	* Remote Terminals and move data from one to the other in real time
	*/

	numints1 = 0;
	numints2 = 0;
	oldnumints1 = 0;
 	devnum = 0xd0;
	device = (usint)devnum;
	ioportnum = 0x280; 
	ioport = (usint)ioportnum;

	error = Init_Card_MCH(device, ioport);
	if (error < 0)
	{
	   	Get_Error_String_MCH(error, 255, errstr);
		printf("Init_Card_MCH Failure. %s\n", errstr);
		return 0;	
	}
   	intlev = 5;
	error = Set_Interrupt_Level(intlev);
	Print_n_Exit( "Set_Interrupt_Level FAILURE:  ", error );


	g_RtRecvTaskMsgID = msgQCreate (100, 100, MSG_Q_FIFO);
    	if (g_RtRecvTaskMsgID == NULL)
	{
    	printf("\r\nrecvTsk create fail.");
    	return;
	}

    	taskSpawn ("tRTRecv", 80, 0,5000, (FUNCPTR) RtRecvTsk,0,0,0,0,0,0,0,0,0,0);

	/* set up card as Remote Terminal 1 */
	module1 = 0;
	Select_Channel(module1);
	
	error = Set_Mode( RT_MODE);
	Print_n_Exit("RT_MODE_MCH FAILURE", error );
		
	/* set RT address to 1 */
	error = Set_RT_Num(1);
	Print_n_Exit("Setup_RT FAILURE", error );

	/* Request interrupt on subaddress 1 receive message. This routine
	*  may be called multiple times for different subaddress T/R
	*  combinations.
	*/
	/*mode code*/
	error = Enable_Subaddr_Int(16,TRANSMIT,MODECODE,ACCESS); /*vector word*/
	Print_n_Exit("Enable_vector_word",error );
	error = Enable_Subaddr_Int(19,TRANSMIT,MODECODE,ACCESS); /*transmit bit word*/
	Print_n_Exit("Enable_transmit_bit_word",error );
	/*standard mode*/	
	for(it=1;it<31;it++)
	{
		error = Enable_Subaddr_Int(it,RECEIVE,STANDARD,ACCESS);
		Print_n_Exit("Enable_Subaddr_Int",error );
		printf("it=%d",it);	
	}

	/* Allow interrupts on subaddress access. This is called once. */
	error = Set_Interrupt_Mask(SUBAD);
	Print_n_Exit("Set_Interrupt Mask",error );

    	taskDelay(1);
		
	/* Start the RT running */
	error = Run_RT();
	Print_n_Exit("Run_RT FAILURE",error );

	/* Set up our ISR */
	error = SetupInterruptHandler( (VOIDFUNCPTR) MyInterruptHandler1);
	Print_n_Exit("SetupInterruptHandler1", error);

	printf("\nBegin transmitting now\n\n");
	
	clock_gettime(CLOCK_REALTIME, &tp1);
	while (1)
	{
		/* update the screen with the number of interrupts that have been
		* received.  This should correspond to the number of receive
		* RT 1 subaddress 1 messages encountered.
		*/
		if (numints1 != oldnumints1)
		{
			oldnumints1 = numints1;
		}
		
		clock_gettime(CLOCK_REALTIME, &tp2);
		/*if ((numints1 > 200) || ((tp2.tv_sec - tp1.tv_sec) > 120))
			break;*/
	}
	Stop_Card();  
	ClearInterruptHandler();

	Select_Channel(module1);
	Stop_Card();  
	ClearInterruptHandler();

	Release_Card_MCH();

    	msgQDelete(g_RtRecvTaskMsgID);
	g_RtRecvTaskMsgID = NULL;
	
	printf("\n\nTest complete.\n\n");
	return (0);
} /* end main */

/***************************************************************************/

/* Interrupt routine for IRQ interrupt */

void MyInterruptHandler1(void)
{
    	int i = 0;
	unsigned ii=1;
	struct st_rtMsgInfo st_Msg;

	memset(&st_Msg,0,sizeof(struct st_rtMsgInfo)); 
	
	if(Datablk_Accessed(16,TRANSMIT,MODECODE,ACCESS)==1)
		st_Msg.subAddress = 0;
	else if(Datablk_Accessed(19,TRANSMIT,MODECODE,ACCESS)==1)
		st_Msg.subAddress = 31;
	else
	{	
		for(ii=1;ii<31;ii++)
		{	
			if(Datablk_Accessed(ii,RECEIVE,STANDARD,ACCESS)==1)
				st_Msg.subAddress = ii;
		}
	}
    	if( OK != msgQSend( g_RtRecvTaskMsgID, &st_Msg, sizeof(struct st_rtMsgInfo), NO_WAIT, MSG_PRI_NORMAL ))
 	{
 	    logMsg("msgQSend fail.\n");
 	}
	
	numints1++;
	return;
}

void MyInterruptHandler2(void)
{
	numints2++;
	return;
}


void RtRecvTsk()
{

    	struct st_rtMsgInfo st_Msg;
	unsigned char subaddr_temp;
	unsigned short int rcvbuf[34];
	unsigned char vv,vv1;


   memset(&st_Msg,0,sizeof(struct st_rtMsgInfo));

  while(1)
  {
     if(msgQReceive (g_RtRecvTaskMsgID, (char *)&st_Msg, 100, WAIT_FOREVER) < 1)
    {
           	/* err */
    }
    else
    {
	 int i = 0;
	/* get the data from subaddress 2 of the receiver */
	subaddr_temp = st_Msg.subAddress; 
	printf("\nsubaddr=%d\n",subaddr_temp);
	Read_Datablk(st_Msg.subAddress, 0, rcvbuf,STANDARD, RECEIVE);
	
	if((rcvbuf[0] & 0x00FF)==0x0000)	/*error check*/	
 	{   	
		switch(st_Msg.subAddress)
		{
			case 0:
				printf("mode code 0!\n");
				break;
			case 1:
				printf("hello subaddr1!\n");
				ConfigIO_One(0x80);
				IOW_OneA(0x00);
				vv1=IOR_OneA();
				printf("vv1=0x%x\n",vv1);
				break;
			case 2:
				printf("hello subaddr2!\n");
				ConfigIO_One(0x80);
				IOW_OneA(0x01);
				vv1=IOR_OneA();
				printf("vv1=0x%x\n",vv1);
				break;
			case 3:
				printf("hello subaddr3!\n");
				printf("hello code mode0!\n");
				ConfigIO_One(0x80);
				IOW_OneA(0xff);
				vv=IOR_OneA();
				printf("vv=0x%x\n",vv);
				break;
			case 4:
				printf("hello subaddr4!\n");
				break;
			case 5:
				rtMsgSend(rcvbuf,subaddr_temp);
				break;
			case 31:
				printf("hello code mode31!\n");
				break;
			default:
				break;
		}
		printf("\n\rRECV DATA:\n");
		    printf("%04x %04x %04x %04x %04x %04x\n",rcvbuf[0+i],rcvbuf[1+i],rcvbuf[2+i],rcvbuf[3+i],rcvbuf[4+i],rcvbuf[5+i],rcvbuf[6+i]);
		    i  = i + 6;
			printf("%04x %04x %04x %04x %04x %04x\n",rcvbuf[0+i],rcvbuf[1+i],rcvbuf[2+i],rcvbuf[3+i],rcvbuf[4+i],rcvbuf[5+i],rcvbuf[6+i]);
		    i  = i + 6;
		    printf("%04x %04x %04x %04x %04x %04x\n",rcvbuf[0+i],rcvbuf[1+i],rcvbuf[2+i],rcvbuf[3+i],rcvbuf[4+i],rcvbuf[5+i],rcvbuf[6+i]);
		    i  = i + 6;
		    printf("%04x %04x %04x %04x %04x %04x\n",rcvbuf[0+i],rcvbuf[1+i],rcvbuf[2+i],rcvbuf[3+i],rcvbuf[4+i],rcvbuf[5+i],rcvbuf[6+i]);
		    i  = i + 6;
		    printf("%04x %04x %04x %04x %04x %04x\n",rcvbuf[0+i],rcvbuf[1+i],rcvbuf[2+i],rcvbuf[3+i],rcvbuf[4+i],rcvbuf[5+i],rcvbuf[6+i]);
		    i  = i + 6;
			printf("%04x %04x %04x %04x\n",rcvbuf[0+i],rcvbuf[1+i],rcvbuf[2+i],rcvbuf[3+i]);

			/*rtMsgSend(rcvbuf);*/
			memset(rcvbuf,0,34);
	}
	else
	{

	}
            
      }
   }
}


int rtMsgSend(unsigned short * rcvbuf,unsigned char subaddress)
{
    	int i = 0;
    	unsigned  short int buf[32] = {0};

    	memcpy(buf,rcvbuf+2,64);

    	for(i = 0; i<32; i ++)
		buf[i]++;
	Load_Datablk(subaddress,0,buf);
	{/* print send data */
		i = 0;
		printf("\r\nSEND DATA:\n");
	    	printf("%04x %04x %04x %04x %04x %04x\n",buf[0+i],buf[1+i],buf[2+i],buf[3+i],buf[4+i],buf[5+i],buf[6+i]);
	    	i  = i + 6;
		printf("%04x %04x %04x %04x %04x %04x\n",buf[0+i],buf[1+i],buf[2+i],buf[3+i],buf[4+i],buf[5+i],buf[6+i]);
	    	i  = i + 6;
	    	printf("%04x %04x %04x %04x %04x %04x\n",buf[0+i],buf[1+i],buf[2+i],buf[3+i],buf[4+i],buf[5+i],buf[6+i]);
	    	i  = i + 6;
	    	printf("%04x %04x %04x %04x %04x %04x\n",buf[0+i],buf[1+i],buf[2+i],buf[3+i],buf[4+i],buf[5+i],buf[6+i]);
	    	i  = i + 6;
	    	printf("%04x %04x %04x %04x %04x %04x\n",buf[0+i],buf[1+i],buf[2+i],buf[3+i],buf[4+i],buf[5+i],buf[6+i]);
	    	i  = i + 6;
		printf("%04x %04x\n",buf[0+i],buf[1+i]);

	}	

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -