Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hallo everyone, I have a function to read the data from the uart, which is :

C++
void serial_read_appConfig(uint8_t *buffer, uint8_t noOfBytes)
{
   uint8_t i = 0;
   uint8_t temp = 0, rxDec;
   while (i != noOfBytes*2)
   {
      rxDec = asciiToDecConverter(sio2host_getchar());
      if (i%2 == 0) temp = rxDec << 4;
      else if (i%2 != 0) buffer[i/2] = temp + rxDec;
      if (rxDec != 255) i++; // increment count only when receiving a valid hex value from 0-F
   }
}

uint8_t asciiToDecConverter(uint8_t input)
{
   uint8_t decVal = -1;
   if (input >= 48 && input <= 57)  decVal = input - 48; // decimal values between 1 and 9
   else if (input >= 65 && input <= 70) decVal = input - 55; // hex values between A and F
   else if (input >= 97 && input <= 102) decVal = input - 87;  // hex values between a and f
   return decVal;
}


What I have tried:

How can i similarly write a function to write a buffer to the uart

I am not able to understand the while loop return in the program,

Also the function getchar has a return value.
But the function to read putchar has no return value.

C++
void sio2host_putchar(uint8_t ch)
{
   sio2host_tx(&ch, 1);
}

uint8_t sio2host_getchar(void)
{
   uint8_t c;
   while (0 == sio2host_rx(&c, 1)) {
   }
   return c;
}
I have a function to convert the value from Dectoasciiconverter(char ch);
But i am not able to figure out , how to wite a function such as serial_write_app_config().

if anyone has an idea, kindly provide suggestions on how to write the function.
It would be great helpful

Cheers!
Posted
Updated 29-Oct-19 10:13am
v5

getchar has a return value because it returns the next character received - it uses a while loop to wait until the UART has fully recieved a character, then fetches it from the hardware, and returns the value to the caller. If no such character is ever received, it will never exit.

putchar does not return anything because it doesn't have anything to return - it does the exact opposite of getchar and loads a character into the UART for transmission via the serial port*: there is nothing to return!

* In fact, it does it badly, and simplistic code like that way well cause problems because serial data is slow - 9600 baud for example equates to about 1000 characters per second, and the UART will have a very small internal buffer - normally about 8 characters or less. Since the code doesn't check if the transmitter buffer has space there is a very good chance that you will overwrite existing data, and lose characters when you try to transmit anything more than a few bytes.
 
Share this answer
 
Comments
Member 14499788 25-Oct-19 10:52am    
i have only 8 bytes to be transferred
Member 14499788 25-Oct-19 10:53am    
Thanks for the suggestion, but how can i build a similar function to write data
OriginalGriff 25-Oct-19 11:02am    
I can't tell you - you will have to read your hardware manual / specification / circuit diagrams and work it out. There is no "one way" to do it, or even "one UART" to do it with!
C++
if (input >= 48 && input <= 57)  decVal = input - 48; // decimal values between 1 and 9

Why use decimal values here which do not really mean anything? Use proper character values so people can understand what the code is doing:
C++
if (input >= '0' && input <= '9')  decVal = input - '0'; // decimal values between 0 and 9
 
Share this answer
 
finally got the answer .
I am posting the answer here:

Thankyou all for the suggestions

void serial_write_appConfig(uint8_t*buffer, uint8_t noOfBytes)
{
	uint8_t i = 0;
	uint8_t temp = 0, txDec, chara;
	while (i < noOfBytes)
	
	{
		temp = buffer[i] >> 4;
		sio2host_putchar(Dectoasciiconverter(temp));
		temp = buffer[i] & 0x0F;
		sio2host_putchar(Dectoasciiconverter(temp));
		i++;
	}
	
	
}

unsigned char Dectoasciiconverter(unsigned char ch) {
    unsigned char value;
	ch &= 0x0F;
	
	if (ch >= 0 && ch <= 9)
		value = ch + '0';
	else //if (ch >= 10 && ch <= 15)
		value = ch + 'A' - 10;// || ch + 'a';
		
    return value;
}
 
Share this answer
 
v2
Comments
[no name] 29-Oct-19 12:21pm    
I don't think it is a good idea to a have return 0; in a method with the signature void serial_write_appConfig ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900