Java,J2EE,Weblogic,Oracle

java项目随笔
随笔 - 90, 文章 - 6, 评论 - 61, 引用 - 0
数据加载中……

收藏 JNI串口通信多文件调用时异常

http://bbs.csdn.net/topics/370094675


C/C++ code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#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;
        defaultfprintf(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;
        defaultfprintf(stderr, "Unsupported parity\n");
             return -1;
               
    }
    switch(stopbits)
    {
        case 1: opt.c_cflag &= ~CSTOPB; 
                           break
        case 2: opt.c_cflag |= CSTOPB;           
            break;
        defaultfprintf(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");
    }
}

头文件
C/C++ code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class SerialJNI */
 
#ifndef _Included_SerialJNI
#define _Included_SerialJNI
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     SerialJNI
 * Method:    OpenDev
 * Signature: (Ljava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_SerialJNI_OpenDev
  (JNIEnv *, jobject, jstring);
 
/*
 * Class:     SerialJNI
 * Method:    set_speed
 * Signature: (II)V
 */
JNIEXPORT void JNICALL Java_SerialJNI_set_1speed
  (JNIEnv *, jobject, jint, jint);
 
/*
 * Class:     SerialJNI
 * Method:    set_Parity
 * Signature: (IIII)I
 */
JNIEXPORT jint JNICALL Java_SerialJNI_set_1Parity
  (JNIEnv *, jobject, jint, jint, jint, jint);
 
/*
 * Class:     SerialJNI
 * Method:    write
 * Signature: (ILjava/lang/String;I)V
 */
JNIEXPORT void JNICALL Java_SerialJNI_write
  (JNIEnv *, jobject, jint, jstring, jint);
 
/*
 * Class:     SerialJNI
 * Method:    read
 * Signature: (II)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_SerialJNI_read
  (JNIEnv *, jobject, jint, jint);
 
/*
 * Class:     SerialJNI
 * Method:    close_port
 * Signature: (I)V
 */
JNIEXPORT void JNICALL Java_SerialJNI_close_1port
  (JNIEnv *, jobject, jint);
 
#ifdef __cplusplus
}
#endif
#endif



posted on 2013-03-05 19:26 龚椿深 阅读(766) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: