Advertisement
Guest User

Untitled

a guest
Jul 17th, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. /**
  2. * File:
  3. * sr.c
  4. *
  5. * Notes:
  6. * This file contains native routines for sending and receiving data from
  7. * a specified serial device.
  8. *
  9. * History:
  10. * 2013/07/06 Written by Simon Platten
  11. */
  12. #include <errno.h>
  13. #include <fcntl.h>
  14. #include <termios.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <unistd.h>
  21.  
  22. #include "sr.h"
  23. // Constants
  24. #define BUFFER_SIZE 12
  25. /*
  26. * Opens the specified device
  27. */
  28. static int openDevice(const char* strDevice, int intModes, jboolean blnDebug) {
  29. // Try to open the port
  30. int intFD = open(strDevice, intModes | O_NOCTTY | O_NDELAY);
  31.  
  32. if ( intFD >= 0 ) {
  33. fcntl(intFD, F_SETFL, 0);
  34. }
  35. struct termios options;
  36. // Get the current options for the port...
  37. tcgetattr(intFD, &options);
  38. // Set the baud rates to 115200...
  39. cfsetispeed(&options, B115200);
  40. cfsetospeed(&options, B115200);
  41. // Enable the receiver and set local mode...
  42. options.c_cflag |= (CLOCAL | CREAD);
  43. options.c_cflag &= ~PARENB;
  44. options.c_cflag &= ~CSTOPB;
  45. options.c_cflag &= ~CSIZE;
  46. options.c_cflag |= CS8;
  47. // disable hardware flow control
  48. options.c_cflag &= ~CRTSCTS;
  49. // disable XON XOFF (for transmit and receive)
  50. options.c_iflag &= ~(IXON | IXOFF | IXANY);
  51. //options.c_cflag |= CRTSCTS; // enable hardware flow control
  52. // min carachters to be read
  53. options.c_cc[VMIN] = 0;
  54. // Time to wait for data (tenths of seconds)
  55. options.c_cc[VTIME] = 0;
  56. // Set the new options for the port...
  57. // tcsetattr(fd, TCSANOW, &options);
  58. //Set the new options for the port...
  59. tcflush(intFD, TCIFLUSH);
  60. if ( tcsetattr(intFD, TCSANOW, &options) == -1 ) {
  61. perror("On tcsetattr:");
  62. }
  63. if ( intFD < 0 && blnDebug == JNI_TRUE ) {
  64. printf("%s, port not ready\n", strDevice);
  65. }
  66. return intFD;
  67. }
  68. /*
  69. * Class: cantley_co_uk_clsMain
  70. * Method: receive
  71. * Signature: (I)Ljava/lang/String;
  72. */
  73. JNIEXPORT jbyteArray JNICALL Java_cantley_co_uk_clsMain_receive
  74. (JNIEnv* pEnv, jobject obj, jstring strDevice, jboolean blnDebug) {
  75. const char *pstrDevice = (*pEnv)->GetStringUTFChars(pEnv, strDevice, NULL);
  76. jbyteArray aryRC = NULL;
  77.  
  78. if ( pstrDevice ) {
  79. int intFD = openDevice(pstrDevice, O_RDONLY | O_NONBLOCK, blnDebug);
  80.  
  81. if ( intFD >= 0 ) {
  82. aryRC = (*pEnv)->NewByteArray(pEnv, BUFFER_SIZE);
  83.  
  84. if ( aryRC ) {
  85. printf("receive aaa\n");
  86. char arycData[BUFFER_SIZE];
  87. int intIdx = 0;
  88.  
  89. while( read(intFD, &arycData[intIdx], 1) != -1 ) {
  90. if ( ++intIdx >= BUFFER_SIZE ) {
  91. break;
  92. }
  93. }
  94. printf("receive bbb: %d\n", intIdx);
  95. (*pEnv)->SetByteArrayRegion(pEnv, aryRC, 0, intIdx, (jbyte*)arycData);
  96.  
  97. if ( blnDebug == JNI_TRUE ) {
  98. int i;
  99. printf("%s, receive[%d]: ", pstrDevice, intIdx);
  100. for( i=0; i<intIdx; i++ ) {
  101. printf("%c", arycData[i]);
  102. }
  103. printf("\n");
  104. }
  105. }
  106. // Close the device
  107. close(intFD);
  108. }
  109. }
  110. (*pEnv)->ReleaseStringUTFChars(pEnv, strDevice, pstrDevice);
  111. return aryRC;
  112. }
  113. /*
  114. * Class: cantley_co_uk_clsMain
  115. * Method: send
  116. * Signature: (ILjava/lang/String;)V
  117. */
  118. JNIEXPORT void JNICALL Java_cantley_co_uk_clsMain_send
  119. (JNIEnv* pEnv, jobject obj, jstring strDevice, jstring strData, jboolean blnDebug) {
  120. const char *pstrDevice = (*pEnv)->GetStringUTFChars(pEnv, strDevice, NULL);
  121. const char *pstrData = (*pEnv)->GetStringUTFChars(pEnv, strData, NULL);
  122. int intLength;
  123.  
  124. if ( pstrDevice && pstrData && (intLength = strlen(pstrData)) > 0 ) {
  125. int intFD = openDevice(pstrDevice, O_WRONLY, blnDebug);
  126.  
  127. if ( intFD >= 0 ) {
  128. // Write to the device
  129. int intWritten = write(intFD, pstrData, intLength);
  130.  
  131. if ( blnDebug == JNI_TRUE ) {
  132. printf("%s, send[%d]: %s\n", pstrDevice, intWritten, pstrData);
  133. }
  134. // Close the device
  135. close(intFD);
  136. }
  137. }
  138. (*pEnv)->ReleaseStringUTFChars(pEnv, strDevice, pstrDevice);
  139. (*pEnv)->ReleaseStringUTFChars(pEnv, strData, pstrData);
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement