From: VaibhavGhadiok <vai...@gm...> - 2010-05-28 07:44:34
|
Dear all I am sending a 4 byte struct (typecasting to char *) over serial and recovering this on the other side by typecasting the received data back to the same kind of struct. Host Machine (x86) (Sender) struct js_send { short value; unsigned char type; unsigned char number; }; struct js_send *jsptr; struct js_send jsesend; char *datatosend = malloc(sizeof(jsesend)); // Have some code to write values to jsesend datatosend = (char *) &jsesend; jsptr = (struct js_send *) datatosend; // This and the next line works so I am able to recover the data after all the typecasting printf(" %hd %hu %hu \n", jsptr->value, jsptr->type, jsptr->number); write(fd, datatosend, 4); // Write 4 bytes to to Serial Port The first problem I face is that the program on the receiving side tends to read the data twice. This happens even while using sertest. Using my own program derived from sertest, it reads 10-20 times. I would ideally like to poll for serial data at the start of a loop, don't want it interrupt driven. I would like to have some way to check if new data has arrived. If true, only then read data.(Something like kbhit()) Would I need to look into GPIO Event Driver Code? Code on the Arm processor (Receiver): ( I have ommited some code for the sake of brevity) fd_set readfs; char devName[ 40 ] = "/dev/ttyS2"; unsigned int baudRate = 115200; //Serial Port Settings: attr.c_iflag = 0; attr.c_oflag = 0; attr.c_cflag = CLOCAL | CREAD | CS8; attr.c_lflag = 0; attr.c_cc[ VTIME ] = 0; // timeout in tenths of a second attr.c_cc[ VMIN ] = 0; // Only wait for a single char cfsetispeed( &attr, baudRate ); cfsetospeed( &attr, baudRate ); char datarec[4]; struct js_ev { short value; unsigned char type; unsigned char number; }; //FD_SET(gPortFd, &readfs); //* //select(gPortFd, &readfs, NULL, NULL, 0); //* //if(FD_ISSET(gPortFd, &readfs)) //* { if (( bytesRead = read( gPortFd, datarec, 4 )) < 0 ) { fprintf( stderr, "Serial port read failed: %s\n", strerror( errno )); exit( 1 ); } jserec = (struct js_ev *) datarec; //printf("%s\n", datarec); printf("%hu %hu %hd\n", jserec->type, jserec->number, jserec->value); fflush(stdout); } Note: This program gives me some output. With the lines marked with * uncommented, I don't see any output at all. I was trying to have some kind of mechanism to tell me if there was some kind of data to be read. The second problem I face is that, the data is not displayed correctly. The data is not even consistent. The ARM on the gumstix is configured as Little Endian (I suppose) and so is x86. The size of the structs is same - 4 bytes on both the platforms. And so is the ordering of the elements inside. The Zigbee modules I use send 4 bytes at a time. I have checked these independantly and they work just fine. Its something with the way I read, I suppose. Thanks and with regards Vaibhav -- View this message in context: http://old.nabble.com/Serial-Data-Read-problem-tp28703747p28703747.html Sent from the Gumstix mailing list archive at Nabble.com. |