#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <string.h>
#include <jni.h>
#include "SerialJNI.h"
int
speed_arr[]={B38400, B19200, B9600, B4800, B2400, B1200, B300,
B38400, B19200, B9600, B4800, B2400, B1200, B300,};
int
name_arr[]={38400, 19200, 9600, 4800, 2400, 1200, 300, 38400,
19200, 9600, 4800, 2400, 1200, 300,};
JNIEXPORT jint JNICALL Java_SerialJNI_OpenDev
(JNIEnv *env, jobject obj, jstring Dev)
{
const
char
*Dev_utf = (*env)->GetStringUTFChars(env, Dev, JNI_FALSE);
int
fd = open(Dev_utf, O_RDWR|O_NOCTTY);
(*env)->ReleaseStringUTFChars(env, Dev, Dev_utf);
if
(-1 == fd)
{
perror
(
"Can't Open Serial Port\n"
);
return
-1;
}
else
{
printf
(
"Serial Open\n"
);
return
fd;
}
}
JNIEXPORT
void
JNICALL Java_SerialJNI_set_1speed
(JNIEnv *env, jobject obj, jint fd, jint speed)
{
int
i;
int
status;
struct
termios Opt;
tcgetattr(fd, &Opt);
for
(i=0; i<
sizeof
(speed_arr)/
sizeof
(
int
); i++)
{
if
(speed == name_arr[i])
{
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr[i]);
cfsetospeed(&Opt, speed_arr[i]);
status = tcsetattr(fd, TCSANOW, &Opt);
if
(status != 0)
perror
(
"tcsetattr fd1\n"
);
return
;
}
tcflush(fd, TCIOFLUSH);
}
}
JNIEXPORT jint JNICALL Java_SerialJNI_set_1Parity
(JNIEnv *env, jobject obj, jint fd, jint databits, jint stopbits, jint parity)
{
struct
termios opt;
if
(tcgetattr(fd, &opt) != 0)
{
perror
(
"SetupSerial 1\n"
);
return
-1;
}
opt.c_cflag &= ~CSIZE;
opt.c_lflag &= ~(ICANON|ECHO|ECHOE|ISIG);
opt.c_oflag &= ~OPOST;
switch
(databits)
{
case
7: opt.c_cflag |= CS7;
break
;
case
8: opt.c_cflag |= CS8;
break
;
default
:
fprintf
(stderr,
"Unsupported data size\n"
);
return
-1;
}
switch
(parity)
{
case
'n'
:
case
'N'
: opt.c_cflag &= ~PARENB;
opt.c_iflag &= ~INPCK;
break
;
case
'o'
:
case
'O'
: opt.c_cflag |= (PARODD|PARENB);
opt.c_iflag |= INPCK;
break
;
case
'e'
:
case
'E'
: opt.c_cflag |= PARENB;
opt.c_cflag &= ~PARODD;
opt.c_iflag |= INPCK;
break
;
case
's'
:
case
'S'
: opt.c_cflag &= ~PARENB;
opt.c_cflag &= ~CSTOPB;
break
;
default
:
fprintf
(stderr,
"Unsupported parity\n"
);
return
-1;
}
switch
(stopbits)
{
case
1: opt.c_cflag &= ~CSTOPB;
break
;
case
2: opt.c_cflag |= CSTOPB;
break
;
default
:
fprintf
(stderr,
"Unsupported stop bits\n"
);
return
-1;
}
if
(parity !=
'n'
) opt.c_iflag |= INPCK;
// tcflush(fd,TCIFLUSH);
opt.c_cc[VTIME] = 150;
/* 设置超时 15 seconds*/
opt.c_cc[VMIN] = 0;
tcflush(fd, TCIFLUSH);
if
(tcsetattr(fd,TCSANOW,&opt) != 0)
{
perror
(
"SetupSerial 3\n"
);
return
-1;
}
return
0;
}
JNIEXPORT
void
JNICALL Java_SerialJNI_write
(JNIEnv *env, jobject obj, jint fd, jstring buff, jint len)
{
const
char
*buff_utf = (*env)->GetStringUTFChars(env, buff, JNI_FALSE);
if
(write(fd, buff_utf, len)>0)
{
printf
(
"write success!\nwrite:%s\n"
, buff_utf);
}
else
{
perror
(
"write failed!\n"
);
}
(*env)->ReleaseStringUTFChars(env, buff, buff_utf);
printf
(
"write finish!\n"
);
return
;
}
JNIEXPORT jstring JNICALL Java_SerialJNI_read
(JNIEnv *env, jobject obj, jint fd, jint len)
{
int
nread=0;
char
tempBuff[len];
jstring jstr;
bzero(tempBuff,
sizeof
(tempBuff));
while
((nread = read(fd, tempBuff, len))>0)
{
tempBuff[nread+1] =
'\0'
;
jstr = (*env)->NewStringUTF(env, tempBuff);
return
jstr;
}
}
JNIEXPORT
void
JNICALL Java_SerialJNI_close_1port
(JNIEnv *env, jobject obj, jint fd)
{
if
(close(fd))
{
perror
(
"close failed!\n"
);
}
else
{
printf
(
"close success!\n"
);
}
}