EasyManuals Logo

Arduino uno User Manual

Arduino uno
311 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #271 background imageLoading...
Page #271 background image
Use
serialport_read_until
to read data from a serial port. Pass it a file descriptor
and a buffer to be filled with the data read. The method also expects a
delimiter character, the maximum length of the buffer, and a timeout
value measured in milliseconds.
serial_port_read_until
stops reading when it
finds the delimiter character, when the buffer is full, or when it times out.
If it cannot read any more data before one of these conditions is met, it
returns -1. Otherwise, it returns 0.
To make sure that all data youve written gets actually transferred, call
serialport_flush
and pass it the file descriptor of your serial port connection.
Heres how to use the code for communicating with our analog reader sketch
(note that the following code will run on your PC and not on your Arduino):
SerialProgramming/C/analog_reader.c
#include <stdio.h>
Line 1
#include <unistd.h>
-
#include <termios.h>
-
#include "arduino-serial-lib.h"
-
#define MAX_LINE 256
5
-
int main(int argc, char* argv[]) {
-
int timeout = 1000;
-
-
if (argc == 1) {
10
printf("You have to pass the name of a serial port.\n");
-
return -1;
-
}
-
int baudrate = B9600;
-
int arduino = serialport_init(argv[1], baudrate);
15
if (arduino == -1) {
-
printf("Could not open serial port %s.\n", argv[1]);
-
return -1;
-
}
-
sleep(2);
20
char line[MAX_LINE];
-
while (1) {
-
int rc = serialport_write(arduino, "a0\n");
-
if (rc == -1) {
-
printf("Could not write to serial port.\n");
25
} else {
-
serialport_read_until(arduino, line, '\n', MAX_LINE, timeout);
-
printf("%s", line);
-
}
-
}
30
serialport_close(arduino);
-
return 0;
-
}
-
Appendix 3. Advanced Serial Programming 258
report erratum discuss
www.it-ebooks.info

Table of Contents

Other manuals for Arduino uno

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the Arduino uno and is the answer not in the manual?

Arduino uno Specifications

General IconGeneral
Form factorArduino
CertificationRoHS, FC, CE
Processor model-
Processor frequency- MHz
Microcontroller modelATmega328
Microcontroller frequency16 MHz
DC input voltage7-12 V
Operating voltage5 V
DC current per I/O pin40 mA
Flash memory0.032 MB
Maximum internal memory- GB
SRAM (Static Random Access Memory)2 KB
EEPROM (Electrically Erasable Programmable Read-Only Memory)1 KB
Wi-FiNo
Number of analog I/O pins6
Number of digital I/O pins14
Weight and Dimensions IconWeight and Dimensions
Board dimensions53.4 x 68.6 mm

Related product manuals