199 lines
		
	
	
	
		
			7.4 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			199 lines
		
	
	
	
		
			7.4 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 | 
						|
<html>
 | 
						|
<head>
 | 
						|
  <title>Arduino - Print </title>
 | 
						|
  <link rel='stylesheet' href='arduino.css' type='text/css' />
 | 
						|
</head>
 | 
						|
<body>
 | 
						|
<div id="page">
 | 
						|
<!--PageHeaderFmt-->
 | 
						|
<div id="pageheader">
 | 
						|
  <div class="title"><a href="http://www.arduino.cc"/>Arduino</a></div>
 | 
						|
  <div class="search">
 | 
						|
    <!-- SiteSearch Google -->
 | 
						|
    <FORM method=GET action="http://www.google.com/search">
 | 
						|
    <input type=hidden name=ie value=UTF-8>
 | 
						|
    <input type=hidden name=oe value=UTF-8>
 | 
						|
    <INPUT TYPE=text name=q size=25 maxlength=255 value="">
 | 
						|
    <INPUT type=submit name=btnG VALUE="search">
 | 
						|
    <input type=hidden name=domains value="http://www.arduino.cc/">
 | 
						|
    <input type=hidden name=sitesearch value="http://www.arduino.cc/">
 | 
						|
    </FORM>
 | 
						|
    <!-- SiteSearch Google -->
 | 
						|
  </div>
 | 
						|
</div>
 | 
						|
<!--/PageHeaderFmt-->
 | 
						|
<!--PageLeftFmt-->
 | 
						|
<div id="pagenav" style="text-align: right">
 | 
						|
  <div style="float: left;">
 | 
						|
  <p><a class='wikilink' href='http://arduino.cc/en/Main/Buy'>Buy</a>
 | 
						|
|
 | 
						|
<a class='wikilink' href='http://arduino.cc/en/Main/Software'>Download</a>
 | 
						|
|
 | 
						|
<a class='wikilink' href='Guide_index.html'>Getting Started</a>
 | 
						|
|
 | 
						|
<a class='wikilink' href='http://arduino.cc/en/Tutorial/HomePage'>Learning</a>
 | 
						|
|
 | 
						|
<a class='wikilink' href='index.html'>Reference</a>
 | 
						|
|
 | 
						|
<a class='wikilink' href='http://arduino.cc/en/Main/Hardware'>Hardware</a>
 | 
						|
|
 | 
						|
<a class='wikilink' href='FAQ.html'>FAQ</a>
 | 
						|
</p>
 | 
						|
<p class='vspace'></p>
 | 
						|
 | 
						|
  </div>
 | 
						|
  <a class="urllink" href="http://www.arduino.cc/blog/" rel="nofollow">Blog »</a> |
 | 
						|
  <a class="urllink" href="http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl" rel="nofollow">Forum »</a> |
 | 
						|
  <a class="urllink" href="http://www.arduino.cc/playground/" rel="nofollow">Playground »</a>
 | 
						|
</div>
 | 
						|
<!--/PageLeftFmt-->
 | 
						|
<div id="pagetext">
 | 
						|
<!--PageText-->
 | 
						|
<div id='wikitext'>
 | 
						|
<h2>Serial.print(data)</h2>
 | 
						|
<h4>Description</h4>
 | 
						|
<p>Prints data to the serial port. 
 | 
						|
</p>
 | 
						|
<p class='vspace'></p><h4>Parameter</h4>
 | 
						|
<p>data: integer types, including char, floats<br />Printing of floats is supported with a precision of two places to the right of the decimal point.<br />This may change soon.
 | 
						|
</p>
 | 
						|
<p class='vspace'></p><h4>Syntax</h4>
 | 
						|
<p>This command can take many forms:
 | 
						|
</p>
 | 
						|
<p class='vspace'></p><p><strong>Serial.print(b)</strong> 
 | 
						|
with no format specified, prints b as a decimal number in an ASCII string.  For example, 
 | 
						|
</p>
 | 
						|
<p class='vspace'></p><pre>
 | 
						|
int b = 79;
 | 
						|
Serial.print(b);
 | 
						|
</pre><p>prints the ASCII string "79".
 | 
						|
</p>
 | 
						|
<p class='vspace'></p><p><strong>Serial.print(b, DEC)</strong> 
 | 
						|
prints b as a decimal number in an ASCII string.  For example, 
 | 
						|
</p>
 | 
						|
<p class='vspace'></p><pre>
 | 
						|
int b = 79;
 | 
						|
Serial.print(b, DEC);
 | 
						|
</pre><p>prints the string "79".
 | 
						|
</p>
 | 
						|
<p class='vspace'></p><p><strong>Serial.print(b, HEX)</strong> 
 | 
						|
prints b as a hexadecimal number in an ASCII string.  For example, 
 | 
						|
</p>
 | 
						|
<p class='vspace'></p><pre>
 | 
						|
int b = 79;
 | 
						|
Serial.print(b, HEX);
 | 
						|
</pre><p>prints the string "4F".
 | 
						|
</p>
 | 
						|
<p class='vspace'></p><p><strong>Serial.print(b, OCT)</strong> 
 | 
						|
prints b as an octal number in an ASCII string.  For example, 
 | 
						|
</p>
 | 
						|
<p class='vspace'></p><pre>
 | 
						|
int b = 79;
 | 
						|
Serial.print(b, OCT);
 | 
						|
</pre><p>prints the string "117".
 | 
						|
</p>
 | 
						|
<p class='vspace'></p><p><strong>Serial.print(b, BIN)</strong> 
 | 
						|
prints b as a binary number in an ASCII string.  For example, 
 | 
						|
</p>
 | 
						|
<p class='vspace'></p><pre>
 | 
						|
int b = 79;
 | 
						|
Serial.print(b, BIN);
 | 
						|
</pre><p>prints the string "1001111".
 | 
						|
</p>
 | 
						|
<p class='vspace'></p><p><strong>Serial.print(b, BYTE)</strong> 
 | 
						|
prints b as a single byte.  For example, 
 | 
						|
</p>
 | 
						|
<p class='vspace'></p><pre>
 | 
						|
int b = 79;
 | 
						|
Serial.print(b, BYTE);
 | 
						|
</pre><p>returns the string "O", which is the ASCII character represented by the value 79.  For more information see the <a class='urllink' href='http://www.asciitable.com' rel='nofollow'>ASCII table</a>.
 | 
						|
</p>
 | 
						|
<p class='vspace'></p><p><strong>Serial.print(str)</strong> 
 | 
						|
if str is a string or an array of chars, prints str as an ASCII string.  For example, 
 | 
						|
</p>
 | 
						|
<p class='vspace'></p><pre>
 | 
						|
Serial.print("Hello World!");
 | 
						|
</pre><p>prints the string "Hello World!".
 | 
						|
</p>
 | 
						|
<p class='vspace'></p><h4>Parameters</h4>
 | 
						|
<p>b: the byte to print, or
 | 
						|
</p>
 | 
						|
<p class='vspace'></p><p>str: the string to print
 | 
						|
</p>
 | 
						|
<p class='vspace'></p><h4>Returns</h4>
 | 
						|
<p>None
 | 
						|
</p>
 | 
						|
<p class='vspace'></p><h4>Example:</h4>
 | 
						|
<pre>/*
 | 
						|
Uses a FOR loop for data and prints a number in various formats.
 | 
						|
*/
 | 
						|
int x = 0;    // variable
 | 
						|
 | 
						|
void setup() {
 | 
						|
  Serial.begin(9600);      // open the serial port at 9600 bps:    
 | 
						|
}
 | 
						|
 | 
						|
void loop() {  
 | 
						|
  // print labels 
 | 
						|
  Serial.print("NO FORMAT");       // prints a label
 | 
						|
  Serial.print("\t");              // prints a tab
 | 
						|
 | 
						|
  Serial.print("DEC");  
 | 
						|
  Serial.print("\t");      
 | 
						|
 | 
						|
  Serial.print("HEX"); 
 | 
						|
  Serial.print("\t");   
 | 
						|
 | 
						|
  Serial.print("OCT");
 | 
						|
  Serial.print("\t");
 | 
						|
 | 
						|
  Serial.print("BIN");
 | 
						|
  Serial.print("\t"); 
 | 
						|
 | 
						|
  Serial.println("BYTE");
 | 
						|
 | 
						|
  for(x=0; x< 64; x++){    // only part of the ASCII chart, change to suit
 | 
						|
 | 
						|
    // print it out in many formats:
 | 
						|
    Serial.print(x);       // print as an ASCII-encoded decimal - same as "DEC"
 | 
						|
    Serial.print("\t");    // prints a tab
 | 
						|
 | 
						|
    Serial.print(x, DEC);  // print as an ASCII-encoded decimal
 | 
						|
    Serial.print("\t");    // prints a tab
 | 
						|
 | 
						|
    Serial.print(x, HEX);  // print as an ASCII-encoded hexadecimal
 | 
						|
    Serial.print("\t");    // prints a tab
 | 
						|
 | 
						|
    Serial.print(x, OCT);  // print as an ASCII-encoded octal
 | 
						|
    Serial.print("\t");    // prints a tab
 | 
						|
 | 
						|
    Serial.print(x, BIN);  // print as an ASCII-encoded binary
 | 
						|
    Serial.print("\t");    // prints a tab
 | 
						|
 | 
						|
    Serial.println(x, BYTE);    // prints as a raw byte value, 
 | 
						|
    //                             then adds the carriage return with "println"
 | 
						|
    delay(200);            // delay 200 milliseconds
 | 
						|
  }
 | 
						|
  Serial.println("");      // prints another carriage return
 | 
						|
}
 | 
						|
</pre>
 | 
						|
<p class='vspace'></p><h4>Programming Tips / Known Issues</h4>
 | 
						|
<p>Serial.print() doesn't work on floats, so you'll need to cast them to an integral type, losing any fractional values. It is sometimes useful to multiply your float by a power of ten, to preserve some of this fractional resolution.
 | 
						|
</p>
 | 
						|
<p class='vspace'></p><p>Be careful about doing math inside the brackets e.g.<br /><code>Serial.print(x-2, DEC);</code><br />The unsigned char data type, and byte data type will yield incorrect results and act as though they are signed types i.e. type char.
 | 
						|
</p>
 | 
						|
<p class='vspace'></p><p>The Serial.print function puts data into a buffer. It will wait for one character to send, before going on to the next character. However the function returns before sending the <em>last</em> character.
 | 
						|
</p>
 | 
						|
<p class='vspace'></p><h4>See also</h4>
 | 
						|
<ul><li><a class='wikilink' href='ASCIIchart.html'>ASCII chart</a>
 | 
						|
</li><li><a class='wikilink' href='Serial_Begin.html'>Serial.begin</a>(speed)
 | 
						|
</li><li><a class='wikilink' href='Serial_Available.html'>Serial.available</a>()
 | 
						|
</li><li><a class='wikilink' href='Serial_Read.html'>Serial.read</a>()
 | 
						|
</li><li><a class='wikilink' href='Serial_Println.html'>Serial.println</a>(data)
 | 
						|
</li></ul><p class='vspace'></p><p><a class='wikilink' href='index.html'>Reference Home</a>
 | 
						|
</p>
 | 
						|
</div>
 | 
						|
 | 
						|
</div>
 | 
						|
<!--PageFooterFmt-->
 | 
						|
<div id="pagefooter">
 | 
						|
  <a href='Serial_Print?action=edit.html'>Edit Page</a> | <a href='Serial_Print?action=diff.html'>Page History</a> | <a href='Serial_Print?action=print.html' target='_blank'>Printable View</a> | <a href='http://arduino.cc/en/Site/AllRecentChanges'>All Recent Site Changes</a>
 | 
						|
</div>
 | 
						|
<!--/PageFooterFmt-->
 | 
						|
</div>
 | 
						|
</body>
 | 
						|
</html>
 |