hi,
I've tried to use the function start_rt_timer(...) in my code but
nothing happens.
I've controlled that module rtcfg is loaded.
Could you tell me where have i to insert the start_rt_timer(...) in the code?
I'm working in user space.
code:
// MASTER [M1]
#ifdef HAVE_CONFIG_H
#include <config.h> //lpx: ???
#endif
#include <iostream> //lpx: ???
#include <cstdlib> //lpx: ???
#include <sys/socket.h> //lpx: socket, PF_INET, SOCK_DGRAM
#include <netinet/in.h> //lpx: socket, PF_INET, SOCK_DGRAM
#include <arpa/inet.h> // for inet_addre()
//lpx: necessarie per cambiare la priorità di un processo
#include <sys/time.h>
#include <sys/resource.h>
//lpx: necessario per sprintf()
#include <stdio.h>
//lpx: lettura del tempo
#include <time.h>
#include <rtnet.h>
using namespace std;
int main(int argc, char *argv[])
{
RT_TASK *task_rt; //lpx: task real time
int udpSocket; //lpx: Socket UDP
int nFamily=AF_INET; //lpx: protocollo di rete: IP
int nType=SOCK_DGRAM; //lpx: protocollo di trasporto: UDP
int nProtocol = 0; //lpx: ???
int nAddr=INADDR_ANY; //lpx: Questo indirizzo
int nPort=15000; //lpx: porta 5000,...
// indirizzo interno
struct sockaddr_in MyAdrs; //lpx: indirizzo IP
memset( &MyAdrs, 0, sizeof( MyAdrs ) ); //lpx: azzero variabile
// primo Socket
MyAdrs.sin_family = nFamily; //lpx: tipo di indirizzo
MyAdrs.sin_addr.s_addr = htonl( nAddr ); //lpx: indirizzo IP host
MyAdrs.sin_port = htons( nPort ); //lpx: porta su cui collegarsi
// indirizzi degli slave a cui mi devo connettere
char slaveAddress[16] = "192.0.0.255\0";
int slavePort=6000;
// l'inirizzo a cui invierò sarà di tipo broadcast
struct sockaddr_in slaveAdrs; //indirizzo del server che si vuole connettere
int len; //dimensione della struttura
masterAdrs
// i valori li otterro dal primo mesaggio che mi giunge
memset(&slaveAdrs, 0, sizeof( slaveAdrs ) ); //lpx: azzero variabile
// socket verso il primo slave S1
slaveAdrs.sin_family = nFamily; //lpx: tipo di indirizzo
slaveAdrs.sin_addr.s_addr = inet_addr(slaveAddress); //lpx: indirizzo IP host
slaveAdrs.sin_port = htons(slavePort); //lpx: porta su cui collegarsi
const int BufferLen=256; //lpx: lunghezza buffer
di trasmissione
int BufferLenRx;
char Buffer[BufferLen]; // lpx: punta ai dati da trasmettere
char BufferRx[BufferLen]; //lpx: buffer di
ricezione
Buffer[0]=0;
for (int j=1; j<BufferLen; j++)
{// il Buffer[0] lo uso per inserire il numero di datagram
Buffer[j]=j;
}
int flags=0; // lpx: vedi info sendoto() o recvfrom()
int a,i; // a: ritorno errore , i indice
const char SLAVE=2; //numero di slave
unsigned long int slaveIP[SLAVE] =
{inet_addr("192.0.0.235"),inet_addr("192.0.0.244")};
char response_slave[SLAVE]; //se uno slave risponde pongo a 1 il
suo byte
char i_slave; //indice slave
char j_slave; //indice slave
char flag_slave;
struct sockaddr_in slaveRx; //indirizzo dello slave che ha risposto
socklen_t lenRx; //dimensione della
struttura masterAdrs
int add_rtskbs=100; //numero di buffer in più da allocare per il socket
int new_rtskbs; //contiene la quantità dei nuovi buffer realmente allocata
RTIME t_start; //tempo in cui ho spedito il pkt
RTIME t_stop;
RTIME t_sum; //sommatoria delle differenze
const int Npkt=100/*200000*/; //Numero di pkt da inviare :x1ora: 7.2M
//nice(-15);
long TimeOut; //timeout, espresso in ns
// creo i socket
udpSocket = socket_rt( nFamily, nType, nProtocol); //lpx: inizializzo
un soket di tipo IP/UDP
if (udpSocket<0)
{
printf("errore Socket non creato %d!\n",udpSocket);
//devo rimuovere i socket precedentemente creati
close_rt(udpSocket); //chiudo il socket
exit(1);
}
//aggiungo nuovi buffer al socket
new_rtskbs=ioctl_rt(udpSocket,RTNET_RTIOC_EXTPOOL,&add_rtskbs);
if (new_rtskbs!=add_rtskbs) // buffer allocati sono in egual numero a
quelli richiesti?
{ //non ho allocato tutti i buffer
close_rt(udpSocket);
printf("non creati new buffer = %d \n",new_rtskbs);
exit(1);
}
printf("new buffer = %d \n",new_rtskbs);
// faccio l'associazione tra soket e porta
if (bind_rt(udpSocket,(sockaddr*)&MyAdrs,sizeof(MyAdrs))<0) //lpx:
effettuo il Bind
{ //asssociazione nn riuscita
close_rt(udpSocket);
printf("errore BIND\n");
exit(1);
}
// setto il time_out
TimeOut=500000000; //7ms
ioctl_rt(udpSocket,RTNET_RTIOC_TIMEOUT,&TimeOut);
// inizializzo il task real-time, tutti i metodi usati devono essere RT
task_rt = rt_task_init(4901,1,0,0); //Id_Task, priorità, ...
if (task_rt==NULL) // controllo che il task sia stato creato
{
close_rt(udpSocket); //chiudo il socket
printf("task non creato\n");
exit(1);
}
start_rt_timer(50000);
i=0; //i conta i segmenti
t_sum=0;
while (i<Npkt) //aspetto l'invio di 10 segmenti 1200000
{
*((long*)(&(Buffer[1])))=i;
rt_make_hard_real_time();
t_start=rt_get_time();
a=sendto_rt(udpSocket, Buffer, BufferLen,flags,
(sockaddr*)&slaveAdrs,(socklen_t) sizeof(sockaddr));
// ricevo il segmento e lo reinvio al mittente
//memset(&response_slave, 0, sizeof( response_slave ) ); //lpx:
azzero variabile */
for (i_slave=0; i_slave<SLAVE;)
{
BufferLenRx=recvfrom_rt(udpSocket, BufferRx, sizeof(BufferRx),flags,
(sockaddr*)&slaveRx,&lenRx );
if (BufferLenRx==ETIMEDOUT)
{//Time Out
rt_make_soft_real_time();
rt_task_delete(task_rt);
close_rt(udpSocket);
printf("TimeOut %d\n",BufferLenRx);
exit(1);
}
//il confronto è tra due unsigned long
for(j_slave=0; j_slave<SLAVE; j_slave++)
{// controllo chi mi ha risposto
if (slaveRx.sin_addr.s_addr==slaveIP[j_slave])
{
if (i==(*((long*)(&(BufferRx[1])))))
{
i_slave++;
continue;
}
else
{
//errore uscita
rt_make_soft_real_time();
rt_task_delete(task_rt);
close_rt(udpSocket);
printf("dati errati\n");
exit(1);
}
}
}
}
t_stop=rt_get_time();
rt_make_soft_real_time();
t_sum+=((long)count2nano(t_stop-t_start)/1000);
i++;
}
// per chiudere gli slave
rt_make_hard_real_time();
Buffer[0]=1;
sendto_rt(udpSocket, Buffer, BufferLen,flags,
(sockaddr*)&slaveAdrs,(socklen_t) sizeof(sockaddr));
rt_make_soft_real_time();
// Passo nella modalità non real time
//rt_make_soft_real_time();
// chiudo il socket
close_rt(udpSocket);
// rimuovo il task
rt_task_delete(task_rt);
if (a<0) printf("errore %s!\n",strerror(a));
printf("flusso eseguito\n");
printf("tempo medio=%d\n",(t_sum/Npkt)*1000);
return 0;// EXIT_SUCCESS;
}
-------------------------------------------------------
This SF.Net email is sponsored by the JBoss Inc. Get Certified Today
Register for a JBoss Training Course. Free Certification Exam
for All Training Attendees Through End of 2005. For more info visit:
http://ads.osdn.com/?ad_idv28&alloc_id845&op=click
_______________________________________________
RTnet-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/rtnet-users