激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Java使用開源Rxtx實現(xiàn)串口通訊

Java使用開源Rxtx實現(xiàn)串口通訊

2021-06-26 13:32heartbeaty Java教程

這篇文章主要為大家詳細介紹了Java使用開源Rxtx實現(xiàn)串口通訊,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了java使用開源rxtx實現(xiàn)串口通訊的具體代碼,供大家參考,具體內(nèi)容如下

使用方法:

windows平臺:

1、把rxtxparallel.dll、rxtxserial.dll拷貝到:c:\windows\system32下。

2、如果是在開發(fā)的時候(jdk),需要把rxtxcomm.jar、rxtxparallel.dll、rxtxserial.dll拷貝到..\jre...\lib\ext下;如:d:\program files\java\jre1.6.0_02\lib\ext

3、而且需要把項目1.右鍵->2.preperties(首選項)->3.java build path->4.libraries->5.展開rxtxcomm.jar->6.native library location:(none)->7.瀏覽external folder(選擇至該項目的lib文件夾,如:e:/item/myitem/webroot/web-inf/lib).

?
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import gnu.io.*;
import java.io.*;
import java.util.*;
import com.call.dao.*;
 
 public class serialreader extends observable implements runnable,serialporteventlistener
 {
 static commportidentifier portid;
 int delayread = 100;
 int numbytes; // buffer中的實際數(shù)據(jù)字節(jié)數(shù)
 private static byte[] readbuffer = new byte[1024]; // 4k的buffer空間,緩存串口讀入的數(shù)據(jù)
 static enumeration portlist;
 inputstream inputstream;
 outputstream outputstream;
 static serialport serialport;
 hashmap serialparams;
 thread readthread;//本來是static類型的
 //端口是否打開了
 boolean isopen = false;
 // 端口讀入數(shù)據(jù)事件觸發(fā)后,等待n毫秒后再讀取,以便讓數(shù)據(jù)一次性讀完
 public static final string params_delay = "delay read"; // 延時等待端口數(shù)據(jù)準備的時間
 public static final string params_timeout = "timeout"; // 超時時間
 public static final string params_port = "port name"; // 端口名稱
 public static final string params_databits = "data bits"; // 數(shù)據(jù)位
 public static final string params_stopbits = "stop bits"; // 停止位
 public static final string params_parity = "parity"; // 奇偶校驗
 public static final string params_rate = "rate"; // 波特率
 
 public boolean isopen(){
  return isopen;
 }
 /**
  * 初始化端口操作的參數(shù).
  * @throws serialportexception
  *
  * @see
  */
 public serialreader()
 {
  isopen = false;
 }
 
 public void open(hashmap params)
 {
  serialparams = params;
  if(isopen){
  close();
  }
  try
  {
   // 參數(shù)初始化
   int timeout = integer.parseint( serialparams.get( params_timeout )
    .tostring() );
   int rate = integer.parseint( serialparams.get( params_rate )
    .tostring() );
   int databits = integer.parseint( serialparams.get( params_databits )
    .tostring() );
   int stopbits = integer.parseint( serialparams.get( params_stopbits )
    .tostring() );
   int parity = integer.parseint( serialparams.get( params_parity )
    .tostring() );
   delayread = integer.parseint( serialparams.get( params_delay )
    .tostring() );
   string port = serialparams.get( params_port ).tostring();
   // 打開端口
   portid = commportidentifier.getportidentifier( port );
   serialport = ( serialport ) portid.open( "serialreader", timeout );
   inputstream = serialport.getinputstream();
   serialport.addeventlistener( this );
   serialport.notifyondataavailable( true );
   serialport.setserialportparams( rate, databits, stopbits, parity );
   
   isopen = true;
  }
  catch ( portinuseexception e )
  {
   // 端口"+serialparams.get( params_port ).tostring()+"已經(jīng)被占用";
  }
  catch ( toomanylistenersexception e )
  {
   //"端口"+serialparams.get( params_port ).tostring()+"監(jiān)聽者過多";
  }
  catch ( unsupportedcommoperationexception e )
  {
   //"端口操作命令不支持";
  }
  catch ( nosuchportexception e )
  {
   //"端口"+serialparams.get( params_port ).tostring()+"不存在";
  }
  catch ( ioexception e )
  {
   //"打開端口"+serialparams.get( params_port ).tostring()+"失敗";
  }
  serialparams.clear();
  thread readthread = new thread( this );
  readthread.start();
 }
 
  
 public void run()
 {
  try
  {
   thread.sleep(50);
  }
  catch ( interruptedexception e )
  {
  }
 }
 public void start(){
  try {
  outputstream = serialport.getoutputstream();
   }
 catch (ioexception e) {}
 try{
  readthread = new thread(this);
  readthread.start();
 }
 catch (exception e) { }
 } //start() end
 
 
 public void run(string message) {
 try {
  thread.sleep(4);
   }
  catch (interruptedexception e) { }
  try {
  if(message!=null&&message.length()!=0)
  {
  system.out.println("run message:"+message);
   outputstream.write(message.getbytes());
  }
 } catch (ioexception e) {}
 }
 
 
 public void close()
 {
  if (isopen)
  {
   try
   {
    serialport.notifyondataavailable(false);
    serialport.removeeventlistener();
    inputstream.close();
    serialport.close();
    isopen = false;
   } catch (ioexception ex)
   {
   //"關(guān)閉串口失敗";
   }
  }
 }
 
 public void serialevent( serialportevent event )
 {
  try
  {
   thread.sleep( delayread );
  }
  catch ( interruptedexception e )
  {
   e.printstacktrace();
  }
  switch ( event.geteventtype() )
  {
   case serialportevent.bi: // 10
   case serialportevent.oe: // 7
   case serialportevent.fe: // 9
   case serialportevent.pe: // 8
   case serialportevent.cd: // 6
   case serialportevent.cts: // 3
   case serialportevent.dsr: // 4
   case serialportevent.ri: // 5
   case serialportevent.output_buffer_empty: // 2
    break;
   case serialportevent.data_available: // 1
    try
    {
     // 多次讀取,將所有數(shù)據(jù)讀入
      while (inputstream.available() > 0) {
      numbytes = inputstream.read(readbuffer);
      }
      
      //打印接收到的字節(jié)數(shù)據(jù)的ascii碼
      for(int i=0;i<numbytes;i++){
      // system.out.println("msg[" + numbytes + "]: [" +readbuffer[i] + "]:"+(char)readbuffer[i]);
      }
//     numbytes = inputstream.read( readbuffer );
     changemessage( readbuffer, numbytes );
    }
    catch ( ioexception e )
    {
     e.printstacktrace();
    }
    break;
  }
 }
 // 通過observer pattern將收到的數(shù)據(jù)發(fā)送給observer
 // 將buffer中的空字節(jié)刪除后再發(fā)送更新消息,通知觀察者
 public void changemessage( byte[] message, int length )
 {
  setchanged();
  byte[] temp = new byte[length];
  system.arraycopy( message, 0, temp, 0, length );
  notifyobservers( temp );
 }
 static void listports()
 {
  enumeration portenum = commportidentifier.getportidentifiers();
  while ( portenum.hasmoreelements() )
  {
   commportidentifier portidentifier = ( commportidentifier ) portenum
    .nextelement();
   
  }
 }
 
 
 public void openserialport(string message)
 {
  hashmap<string, comparable> params = new hashmap<string, comparable>();
  otherdao odao=new otherdao();
  string port=odao.selectnumberbyid(15);
  string rate = "9600";
  string databit = ""+serialport.databits_8;
  string stopbit = ""+serialport.stopbits_1;
  string parity = ""+serialport.parity_none;
  int parityint = serialport.parity_none;
  params.put( serialreader.params_port, port ); // 端口名稱
  params.put( serialreader.params_rate, rate ); // 波特率
  params.put( serialreader.params_databits,databit ); // 數(shù)據(jù)位
  params.put( serialreader.params_stopbits, stopbit ); // 停止位
  params.put( serialreader.params_parity, parityint ); // 無奇偶校驗
  params.put( serialreader.params_timeout, 100 ); // 設(shè)備超時時間 1秒
  params.put( serialreader.params_delay, 100 ); // 端口數(shù)據(jù)準備時間 1秒
  try {
 open(params);//打開串口
 loginframe cf=new loginframe();
 addobserver(cf);
 if(message!=null&&message.length()!=0)
 {
 string str="";
 for(int i=0;i<10;i++)
 {
  str+=message;
 }
  start();
  run(str);
 }
 } catch (exception e) {
 }
 }
 static string getporttypename( int porttype )
 {
  switch ( porttype )
  {
   case commportidentifier.port_i2c:
    return "i2c";
   case commportidentifier.port_parallel:
    return "parallel";
   case commportidentifier.port_raw:
    return "raw";
   case commportidentifier.port_rs485:
    return "rs485";
   case commportidentifier.port_serial:
    return "serial";
   default:
    return "unknown type";
  }
 }
  
 public hashset<commportidentifier> getavailableserialports()//本來static
 {
  hashset<commportidentifier> h = new hashset<commportidentifier>();
  enumeration theports = commportidentifier.getportidentifiers();
  while ( theports.hasmoreelements() )
  {
   commportidentifier com = ( commportidentifier ) theports
    .nextelement();
   switch ( com.getporttype() )
   {
    case commportidentifier.port_serial:
     try
     {
      commport theport = com.open( "commutil", 50 );
      theport.close();
      h.add( com );
     }
     catch ( portinuseexception e )
     {
      system.out.println( "port, " + com.getname()
       + ", is in use." );
     }
     catch ( exception e )
     {
      system.out.println( "failed to open port "
       + com.getname() + e );
     }
   }
  }
  return h;
 }
}
 
//ascii表
//-------------------------------------------------------------
//     ascii characters      
//      
//dec hex  char code dec hex char
//      
//0  0   nul   64 40 @
//1  1   soh   65 41 a
//2  2   stx   66 42 b
//3  3   etx   67 43 c
//4  4   eot   68 44 d
//5  5   enq   69 45 e
//6  6   ack   70 46 f
//7  7   bel   71 47 g
//8  8   bs    72 48 h
//9  9   ht    73 49 i
//10 0a  lf    74 4a j
//11 0b  vt    75 4b k
//12 0c  ff    76 4c l
//13 0d  cr    77 4d m
//14 0e  so    78 4e n
//15 0f  si    79 4f o
//16 10  sle   80 50 p
//17 11  cs1   81 51 q
//18 12  dc2   82 52 r
//19 13  dc3   83 53 s
//20 14  dc4   84 54 t
//21 15  nak   85 55 u
//22 16  syn   86 56 v
//23 17  etb   87 57 w
//24 18  can   88 58 x
//25 19  em    89 59 y
//26 1a  sib   90 5a z
//27 1b  esc   91 5b [
//        92 5c  \
//28 1c  fs    93 5d ]
//29 1d  gs    94 5e ^
//30 1e  rs    95 5f _
//31 1f  us    96 60 `
//32 20 (space)   97 61 a
//33 21  !    98 62 b
//34 22  "
//        99 63 c
//35 23  #    100 64 d
//36 24  $    
//37 25  %    101 65 e
//38 26  &    102 66 f
//39 27  '    103 67 g
//40 28  (    104 68 h
//41 29  )    105 69 i
//42 2a  *    106 6a j
//43 2b  +    107 6b k
//44 2c  ,    108 6c l
//45 2d  -    109 6d m
//46 2e  .    110 6e n
//47 2f  /    111 6f o
//48 30  0    112 70 p
//49 31  1    113 72 q
//50 32  2    114 72 r
//51 33  3    115 73 s
//52 34  4    116 74 t
//53 35  5    117 75 u
//54 36  6    118 76 v
//55 37  7    119 77 w
//56 38  8    120 78 x
//57 39  9    121 79 y
//58 3a  :    122 7a z
//59 3b  ;    123 7b {
//60 3c  <    124 7c |
//61 3d  =    125 7d }
//62 3e  >    126 7e ~
//63 3f  ?    127 7f

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/liu4071325/article/details/53391800

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 一区二区精品视频在线观看 | 国产精品亚洲一区二区三区在线观看 | 国产电影精品久久 | 万圣街在线观看免费完整版 | 国产欧美精品一区二区三区四区 | 国产精品1区 | 91成人免费电影 | 韩国精品一区二区三区四区五区 | 欧美老逼| 欧美雌雄另类xxxxx | 在线免费观看麻豆 | 久久亚洲春色中文字幕久久 | 日韩精品| 国产免费一区二区三区在线能观看 | 一级黄色电影网站 | 91看片在线观看视频 | 精品久久一区二区三区 | 国产精品视频网 | 国产免费一区二区三区在线能观看 | 黄色大片在线观看 | 一级免费黄视频 | 久久久久亚洲美女啪啪 | 91美女视频在线 | 亚洲午夜免费 | 亚洲国产超高清a毛毛片 | 成人免费福利视频 | av在线久草 | 黄色片在线观看网站 | 嫩嫩的freehdxxx | 成人艳情一二三区 | 欧美日韩亚洲不卡 | 性色tv| aa级黄色片| 一区二区三区在线播放视频 | 久久人人爽人人爽人人片av高请 | 欧产日产国产精品乱噜噜 | 久久精品视频69 | 国产精品久久久久久影视 | 国产一区二区三区网站 | 91精品国产91热久久久做人人 | 一区二区三区欧美在线观看 |