View topic - pipe() in qnx 4

pipe() in qnx 4

anything that doesn't fit to other groups.

pipe() in qnx 4

Postby mounterriver » Wed Nov 26, 2003 8:02 am

I am using pipe as a means of IPC
process a: server
process b: read serial port and send data to process a via a pipe

however, when i start the process a which spawnl process b, the data corrupted as long as i use write (pipe_fd, buf, length);
and the series port also get corrupted when i use qtalk -m /dev/ser3 to view the data. that is to say, the data coming from ser3 becomes unstable as long as i use pipe and write (pipe_fd, buf, length).
After i restart the computer, the ser3 becomes normal when using qtalk to probe.


the following are the partial code for process b


/* process b
* ser3 data collection
**************************/


#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/proxy.h>
#include <termios.h>
#include <sys/dev.h>
#include <sys/kernel.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#define RS232_SONAR_SPEED 9600
#define STRING_LENGTH 75

typedef struct _msgstr
{
short int msgno;
short int msghead;
char msgcontent[STRING_LENGTH];
}msgstr;



/* sio initialization */

int sioOpen(char *path, unsigned int baudrate)
{
int RS232_fd;
unsigned SER1RS232_OLDMODE;
struct termios SER1RS232_MODE;

RS232_fd = open (path, O_RDWR);
SER1RS232_OLDMODE = dev_mode ( RS232_fd, 0, _DEV_MODES);

tcgetattr ( RS232_fd, &SER1RS232_MODE);
cfsetispeed( &SER1RS232_MODE, baudrate);
cfsetospeed( &SER1RS232_MODE, baudrate);

tcsetattr(RS232_fd, TCSADRAIN, &SER1RS232_MODE);

tcflush(RS232_fd, TCIOFLUSH);

return RS232_fd;
}

int readPrelimFixSize ( int fd, char *buf, char pelim, int fixsize)
{
int status;

do
{
status = read(fd, buf, 1);
if (status < 0 ) return status;
if (status < 1 ) perror( " error in reading " );
} while ( *buf != pelim );

status = readFixSize (fd, buf+1, fixsize-1);
if ( status == (fixsize -1))
return ( status + 1);
else
{
perror ("error in read ");
return ( status + 1 );
}
}


int readFixSize( int fd, char *buf, int fixsize)
{
char *ptr = buf;
int status=0, n;

if ( fixsize < 0 ) return (-1);

while ( status != fixsize )
{

n=read(fd, ptr+status, fixsize-status);
if (n <0) return n;
if (n == 0) return status;
status +=n;
delay (10);
}
return (status);

}


int main (int argc, char **argv)
{
struct termios RS232_mode;
pid_t RS232_proxy, RS232_fd;
int state, size, i;
static int counter=0;
char sonarBuffer[STRING_LENGTH];
char rs232Buffer[STRING_LENGTH];
short int s[14];
char flag=0x00;
msgstr msg;
msgstr *msg_ptr=&msg;

int pipe_fd = atoi(argv[1]);
printf(" argc =%d, argv = %d \n", argc, pipe_fd);
RS232_proxy = qnx_proxy_attach (0, 0, 0, -1);

RS232_fd = sioOpen( "/dev/ser3", RS232_SONAR_SPEED);
dev_state(RS232_fd, 0, _DEV_EVENT_INPUT);
dev_arm(RS232_fd, RS232_proxy, _DEV_EVENT_INPUT);

while (1)
{
Receive( RS232_proxy, 0, 0);
//delay(1000);
state = dev_state(RS232_fd, 0, _DEV_EVENT_INPUT);
if ( state & _DEV_EVENT_INPUT)
{

size = readPrelimFixSize ( RS232_fd, msg_ptr->msgcontent, '$', (sizeof( rs232Buffer)-2));


printf(" size =%d \n", size);
for (i = 0; i< size; i++)
{
sonarBuffer[counter] = rs232Buffer[i];

if ( rs232Buffer[i] == '$' )
{
sonarBuffer[0]='$';
flag++;
if (flag == 0x02)
{
memmove ( msg_ptr->msgcontent, sonarBuffer, sizeof(sonarBuffer));
printf("writing to pipe_fd\n");
flag = 0x00;
size = write (pipe_fd, msg_ptr, sizeof(msgstr));
if (size != sizeof (msgstr))
perror("write to pipe_fd failed");
}

counter = 0;
}
counter++;

}


}

dev_arm (RS232_fd, RS232_proxy, _DEV_EVENT_INPUT);

}

return (EXIT_SUCCESS);
}
mounterriver
Active Member
 
Posts: 17
Joined: Sun Oct 12, 2003 6:50 pm

Postby pisoi2001 » Fri Nov 28, 2003 3:41 am

You didn't post how you spawn the process b - but if you use - as it seems - spawnl - the pipe_fd is not a valid file descriptor.
You cannot spawn the process b, give it a file descriptor as an argument
and start writing in that file descriptor.
Maybe you should spawn() instead.

It's most likely that pipe_fd has the same value as the file descriptor you get by opening the serial port, and you end up reading and writing on the serial port.

You could use a named fifo file instead, and open it for read in process a and for write in process b. If you are using the pipe() system call, you should use fork() to create the process b or spawn().
pisoi2001
QNX Master
 
Posts: 5
Joined: Fri Nov 14, 2003 4:46 pm

pipe() in qnx 4

Postby mounterriver » Fri Nov 28, 2003 12:12 pm

Thanks to pisoi2001
The problem is as what you said, there is a mistake between the file discriptor of serial port and pipe in the two processes.
mounterriver
Active Member
 
Posts: 17
Joined: Sun Oct 12, 2003 6:50 pm


Return to General Programming

Who is online

Users browsing this forum: No registered users and 2 guests

cron