At recently, some user reported that arduino serial print function always appears this or that problem on pcDuino, and some parameters are not the same as with regular use, it is quite inconvenient. For example, to print a string with parameters, with arduino existing library to achieve as the following:
Serail.print("xxxxxx"); Serail.print(x,HEX);
Obviously it’s too much trouble, if many parameters that have to be divided into many segments. There is a function we should be very familiar with, and that is printf (fmt, …) if using printf function to achieve the above function code is as follows:
printf(“xxxxx%x”,x);
It is not very simple, then how to use printf function to send data by serial port on pcduino? This requires modifying the library on the arduino serial port.
Modified as follows:
1. The Arduino library file path on pcDuino is: /usr/share/arduino/hardware/arduino/pcduino/core/arduion
Open header file of serial library “Serail.h”
2. Added “#include<stdarg.h>” to the header file.
3. Add “void Printf(const char*fmt,…);” to public function in Hwserial class.
4. Open file “Serail.cpp”
5. Add the code to the file:
void Hwserial::Printf(const char *fmt,...) { int count; char *buffer; var_list Arglist; count = 0; buffer = (char*)malloc(128); if(buffer == NULL)return; va_start(Arglist,fmt); vsprintf(buffer,fmt,Arglist); while(buffer[count] != '\0')this->write(buffer[count++]); free(buffer); }
Use this function to achieve the above function code is as follows:
Serail.Printf(“xxxxx%x”,x);
Leave a Reply
You must be logged in to post a comment.