File: Serial_Print.html

package info (click to toggle)
arduino 0018%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 5,704 kB
  • ctags: 8,331
  • sloc: java: 38,036; ansic: 5,841; cpp: 2,867; makefile: 667; perl: 111; sh: 10
file content (207 lines) | stat: -rw-r--r-- 7,625 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<!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' />
  <meta name="verify-v1" content="TtxFIEJAB6zdJ509wLxjnapQzKAMNm9u0Wj4ho6wxIY=" />
</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 &raquo;</a> |
  <a class="urllink" href="http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl" rel="nofollow">Forum &raquo;</a> |
  <a class="urllink" href="http://www.arduino.cc/playground/" rel="nofollow">Playground &raquo;</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&lt; 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>