Page 1 of 1

MOD54415:Serial reading problem

Posted: Wed Jun 05, 2013 12:40 pm
by nicobari
Hi,
I have written a code to read data from serial port. The data being sent is 12 bytes long with first 6 bytes as header. The code works and displays the correct data (shown below)

Code: Select all

///included
#include "predef.h"
#include <stdio.h>
#include <stdlib.h>
#include <basictypes.h>
#include <ucos.h>
#include <ctype.h>
#include <startnet.h>
#include <autoupdate.h>
#include <dhcpclient.h>
#include <taskmon.h>
#include <smarttrap.h>
#include <effs_fat/fat.h>
#include <effs_fat/multi_drive_mmc_mcf.h>
#include <effs_fat/effs_utils.h>
#include <sim.h>
#include <pins.h>
#include <ucosmcfc.h>
#include <pinconstant.h>
#include <HiResTimer.h>
#include <utils.h>
#include <constants.h>
#include <cfinter.h>
#include <math.h>
#include <serial.h>

#define header1 0xAAAA
#define header2 0xABAB
#define header3 0xBBBB



//Global Variables


extern "C"
{
void UserMain( void * pd);
void SetIntc( int intc, long func, int source, int level);

}


void initPINS()
{
	J2[15].function(0);
	J2[15]=0;

	J2[39].function(2);//RX 8
	J2[42].function(2);//TX 8
}

void UserMain( void* pd )
{


	/////Usual Routine
	InitializeStack();
	OSChangePrio( MAIN_PRIO );
    EnableAutoUpdate();
	EnableTaskMonitor();
	EnableSmartTraps();

	//Initializing
	initPINS();
	char input_buff[6];
	char hdr1[2];
	char nxt_hdr[4];
	uint16_t header_buff[3]={0,0,0};
	int16_t data[3];
	int fdSerial;
	int fdSerial2;
	SerialClose(0);
	SerialClose(8);
	fdSerial2=OpenSerial( 0, 115200, 1, 8, eParityNone );
	fdSerial = OpenSerial( 8, 115200, 1, 8, eParityNone );


	while(1)
	{
		OSTimeDly(1);

		J2[15]=J2[15]^J2[15];


		read(fdSerial,(char*)hdr1,2);
		header_buff[0]=(uint16_t)((uint8_t)hdr1[0])*256+(uint8_t)hdr1[1];
		if(header_buff[0]==0xAAAA)
		{

			read(fdSerial,(char*)nxt_hdr,4);
			header_buff[1]=(uint16_t)((uint8_t)nxt_hdr[0])*256+(uint8_t)nxt_hdr[1];
			header_buff[2]=(uint16_t)((uint8_t)nxt_hdr[2])*256+(uint8_t)nxt_hdr[3];
			if(header_buff[1]==0xABAB && header_buff[2]==0xBBBB)
			{

				read(fdSerial,(char*)input_buff,6);
				data[0]=(int16_t)((uint8_t)input_buff[0])*256+(uint8_t)input_buff[1];
				data[1]=(int16_t)((uint8_t)input_buff[2])*256+(uint8_t)input_buff[3];
				data[2]=(int16_t)((uint8_t)input_buff[4])*256+(uint8_t)input_buff[5];
				printf("%d %d %d\n",data[0],data[1],data[2]);


			}
			}
		}
}

The issue that I can't understand are
1)Their is a huge delay between the time when data is being transmitted and the time when data is displayed on MTTTY screen (more than 10 seconds)
2)The code doesn't work if I remove "OSTimeDly(1)" in the while loop.

I am assuming that serial port 0 and serial port 8 are interfering with each other. Any comments and suggestions are welcome.

Thanks in advance,
TM

Re: MOD54415:Serial reading problem

Posted: Wed Jun 05, 2013 1:54 pm
by Forrest
I did not go over the code line by line, but one thing that immediately stood out... Calling read (fd,*buf,bytes) does not mean that you will get n bytes of data. In fact, calling it once often means you only get 1 byte of data. You need to check the return value of read() and keep calling it until you get however many bytes you expect to receive.

Re: MOD54415:Serial reading problem

Posted: Wed Jun 05, 2013 4:11 pm
by dciliske
Really what you should be doing is using select() to pend on the serial port and wait for data to be available. You should then do a read loop similar to this:

Code: Select all

while (bytesRead < messageLength) {
        bytesRead += ReadWithTimeout(
                        fd_serial, data + bytesRead, messageLength - bytesRead, 0 );
    }
where data is a pointer to your receive buffer and messageLength is how long the message is you need to read.

-Dan

Re: MOD54415:Serial reading problem

Posted: Sun Jun 09, 2013 8:45 am
by nicobari
Hi,
Thanks now everything is working as should be!!! on a different note can I call f_write, f_putc in a new task which I created? because I am trying to write to sd card in the new task that I created but I am assuming you cannot call these functions in a separate task.

Thanks again,
TM

Re: MOD54415:Serial reading problem

Posted: Mon Jun 10, 2013 9:33 am
by dciliske
You can use those in a seperate task, however you will need to call

Code: Select all

    f_enterFS();
   f_chdir("/");
in each task priority. Take a look at the EFFS-MultiTask or EFFS-HTTP for the various ways to do this.

-Dan