Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * File:
- * sr.c
- *
- * Notes:
- * This file contains native routines for sending and receiving data from
- * a specified serial device.
- *
- * History:
- * 2013/07/06 Written by Simon Platten
- */
- #include <errno.h>
- #include <fcntl.h>
- #include <termios.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include "sr.h"
- // Constants
- #define BUFFER_SIZE 12
- // Common strings
- static char strPortNotReady[] = ", port not ready\n";
- /*
- * Opens the specified device
- */
- static int openDevice(const char* strDevice, int intModes, jboolean blnDebug) {
- // Try to open the port
- int intFD = open(strDevice, intModes | O_NOCTTY | O_NDELAY);
- if ( intFD >= 0 ) {
- fcntl(intFD, F_SETFL, 0);
- }
- struct termios options;
- // Get the current options for the port...
- tcgetattr(intFD, &options);
- // Set the baud rates to 115200...
- cfsetispeed(&options, B115200);
- cfsetospeed(&options, B115200);
- // Enable the receiver and set local mode...
- options.c_cflag |= (CLOCAL | CREAD);
- options.c_cflag &= ~PARENB;
- options.c_cflag &= ~CSTOPB;
- options.c_cflag &= ~CSIZE;
- options.c_cflag |= CS8;
- // disable hardware flow control
- options.c_cflag &= ~CRTSCTS;
- // disable XON XOFF (for transmit and receive)
- options.c_iflag &= ~(IXON | IXOFF | IXANY);
- //options.c_cflag |= CRTSCTS; // enable hardware flow control
- // min carachters to be read
- options.c_cc[VMIN] = 0;
- // Time to wait for data (tenths of seconds)
- options.c_cc[VTIME] = 0;
- // Set the new options for the port...
- // tcsetattr(fd, TCSANOW, &options);
- //Set the new options for the port...
- tcflush(intFD, TCIFLUSH);
- if ( tcsetattr(intFD, TCSANOW, &options) == -1 ) {
- perror("On tcsetattr:");
- }
- if ( intFD < 0 && blnDebug == JNI_TRUE ) {
- printf("%s, port not ready\n", strDevice);
- }
- return intFD;
- }
- /*
- * Class: cantley_co_uk_clsMain
- * Method: receive
- * Signature: (I)Ljava/lang/String;
- */
- JNIEXPORT jbyteArray JNICALL Java_cantley_co_uk_clsMain_receive
- (JNIEnv* pEnv, jobject obj, jstring strDevice, jboolean blnDebug) {
- const char *pstrDevice = (*pEnv)->GetStringUTFChars(pEnv, strDevice, NULL);
- jbyteArray aryRC = NULL;
- if ( pstrDevice ) {
- int intFD = openDevice(pstrDevice, O_RDONLY, blnDebug);
- if ( intFD >= 0 ) {
- aryRC = (*pEnv)->NewByteArray(pEnv, BUFFER_SIZE);
- if ( aryRC ) {
- printf("receive aaa\n");
- char arycData[BUFFER_SIZE];
- int intRead = read(intFD, arycData, BUFFER_SIZE);
- printf("receive bbb: %d\n", intRead);
- (*pEnv)->SetByteArrayRegion(pEnv, aryRC, 0, intRead, (jbyte*)arycData);
- if ( blnDebug == JNI_TRUE ) {
- int i;
- printf("receive[%d]: ", intRead);
- for( i=0; i<intRead; i++ ) {
- printf("%c", arycData[i]);
- }
- printf("\n");
- }
- }
- // Close the device
- close(intFD);
- }
- }
- (*pEnv)->ReleaseStringUTFChars(pEnv, strDevice, pstrDevice);
- return aryRC;
- }
- /*
- * Class: cantley_co_uk_clsMain
- * Method: send
- * Signature: (ILjava/lang/String;)V
- */
- JNIEXPORT void JNICALL Java_cantley_co_uk_clsMain_send
- (JNIEnv* pEnv, jobject obj, jstring strDevice, jstring strData, jboolean blnDebug) {
- const char *pstrDevice = (*pEnv)->GetStringUTFChars(pEnv, strDevice, NULL);
- const char *pstrData = (*pEnv)->GetStringUTFChars(pEnv, strData, NULL);
- int intLength;
- if ( pstrDevice && pstrData && (intLength = strlen(pstrData)) > 0 ) {
- int intFD = openDevice(pstrDevice, O_WRONLY, blnDebug);
- if ( intFD >= 0 ) {
- if ( blnDebug == JNI_TRUE ) {
- printf("send[%d]: %s\n", intLength, pstrData);
- }
- // Write to the device
- write(intFD, pstrData, intLength);
- // Close the device
- close(intFD);
- }
- }
- (*pEnv)->ReleaseStringUTFChars(pEnv, strDevice, pstrDevice);
- (*pEnv)->ReleaseStringUTFChars(pEnv, strData, pstrData);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement