openFrameworksArduino
ofSerialLinux.h
1 #pragma once
2 
3 
4 #include "ofConstants.h"
5 #include "ofTypes.h"
6 
7 #if defined( TARGET_OSX ) || defined( TARGET_LINUX )
8 
9 #include <termios.h>
10 #include <stdint.h>
11 
12 // notes below
13 
14 //----------------------------------------------------------------------
15 class ARDUINO_PORT ofSerial {
16 
17  public:
18  ofSerial();
19  virtual ~ofSerial();
20 
21  void listDevices();
22 
23  //old method - deprecated
24  void enumerateDevices();
25 
26  std::vector <ofSerialDeviceInfo> getDeviceList();
27 
28  void close();
29  bool setup(); // use default port, baud (0,9600)
30  bool setup(std::string portName, int baudrate);
31  bool setup(int deviceNumber, int baudrate);
32 
33 
34  int readBytes(unsigned char * buffer, int length);
35  int writeBytes(unsigned char * buffer, int length);
36  bool writeByte(unsigned char singleByte);
37  int readByte(); // returns -1 on no read or error...
38  void flush(bool flushIn = true, bool flushOut = true);
39  int available();
40 
41  void drain();
42  bool isInitialized() const;
43 
44  bool setBaud(int baud);
45  bool setBaud(const std::string& baud_str);
46 
47  protected:
48  void buildDeviceList();
49 
50  std::string deviceType;
51  std::vector <ofSerialDeviceInfo> devices;
52 
53  bool bHaveEnumeratedDevices;
54 
55  int port_is_open;
56  int baud_rate;
57  std::string error_msg;
58  std::string port_name;
59 
60  int port_fd;
61  struct termios settings_orig;
62  struct termios settings;
63 };
64 
65 //----------------------------------------------------------------------
66 
67 
68 
69 
70 // this serial code contains small portions of the following code-examples:
71 // ---------------------------------------------------
72 // http://todbot.com/arduino/host/arduino-serial/arduino-serial.c
73 // web.mac.com/miked13/iWeb/Arduino/Serial%20Write_files/main.cpp
74 // www.racer.nl/docs/libraries/qlib/qserial.htm
75 // ---------------------------------------------------
76 
77 // notes:
78 // ----------------------------
79 // when calling setup("....") you need to pass in
80 // the name of the com port the device is attached to
81 // for example, on a mac, it might look like:
82 //
83 // setup("/dev/tty.usbserial-3B1", 9600)
84 //
85 // and on a pc, it might look like:
86 //
87 // setup("COM4", 9600)
88 //
89 // if you are using an arduino board, for example,
90 // you should check what ports you device is on in the
91 // arduino program
92 
93 // to do:
94 // ----------------------------
95 // a) support blocking / non-blocking
96 // b) support numChars available type functions
97 // c) can we reduce the number of includes here?
98 
99 // useful :
100 // http://en.wikibooks.org/wiki/Serial_Programming:Unix/termios
101 // http://www.keyspan.com/downloads-files/developer/win/USBSerial/html/DevDocsUSBSerial.html
102 // ----------------------------
103 // (also useful, might be this serial example - worth checking out:
104 // http://web.mit.edu/kvogt/Public/osrc/src/
105 // if has evolved ways of dealing with blocking
106 // and non-blocking instances)
107 // ----------------------------
108 
109 #endif
110 
Definition: ofSerial.h:27