Testing out the PPD42 Air Quality Sensor, with an MSP430 Launchpad and graphing the data with GNUplot.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
6.3 KiB

5 years ago
  1. /*-------------------------------------------------------------------------
  2. | rxtx is a native interface to serial ports in java.
  3. | Copyright 2002-2004 Michal Hobot MichalHobot@netscape.net
  4. | Copyright 1997-2004 by Trent Jarvi taj@parcelfarce.linux.theplanet.co.uk
  5. |
  6. | This library is free software; you can redistribute it and/or
  7. | modify it under the terms of the GNU Library General Public
  8. | License as published by the Free Software Foundation; either
  9. | version 2 of the License, or (at your option) any later version.
  10. |
  11. | This library is distributed in the hope that it will be useful,
  12. | but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. | Library General Public License for more details.
  15. |
  16. | You should have received a copy of the GNU Library General Public
  17. | License along with this library; if not, write to the Free
  18. | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. --------------------------------------------------------------------------*/
  20. #include "StdAfx.h"
  21. #include "rxtxHelpers.h"
  22. /*
  23. nativeGetVersion
  24. accept: none
  25. perform: return the current version
  26. return: version
  27. exceptions: none
  28. comments: This is used to avoid mixing versions of the .jar and
  29. native library.
  30. First introduced in rxtx-1.5-9
  31. * Class: gnu_io_RXTXCommDriver
  32. * Method: nativeGetVersion
  33. * Signature: ()Ljava/lang/String;
  34. */
  35. JNIEXPORT jstring JNICALL Java_gnu_io_RXTXCommDriver_nativeGetVersion(JNIEnv *env, jclass cls)
  36. {
  37. return env->NewStringUTF("RXTX-2.0-1");
  38. }
  39. /*
  40. registerKnownPorts
  41. accept: the type of port
  42. perform: register any ports of the desired type a priori known to this OS
  43. return: JNI_TRUE if any such ports were registered otherwise JNI_FALSE
  44. exceptions: none
  45. comments:
  46. * Class: gnu_io_RXTXCommDriver
  47. * Method: registerKnownPorts
  48. * Signature: (I)Z
  49. */
  50. JNIEXPORT jboolean JNICALL Java_gnu_io_RXTXCommDriver_registerKnownPorts(JNIEnv *env, jobject jobj, jint portType)
  51. {
  52. enum {PORT_TYPE_SERIAL = 1,
  53. PORT_TYPE_PARALLEL,
  54. PORT_TYPE_I2C,
  55. PORT_TYPE_RS485,
  56. PORT_TYPE_RAW};
  57. jboolean result = JNI_FALSE;
  58. switch(portType) {
  59. case PORT_TYPE_SERIAL:
  60. // We could check here what COMs are on system and register them.
  61. break;
  62. case PORT_TYPE_PARALLEL:
  63. case PORT_TYPE_I2C:
  64. case PORT_TYPE_RS485:
  65. case PORT_TYPE_RAW:
  66. break;
  67. default:
  68. // Wrong port type - I'd raise exception, but there's no defined for Java
  69. IF_DEBUG
  70. (
  71. printj(env, L"!!! RXTXCommDriver.registerKnownPorts(%ld): Wrong port type\n", portType);
  72. //MessageBox(NULL, TEXT("RXTXCommDriver.registerKnownPorts(): Wrong port type"), TEXT("Error"), MB_OK);
  73. )
  74. break;
  75. }
  76. return result;
  77. }
  78. /*
  79. isPortPrefixValid
  80. accept: a port prefix
  81. perform: see if the port prefix matches a port that is valid on this OS.
  82. return: JNI_TRUE if it exists otherwise JNI_FALSE
  83. exceptions: none
  84. comments:
  85. * Class: gnu_io_RXTXCommDriver
  86. * Method: isPortPrefixValid
  87. * Signature: (Ljava/lang/String;)Z
  88. */
  89. JNIEXPORT jboolean JNICALL Java_gnu_io_RXTXCommDriver_isPortPrefixValid(JNIEnv *env, jobject jobj, jstring dev)
  90. {
  91. jboolean retVal;
  92. const char *szDev = env->GetStringUTFChars(dev, NULL);
  93. if( strncmp(szDev, "COM", 3) == 0 || strncmp(szDev, "LPT", 3) == 0) // is first 3 chars OK?
  94. retVal = JNI_TRUE;
  95. else
  96. retVal = JNI_FALSE;
  97. env->ReleaseStringUTFChars(dev, szDev);
  98. return retVal;
  99. }
  100. /*
  101. testRead
  102. accept: dev The device to be tested
  103. perform: test if the device can be read from
  104. return: JNI_TRUE if the device can be read from
  105. exceptions: none
  106. comments: From Wayne Roberts wroberts1@home.com
  107. check tcget/setattr returns.
  108. support for non serial ports Trent
  109. * Class: gnu_io_RXTXCommDriver
  110. * Method: testRead
  111. * Signature: (Ljava/lang/String;I)Z
  112. */
  113. JNIEXPORT jboolean JNICALL Java_gnu_io_RXTXCommDriver_testRead(JNIEnv *env, jobject jobj, jstring dev, jint type)
  114. {
  115. jboolean retVal;
  116. DWORD dwError;
  117. if ( type == PORT_SERIAL )
  118. {
  119. const WCHAR *wszDev = env->GetStringChars(dev, NULL);
  120. HANDLE hPort = CreateFileW(wszDev, // Pointer to the name of the port
  121. GENERIC_READ | GENERIC_WRITE,
  122. // Access (read-write) mode
  123. 0, // Share mode
  124. NULL, // Pointer to the security attribute
  125. OPEN_EXISTING,// How to open the serial port
  126. 0, // Port attributes
  127. NULL); // Handle to port with attribute
  128. // to copy
  129. // If it fails to open the port, return FALSE.
  130. if ( hPort == INVALID_HANDLE_VALUE )
  131. { // Could not open the port.
  132. IF_DEBUG
  133. (
  134. ;//printj(env, TEXT("!!! RXTXCommDriver.testRead(%s, %ld): cannot open port\n"), wszDev, type);
  135. //MessageBox(NULL, TEXT("RXTXCommDriver.testRead(): cannot open port"), wszDev /*TEXT("Error")*/, MB_OK | MB_SETFOREGROUND);
  136. )
  137. dwError = GetLastError();
  138. retVal = JNI_FALSE;
  139. }
  140. else
  141. { // Port open OK - let's close it and return TRUE
  142. if (!CloseHandle(hPort))
  143. dwError = GetLastError();
  144. IF_DEBUG
  145. (
  146. ;//printj(env, TEXT("--- RXTXCommDriver.testRead(%s, %ld): port open OK\n"), wszDev, type);
  147. //MessageBox(NULL, TEXT("RXTXCommDriver.testRead(): port open OK"), wszDev /*TEXT("Success")*/, MB_OK | MB_SETFOREGROUND);
  148. )
  149. retVal = JNI_TRUE;
  150. }
  151. env->ReleaseStringChars(dev, wszDev);
  152. }
  153. else
  154. retVal = JNI_FALSE;
  155. return retVal;
  156. }
  157. /*
  158. getDeviceDirectory
  159. accept:
  160. perform:
  161. return: the directory containing the device files
  162. exceptions:
  163. comments: we need it only for Unix
  164. * Class: gnu_io_RXTXCommDriver
  165. * Method: getDeviceDirectory
  166. * Signature: ()Ljava/lang/String;
  167. */
  168. JNIEXPORT jstring JNICALL Java_gnu_io_RXTXCommDriver_getDeviceDirectory(JNIEnv *env, jobject jobj)
  169. {
  170. return env->NewStringUTF("");
  171. }