飞思卡尔简易路径检测上位机(JAVA写的)

Code:
  1. importjava.io.*;
  2. importjava.util.*;
  3. importjavax.comm.*;
  4. importjavax.swing.*;
  5. importjava.awt.*;
  6. importjava.awt.event.*;
  7. publicclassSimpleReadimplementsRunnable,SerialPortEventListener{
  8. staticCommPortIdentifierportId;
  9. staticEnumerationportList;
  10. InputStreaminputStream;
  11. SerialPortserialPort;
  12. ThreadreadThread;
  13. introadData=250;
  14. JFrameframe;
  15. classserialPaintextendsJPanelimplementsRunnable
  16. {
  17. publicvoidpaintComponent(GraphicsdrawPaint)
  18. {
  19. drawPaint.setColor(Color.blue);
  20. //drawPaint.fillRect(roadData,20,20,20);
  21. drawPaint.fillOval(roadData,250,10,10);
  22. }
  23. publicvoidrun()
  24. {
  25. go();
  26. }
  27. publicvoidgo()
  28. {
  29. while(true)
  30. {
  31. //roadData++;
  32. System.out.println(roadData);
  33. try{
  34. Thread.sleep(30);
  35. }catch(InterruptedExceptione){
  36. //TODOAuto-generatedcatchblock
  37. e.printStackTrace();
  38. }
  39. frame.repaint();
  40. }
  41. }
  42. }
  43. publicvoidui()
  44. {
  45. frame=newJFrame();
  46. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  47. serialPaintserialpaint=newserialPaint();
  48. frame.add(serialpaint);
  49. frame.setSize(500,500);
  50. frame.setVisible(true);
  51. }
  52. publicvoidserialThread()
  53. {
  54. RunnableserialRunnable=newserialPaint();
  55. ThreadthreadS=newThread(serialRunnable);
  56. threadS.start();
  57. }
  58. publicstaticvoidmain(String[]args){
  59. //
  60. portList=CommPortIdentifier.getPortIdentifiers();
  61. //SimpleReadserialui=newSimpleRead();
  62. //serialui.ui();
  63. //serialui.serialThread();
  64. while(portList.hasMoreElements()){
  65. portId=(CommPortIdentifier)portList.nextElement();
  66. if(portId.getPortType()==CommPortIdentifier.PORT_SERIAL){
  67. if(portId.getName().equals("COM2")){
  68. //if(portId.getName().equals("/dev/term/a")){
  69. SimpleReadreader=newSimpleRead();
  70. reader.ui();
  71. reader.serialThread();
  72. }
  73. }
  74. }
  75. }
  76. publicSimpleRead(){
  77. try{
  78. serialPort=(SerialPort)portId.open("SimpleReadApp",2000);
  79. }catch(PortInUseExceptione){}
  80. try{
  81. inputStream=serialPort.getInputStream();
  82. }catch(IOExceptione){}
  83. try{
  84. serialPort.addEventListener(this);
  85. }catch(TooManyListenersExceptione){}
  86. serialPort.notifyOnDataAvailable(true);
  87. try{
  88. serialPort.setSerialPortParams(9600,
  89. SerialPort.DATABITS_8,
  90. SerialPort.STOPBITS_1,
  91. SerialPort.PARITY_NONE);
  92. }catch(UnsupportedCommOperationExceptione){}
  93. readThread=newThread(this);
  94. readThread.start();
  95. }
  96. publicvoidrun(){
  97. try{
  98. Thread.sleep(20000);
  99. }catch(InterruptedExceptione){}
  100. }
  101. publicvoidserialEvent(SerialPortEventevent){
  102. switch(event.getEventType()){
  103. caseSerialPortEvent.BI:
  104. caseSerialPortEvent.OE:
  105. caseSerialPortEvent.FE:
  106. caseSerialPortEvent.PE:
  107. caseSerialPortEvent.CD:
  108. caseSerialPortEvent.CTS:
  109. caseSerialPortEvent.DSR:
  110. caseSerialPortEvent.RI:
  111. caseSerialPortEvent.OUTPUT_BUFFER_EMPTY:
  112. break;
  113. caseSerialPortEvent.DATA_AVAILABLE:
  114. byte[]readBuffer=newbyte[1];
  115. try{
  116. while(inputStream.available()>0){
  117. intnumBytes=inputStream.read(readBuffer);
  118. }
  119. System.out.print(newString(readBuffer));
  120. roadData=readBuffer[0];
  121. }catch(IOExceptione){}
  122. break;
  123. }
  124. }
  125. }

你可能感兴趣的:(java)