EmbeddedRelated.com
Forums

Serial port communication - MSB is always set

Started by Affan August 11, 2007
Hi all,
I have spent some time on writing a simple code that works with the
serial port on my embedded system (Mica2 from crossbow) that has an
Atmel 128L. After debugging the code upmteeth time and with help from
http://www.easysw.com/~mike/serial/serial.html, i seem to have it
working with the correct baud rate being set.
However, each byte that I receive in my embedded processor seems to
have its MSB set: e.g. if I send 4 as the first byte, the received
byte is 128+4=132, if I send 9 it is 128+9=137. Is there anything I am
missing here? I get no write errors on my PC side, and I have tried it
on different computers, nothing seems to work. Has any body had this
issue before? Thank you.

Here is the code that I use to set it to 8N1 (I dont want to use any
parity).

int setSerial( char *p_szPlatform,
               struct termios *p_pNewtio )
{
     if (tcgetattr(input_stream, p_pNewtio) == -1)
    {

        printf("Error getting tty attributes %s(%d).\n",
strerror(errno), errno);
	return -1;
    }
    //to setup 8N1
  p_pNewtio->c_cflag &= ~PARENB;//make sure parity bit is not set
   p_pNewtio->c_cflag &= ~CSTOPB;
  p_pNewtio->c_cflag &= ~CSIZE;//mask character size bits
  p_pNewtio->c_cflag |= CS8 | CLOCAL | CREAD;

  cfsetispeed(p_pNewtio, BAUDRATE_MICA2);
	cfsetospeed(p_pNewtio, BAUDRATE_MICA2);

    p_pNewtio->c_iflag |= IGNPAR;
    //chooising raw input
    p_pNewtio->c_iflag &=~(ICANON|ECHO|ECHOE|ISIG);

    /* Raw output_file */
    p_pNewtio->c_oflag &=~OPOST;

  return iRet;
}

///The code where I write to the serial port is as follows
  tcflush(input_stream, TCIFLUSH);

	    if (tcsetattr(input_stream, TCSANOW, &newtio) == -1)
	    	{
	    	printf("Error setting tty attributes  %s(%d).
\n",strerror(errno), errno);
	    }
        printf("Input baud rate changed to %d\n", (int)
cfgetispeed(&newtio));
	 printf("Output baud rate changed to %d\n", (int)
cfgetospeed(&newtio));
	//send a packet via the serial port to the mote to test if the serial
is correctly set or not
	typedef struct _app_header
	{
	        uint8_t len;    //the phyHeader

		uint8_t type;//just for test purposes
		int16_t crc;	//the crc reqd by the physical layer
	} __attribute__ ((packed)) AppHeader;

	AppHeader pkt;
	uint8_t* buf= (uint8_t*)&pkt;

	pkt.len = sizeof(pkt);
	pkt.type= 9;//debugging
	pkt.crc=0;

	pkt.crc=update_crc(pkt.len, pkt.crc);
	pkt.crc=update_crc(pkt.type, pkt.crc);

	for(i=0; i<pkt.len; i++){
			if(write(input_stream, &buf[i], 1) != 1)
					perror("write: ");
				printf("Byte Value: %d\n",(uint8_t)buf[i]);
		}

Affan wrote:
> Hi all, > I have spent some time on writing a simple code that works with the > serial port on my embedded system (Mica2 from crossbow) that has an > Atmel 128L. After debugging the code upmteeth time and with help from > http://www.easysw.com/~mike/serial/serial.html, i seem to have it > working with the correct baud rate being set. > However, each byte that I receive in my embedded processor seems to > have its MSB set: e.g. if I send 4 as the first byte, the received > byte is 128+4=132, if I send 9 it is 128+9=137. Is there anything I am > missing here? I get no write errors on my PC side, and I have tried it > on different computers, nothing seems to work. Has any body had this > issue before? Thank you. > > Here is the code that I use to set it to 8N1 (I dont want to use any > parity). > > int setSerial( char *p_szPlatform, > struct termios *p_pNewtio ) > { > if (tcgetattr(input_stream, p_pNewtio) == -1) > { > > printf("Error getting tty attributes %s(%d).\n", > strerror(errno), errno); > return -1; > } > //to setup 8N1 > p_pNewtio->c_cflag &= ~PARENB;//make sure parity bit is not set > p_pNewtio->c_cflag &= ~CSTOPB; > p_pNewtio->c_cflag &= ~CSIZE;//mask character size bits > p_pNewtio->c_cflag |= CS8 | CLOCAL | CREAD; > > cfsetispeed(p_pNewtio, BAUDRATE_MICA2); > cfsetospeed(p_pNewtio, BAUDRATE_MICA2); > > p_pNewtio->c_iflag |= IGNPAR; > //chooising raw input > p_pNewtio->c_iflag &=~(ICANON|ECHO|ECHOE|ISIG); > > /* Raw output_file */ > p_pNewtio->c_oflag &=~OPOST; > > return iRet; > } > > ///The code where I write to the serial port is as follows > tcflush(input_stream, TCIFLUSH); > > if (tcsetattr(input_stream, TCSANOW, &newtio) == -1) > { > printf("Error setting tty attributes %s(%d). > \n",strerror(errno), errno); > } > printf("Input baud rate changed to %d\n", (int) > cfgetispeed(&newtio)); > printf("Output baud rate changed to %d\n", (int) > cfgetospeed(&newtio)); > //send a packet via the serial port to the mote to test if the serial > is correctly set or not > typedef struct _app_header > { > uint8_t len; //the phyHeader > > uint8_t type;//just for test purposes > int16_t crc; //the crc reqd by the physical layer > } __attribute__ ((packed)) AppHeader; > > AppHeader pkt; > uint8_t* buf= (uint8_t*)&pkt; > > pkt.len = sizeof(pkt); > pkt.type= 9;//debugging > pkt.crc=0; > > pkt.crc=update_crc(pkt.len, pkt.crc); > pkt.crc=update_crc(pkt.type, pkt.crc); > > for(i=0; i<pkt.len; i++){ > if(write(input_stream, &buf[i], 1) != 1) > perror("write: "); > printf("Byte Value: %d\n",(uint8_t)buf[i]); > }
Sounds like your a tiny bit off on your clocking. The MSB is sent just before the stop bit. The stop bit is always one, so it sounds like your micro is seeing the stop bit instead of the MSB.
On Aug 11, 5:11 pm, Affan <int...@gmail.com> wrote:
> Hi all, > I have spent some time on writing a simple code that works with the > serial port on my embedded system (Mica2 from crossbow) that has an > Atmel 128L. After debugging the code upmteeth time and with help fromhttp://www.easysw.com/~mike/serial/serial.html, i seem to have it > working with the correct baud rate being set. > However, each byte that I receive in my embedded processor seems to > have its MSB set: e.g. if I send 4 as the first byte, the received > byte is 128+4=132, if I send 9 it is 128+9=137. Is there anything I am > missing here? I get no write errors on my PC side, and I have tried it > on different computers, nothing seems to work. Has any body had this > issue before? Thank you. > > Here is the code that I use to set it to 8N1 (I dont want to use any > parity). > > int setSerial( char *p_szPlatform, > struct termios *p_pNewtio ) > { > if (tcgetattr(input_stream, p_pNewtio) == -1) > { > > printf("Error getting tty attributes %s(%d).\n", > strerror(errno), errno); > return -1; > } > //to setup 8N1 > p_pNewtio->c_cflag &= ~PARENB;//make sure parity bit is not set > p_pNewtio->c_cflag &= ~CSTOPB; > p_pNewtio->c_cflag &= ~CSIZE;//mask character size bits > p_pNewtio->c_cflag |= CS8 | CLOCAL | CREAD; > > cfsetispeed(p_pNewtio, BAUDRATE_MICA2); > cfsetospeed(p_pNewtio, BAUDRATE_MICA2); > > p_pNewtio->c_iflag |= IGNPAR; > //chooising raw input > p_pNewtio->c_iflag &=~(ICANON|ECHO|ECHOE|ISIG); > > /* Raw output_file */ > p_pNewtio->c_oflag &=~OPOST; > > return iRet; > > } > > ///The code where I write to the serial port is as follows > tcflush(input_stream, TCIFLUSH); > > if (tcsetattr(input_stream, TCSANOW, &newtio) == -1) > { > printf("Error setting tty attributes %s(%d). > \n",strerror(errno), errno); > } > printf("Input baud rate changed to %d\n", (int) > cfgetispeed(&newtio)); > printf("Output baud rate changed to %d\n", (int) > cfgetospeed(&newtio)); > //send a packet via the serial port to the mote to test if the serial > is correctly set or not > typedef struct _app_header > { > uint8_t len; //the phyHeader > > uint8_t type;//just for test purposes > int16_t crc; //the crc reqd by the physical layer > } __attribute__ ((packed)) AppHeader; > > AppHeader pkt; > uint8_t* buf= (uint8_t*)&pkt; > > pkt.len = sizeof(pkt); > pkt.type= 9;//debugging > pkt.crc=0; > > pkt.crc=update_crc(pkt.len, pkt.crc); > pkt.crc=update_crc(pkt.type, pkt.crc); > > for(i=0; i<pkt.len; i++){ > if(write(input_stream, &buf[i], 1) != 1) > perror("write: "); > printf("Byte Value: %d\n",(uint8_t)buf[i]); > }
Are you absolutely certain that you have the size set properly? You can get this if you have 7 bits, or if you set "ON" parity. I would look at what actually got into the UCSRB & UCSRC registers after initialization. G.
On Aug 11, 6:36 pm, "Anthony Fremont" <any...@nospam.com> wrote:
> Affan wrote: > > Hi all, > > I have spent some time on writing a simple code that works with the > > serial port on my embedded system (Mica2 from crossbow) that has an > > Atmel 128L. After debugging the code upmteeth time and with help from > >http://www.easysw.com/~mike/serial/serial.html, i seem to have it > > working with the correct baud rate being set. > > However, each byte that I receive in my embedded processor seems to > > have its MSB set: e.g. if I send 4 as the first byte, the received > > byte is 128+4=132, if I send 9 it is 128+9=137. Is there anything I am > > missing here? I get no write errors on my PC side, and I have tried it > > on different computers, nothing seems to work. Has any body had this > > issue before? Thank you. > > > Here is the code that I use to set it to 8N1 (I dont want to use any > > parity). > > > int setSerial( char *p_szPlatform, > > struct termios *p_pNewtio ) > > { > > if (tcgetattr(input_stream, p_pNewtio) == -1) > > { > > > printf("Error getting tty attributes %s(%d).\n", > > strerror(errno), errno); > > return -1; > > } > > //to setup 8N1 > > p_pNewtio->c_cflag &= ~PARENB;//make sure parity bit is not set > > p_pNewtio->c_cflag &= ~CSTOPB; > > p_pNewtio->c_cflag &= ~CSIZE;//mask character size bits > > p_pNewtio->c_cflag |= CS8 | CLOCAL | CREAD; > > > cfsetispeed(p_pNewtio, BAUDRATE_MICA2); > > cfsetospeed(p_pNewtio, BAUDRATE_MICA2); > > > p_pNewtio->c_iflag |= IGNPAR; > > //chooising raw input > > p_pNewtio->c_iflag &=~(ICANON|ECHO|ECHOE|ISIG); > > > /* Raw output_file */ > > p_pNewtio->c_oflag &=~OPOST; > > > return iRet; > > } > > > ///The code where I write to the serial port is as follows > > tcflush(input_stream, TCIFLUSH); > > > if (tcsetattr(input_stream, TCSANOW, &newtio) == -1) > > { > > printf("Error setting tty attributes %s(%d). > > \n",strerror(errno), errno); > > } > > printf("Input baud rate changed to %d\n", (int) > > cfgetispeed(&newtio)); > > printf("Output baud rate changed to %d\n", (int) > > cfgetospeed(&newtio)); > > //send a packet via the serial port to the mote to test if the serial > > is correctly set or not > > typedef struct _app_header > > { > > uint8_t len; //the phyHeader > > > uint8_t type;//just for test purposes > > int16_t crc; //the crc reqd by the physical layer > > } __attribute__ ((packed)) AppHeader; > > > AppHeader pkt; > > uint8_t* buf= (uint8_t*)&pkt; > > > pkt.len = sizeof(pkt); > > pkt.type= 9;//debugging > > pkt.crc=0; > > > pkt.crc=update_crc(pkt.len, pkt.crc); > > pkt.crc=update_crc(pkt.type, pkt.crc); > > > for(i=0; i<pkt.len; i++){ > > if(write(input_stream, &buf[i], 1) != 1) > > perror("write: "); > > printf("Byte Value: %d\n",(uint8_t)buf[i]); > > } > > Sounds like your a tiny bit off on your clocking. The MSB is sent just > before the stop bit. The stop bit is always one, so it sounds like your > micro is seeing the stop bit instead of the MSB.
Thank you.. I think this might be it.. I know the code that I am using changes timing to an external clock with a slightly different clock rate. Let me get to my test bed and I will try to look into the code to make sure that the clocking is corrected.. But thank you for guiding me in the right direction.. I think :) Affan.