reference

This documentation is automatically generated from the openFrameworks source code using doxygen and refers to the most recent release, version 0.12.0.

ofSerial.h
Go to the documentation of this file.
1#pragma once
2
3#include "ofConstants.h"
4
5class ofBuffer;
6
7#if defined( TARGET_OSX ) || defined( TARGET_LINUX ) || defined (TARGET_ANDROID)
8 #include <termios.h>
9#else
10 #include <winbase.h>
11 #include <tchar.h>
12 #include <iostream>
13 #include <string.h>
14 #include <devpropdef.h>
15 #include <setupapi.h>
16 #include <regstr.h>
18 #define MAX_SERIAL_PORTS 256
20 #include <winioctl.h>
21 /*#ifndef _MSC_VER
22 #define INITGUID
23 #include <initguid.h> // needed for dev-c++ & DEFINE_GUID
24 #endif*/
25#endif
26
27// serial error codes
28#define OF_SERIAL_NO_DATA -2
29#define OF_SERIAL_ERROR -1
30
31
32
35 friend class ofSerial;
36
37 public:
42 ofSerialDeviceInfo(std::string devicePathIn, std::string deviceNameIn, int deviceIDIn){
43 devicePath = devicePathIn;
44 deviceName = deviceNameIn;
45 deviceID = deviceIDIn;
46 }
47
50 deviceName = "device undefined";
51 deviceID = -1;
52 }
53
59 std::string getDevicePath(){
60 return devicePath;
61 }
62
68 std::string getDeviceName(){
69 return deviceName;
70 }
71
78 return deviceID;
79 }
80
81 protected:
83
85 std::string devicePath;
86
88 std::string deviceName;
89
91 int deviceID;
92
94};
95
113class ofSerial {
114
115public:
118
122 ofSerial();
123
124 virtual ~ofSerial();
125
129
137 void listDevices();
138
152 OF_DEPRECATED_MSG("Use listDevices() instead", void enumerateDevices());
153
156 std::vector <ofSerialDeviceInfo> getDeviceList();
157
161
169 bool setup();
170
184 bool setup(std::string portName, int baudrate);
185
194 bool setup(int deviceNumber, int baudrate);
195
196 bool isInitialized() const;
197
199 void close();
200
204
217 int available();
218
262 long readBytes(unsigned char * buffer, size_t length);
263 long readBytes(char * buffer, size_t length);
264 long readBytes(ofBuffer & buffer, size_t length);
265
285 int readByte();
286
290
297 long writeBytes(const unsigned char * buffer, size_t length);
298 long writeBytes(const char * buffer, size_t length);
299 long writeBytes(const ofBuffer & buffer);
300
312 bool writeByte(unsigned char singleByte);
313 bool writeByte(char singleByte);
314
318
324 void flush(bool flushIn = true, bool flushOut = true);
325
329 void drain();
330
332
333protected:
340 void buildDeviceList();
341
342 std::string deviceType;
343 std::vector <ofSerialDeviceInfo> devices;
345 bool bInited;
346
347#ifdef TARGET_WIN32
348
355 void enumerateWin32Ports();
356
357 COMMTIMEOUTS oldTimeout;
361 char** portNamesShort;
366 char** portNamesFriendly;
372 HANDLE hComm;
373 int nPorts;
374 bool bPortsEnumerated;
375
376#else
377 int fd;
378 struct termios oldoptions;
379#endif
380
381};
382
383//----------------------------------------------------------------------
384// this serial code contains small portions of the following code-examples:
385// ---------------------------------------------------
386// http://todbot.com/arduino/host/arduino-serial/arduino-serial.c
387// web.mac.com/miked13/iWeb/Arduino/Serial%20Write_files/main.cpp
388// www.racer.nl/docs/libraries/qlib/qserial.htm
389// ---------------------------------------------------
390
391// to do:
392// ----------------------------
393// a) support blocking / non-blocking
394// b) support numChars available type functions
395// c) can we reduce the number of includes here?
396
397// useful :
398// http://en.wikibooks.org/wiki/Serial_Programming:Unix/termios
399// http://www.keyspan.com/downloads-files/developer/win/USBSerial/html/DevDocsUSBSerial.html
400// ----------------------------
401// (also useful, might be this serial example - worth checking out:
402// http://web.mit.edu/kvogt/Public/osrc/src/
403// if has evolved ways of dealing with blocking
404// and non-blocking instances)
405// ----------------------------
Definition ofFileUtils.h:15
Describes a Serial device, including ID, name and path.
Definition ofSerial.h:34
int getDeviceID()
Gets the ID of the device.
Definition ofSerial.h:77
std::string getDevicePath()
Gets the path to the device.
Definition ofSerial.h:59
std::string getDeviceName()
Gets the name of the device.
Definition ofSerial.h:68
ofSerialDeviceInfo()
Construct an undefined serial device.
Definition ofSerial.h:49
ofSerialDeviceInfo(std::string devicePathIn, std::string deviceNameIn, int deviceIDIn)
Construct an ofSerialDeviceInfo with parameters.
Definition ofSerial.h:42
ofSerial provides a cross platform system for interfacing with the serial port. You can choose the po...
Definition ofSerial.h:113
std::vector< ofSerialDeviceInfo > getDeviceList()
Returns a vector of ofSerialDeviceInfo instances with the devicePath, deviceName, deviceID set.
Definition ofSerial.cpp:232
void drain()
Drain is only available on OSX and Linux and is very similar to flush(), but blocks until all the dat...
Definition ofSerial.cpp:649
std::string deviceType
Definition ofSerial.h:342
void buildDeviceList()
Enumerate all devices attached to a serial port.
Definition ofSerial.cpp:154
bool bInited
<
Definition ofSerial.h:345
bool setup(std::string portName, int baudrate)
Opens the serial port, with the given name and baud rate.
struct termios oldoptions
This is the set of (current) terminal attributes to be reused when changing a subset of options.
Definition ofSerial.h:378
bool bHaveEnumeratedDevices
< This vector stores information about all serial devices found.
Definition ofSerial.h:344
virtual ~ofSerial()
Definition ofSerial.cpp:124
long writeBytes(const unsigned char *buffer, size_t length)
This writes bytes into the serial buffer from the buffer pointer passed in.
Definition ofSerial.cpp:505
bool setup()
Attempts to setup the first available device at a baud rate of 9600.
Definition ofSerial.cpp:267
std::vector< ofSerialDeviceInfo > devices
<
Definition ofSerial.h:343
OF_DEPRECATED_MSG("Use listDevices() instead", void enumerateDevices())
Prints out the available serial devices.
int readByte()
Reads and returns a single byte from the requested device.
Definition ofSerial.cpp:575
bool writeByte(unsigned char singleByte)
Writes a single byte to the connected serial device.
Definition ofSerial.cpp:566
int available()
The available method is useful when you want to know how many bytes are available in the serial port....
Definition ofSerial.cpp:663
void flush(bool flushIn=true, bool flushOut=true)
Clears data from one or both of the serial buffers.
Definition ofSerial.cpp:622
void listDevices()
This lists all the available serial devices to the console or standard output.
Definition ofSerial.cpp:223
int fd
<
Definition ofSerial.h:377
long readBytes(unsigned char *buffer, size_t length)
Reads 'length' bytes from the connected serial device.
Definition ofSerial.cpp:555
ofSerial()
Definition ofSerial.cpp:98
void close()
Closes the connection to the serial device.
Definition ofSerial.cpp:243
bool isInitialized() const
Definition ofSerial.cpp:696