API
usbDevice.cpp
Go to the documentation of this file.
1 /** \file usbDevice.cpp
2  * \author Jared R. Males
3  * \brief Manage a USB TTY device in the MagAOXApp context
4  *
5  * \ingroup tty_files
6  *
7  */
8 
9 #include "usbDevice.hpp"
10 
11 
12 #include "ttyIOUtils.hpp"
13 #include "ttyUSB.hpp"
14 #include "ttyErrors.hpp"
15 
16 using namespace mx::app;
17 
18 namespace MagAOX
19 {
20 namespace tty
21 {
22 
23 
24 int usbDevice::setupConfig( mx::app::appConfigurator & config )
25 {
26  config.add("usb.idVendor", "", "usb.idVendor", argType::Required, "usb", "idVendor", false, "string", "USB vendor id, 4 digits");
27  config.add("usb.idProduct", "", "usb.idProduct", argType::Required, "usb", "idProduct", false, "string", "USB product id, 4 digits");
28  config.add("usb.serial", "", "usb.serial", argType::Required, "usb", "serial", false, "string", "USB serial number");
29  config.add("usb.baud", "", "usb.baud", argType::Required, "usb", "baud", false, "real", "USB tty baud rate (i.e. 9600)");
30 
31  return 0;
32 }
33 
34 int usbDevice::loadConfig( mx::app::appConfigurator & config )
35 {
36  config(m_idVendor, "usb.idVendor");
37  config(m_idProduct, "usb.idProduct");
38  config(m_serial, "usb.serial");
39 
40  //We read the config as a float to allow 134.5
41  //Then multiply by 10 for the switch statement.
42  float baud = 0;
43  config(baud, "usb.baud");
44 
45  switch((int)(baud*10))
46  {
47  case 0:
48  break; //Don't change default.
49  case 500:
50  m_baudRate = B50;
51  break;
52  case 750:
53  m_baudRate = B75;
54  break;
55  case 1100:
56  m_baudRate = B110;
57  break;
58  case 1345:
59  m_baudRate = B134;
60  break;
61  case 1500:
62  m_baudRate = B150;
63  break;
64  case 2000:
65  m_baudRate = B200;
66  break;
67  case 3000:
68  m_baudRate = B300;
69  break;
70  case 6000:
71  m_baudRate = B600;
72  break;
73  case 18000:
74  m_baudRate = B1800;
75  break;
76  case 24000:
77  m_baudRate = B2400;
78  break;
79  case 48000:
80  m_baudRate = B4800;
81  break;
82  case 96000:
83  m_baudRate = B9600;
84  break;
85  case 192000:
86  m_baudRate = B19200;
87  break;
88  case 576000:
89  m_baudRate = B57600;
90  break;
91  case 384000:
92  m_baudRate = B38400;
93  break;
94  default:
95  break;
96  }
97 
98  if(m_baudRate == 0) return TTY_E_BADBAUDRATE;
99 
100  return getDeviceName();
101 }
102 
103 int usbDevice::getDeviceName()
104 {
105  return ttyUSBDevName( m_deviceName, m_idVendor, m_idProduct, m_serial );
106 }
107 
108 int usbDevice::connect()
109 {
110  if(m_fileDescrip)
111  {
112  ::close(m_fileDescrip);
113  m_fileDescrip = 0;
114  }
115 
116  return ttyOpenRaw( m_fileDescrip, m_deviceName, m_baudRate );
117 }
118 
119 } //namespace tty
120 } //namespace MagAOX
121 
int ttyUSBDevName(std::string &devName, const std::string &vendor, const std::string &product, const std::string &serial)
Get the ttyUSB device name for a specific device.
Definition: ttyUSB.cpp:30
int ttyOpenRaw(int &fileDescrip, std::string &deviceName, speed_t speed)
Open a file as a raw-mode tty device.
Definition: ttyIOUtils.cpp:72
Definition: dm.hpp:24
Error numbers for the tty utilities.
#define TTY_E_BADBAUDRATE
Definition: ttyErrors.hpp:31
Utilities for i/o on a file descriptor pointing to a tty device.
Find the details for USB serial devices.
Manage a USB TTY device in the MagAOXApp context.