cancel
Showing results for 
Search instead for 
Did you mean: 

Question regarding RS232 Serial data output with STM32F4

Bogdan
Senior
Posted on August 16, 2014 at 18:52

Hi,

my experience with serial comunication is very poor, after six months of ''pain'' trying to implement some AHRS algorithms, now i want to port that data to matlab for example, but i need every dataset to be in its own separate row when i send it to serial

#define GPIO_RS232_TX GPIO_Pin_6

#define GPIO_RS232_RX GPIO_Pin_7

#define baudrate 9600

void serial_init(){ // serial usart module & pins initialization

GPIO_InitTypeDef GPIO_InitStruct;

USART_InitTypeDef USART_InitStruct;

GPIO_InitStruct.GPIO_Pin = GPIO_RS232_TX | GPIO_RS232_RX ;

GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;

GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;

GPIO_Init(GPIOB, &GPIO_InitStruct);

//Pins modules asigment

GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1); // Serial TX

GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1); // Serial Rx

USART_InitStruct.USART_BaudRate = baudrate; // the baudrate is set to the value we passed into this init function

USART_InitStruct.USART_WordLength = USART_WordLength_8b;// we want the data frame size to be 8 bits (standard)

USART_InitStruct.USART_StopBits = USART_StopBits_1; // we want 1 stop bit (standard)

USART_InitStruct.USART_Parity = USART_Parity_No; // we don't want a parity bit (standard)

USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // we don't want flow control (standard)

USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; // we want to enable the transmitter and the receiver

USART_Init(USART1, &USART_InitStruct); // again all the properties are passed to the USART_Init function which takes care of all the bit setting

// Usart_interrupt_enable()

USART_Cmd(USART1, ENABLE);

}

//// function just to send data

void serial_out(USART_TypeDef* USARTx, volatile char *s){

while(*s){

// wait until data register is empty

while( !(USARTx->SR & 0x00000040) );

USART_SendData(USARTx, *s);

*s++;

} }

for example i have 3 sets of chars

char txt1;

char txt2

char txt3

for each of them i use the serial out function like bellow:

serial_out(USART1, txt1);

serial_out(USART1, txt2);

serial_out(USART1, txt3);

The data is recived by the PC trough a serial-usb converter, and the problem is how to align every character ser in separate row

for example i want every txtX on its own raw

At the moment if i read the data, they are all on the same row... How can i separte them?

My current code using inemo and output via chars and then to serial

float RoLl=xEulerAngles.m_fRoll* 0f / 3.141592f;

float Pici=xEulerAngles.m_fPitch * 0f / 3.141592f;

float YaW=xEulerAngles.m_fYaw * 0f / 3.141592f;

//converts to string

sprintf(txt1,''%f'',RoLl);

sprintf(txt2,''%f'',Pici);

sprintf(txt3,''%f'',YaW);

//sends the strings to rs-232

serial_out(USART1,txt1);

serial_out(USART1,txt2);

serial_out(USART1,txt3);

1 REPLY 1
pulaskite
Associate
Posted on August 16, 2014 at 23:28

For single values per line just add a newline ''\n'' to your format statement.

//converts to string

sprintf(txt1,''%f\n'',RoLl);

sprintf(txt2,''%f\n'',Pici);

sprintf(txt3,''%f\n'',YaW);   For multiple values per line it gets messier. You'll need to buffer your output. Awk is a good tool for rearranging ASCII text files.