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