Advertisement
Guest User

Untitled

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