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
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