I'm writing a little program to exercise the modbus interface on our embedded
system (modbus is a serial protocol).
I have an infinite loop, which sends a request from the PC to the embedded
device, and then receives a response. So far so good.
But, it will only do it 127 times.
The library I'm using (libmodbus) has a verbose mode, where it will print out
all the information coming & going, as well as the pid's . Each write & read
of data generates a new pid.
While it's working, it looks like this:
pid reading 25443 return -1
pid timeout 25442 return 0
When it stops, it looks like this:
pid reading 0 return 0
pid timeout 25444 return -1
The code from the library looks like this:
int Mbm_send_and_get_result(byte trame[], int timeout, int long_emit, int
longueur)
{
int i,stat1=-1,stat2=-1;
pthread_t thread1,thread2;
Mbm_result = (unsigned char *) malloc(longueur*sizeof(unsigned char));
/* clean port */
tcflush(Mb_device, TCIFLUSH);
/* create 2 threads for read data and to wait end of timeout*/
pthread_create(&thread2, NULL,(void*)&Mbm_sleep,&timeout);
pthread_create(&thread1, NULL,(void*)&Mbm_get_data,&longueur);
if (Mb_verbose)
fprintf(stderr,"start writing \n");
for(i=0;i<long_emit;i++)
{
/* send data */
write(Mb_device,&trame[i],1);
/* call pointer function if exist */
if(Mb_ptr_snd_data!=NULL)
(*Mb_ptr_snd_data)(trame[i]);
}
if (Mb_verbose)
fprintf(stderr,"write ok\n");
do {
if (Mbm_Pid_Child!=0)
/* kill return 0 if the pid is running or -1 if the pid don't exist
*/
stat1=kill(Mbm_Pid_Child,0);
else
stat1=0;
if (Mbm_Pid_Sleep!=0)
stat2=kill(Mbm_Pid_Sleep,0);
else
stat2=0;
/* answer of the slave terminate or and of the timeout */
if (stat1==-1 || stat2==-1)
break;
} while(1);
if (Mb_verbose)
{
fprintf(stderr,"pid reading %d return %d\n",Mbm_Pid_Child,stat1);
fprintf(stderr,"pid timeout %d return %d\n",Mbm_Pid_Sleep,stat2);
}
/* stop both childs */
Mbm_Pid_Child=0;
Mbm_Pid_Sleep=0;
pthread_cancel(thread1);
pthread_cancel(thread2);
/* error : timeout fealure */
if (stat1==0)
{
return 0;
}
/* ok : store the answer packet in the data trame */
for (i=0;i<=longueur;i++)
trame[i]=Mbm_result[i];
return 1;
Any help or pointing in the right direction would be greatly appreciated.
Amanda
--
SLUG - Sydney Linux User's Group - http://slug.org.au/
More Info: http://lists.slug.org.au/listinfo/slug