Read the serial number (original) via Java

zhaozj2021-02-16  55

This is a class read value from serial port through JavaX.comm, this class library (Javax.comm) mainly includes: serialbean.java (interface with other applications), serialbuffer.java (used to Save buffers that receive data received from the serial port), readserial.java (program from serial port reading data).

The design idea of ​​the entire class is probably: implementing a thread has been in the serial port whether there is data transfer, if there is, then print out, then print it directly. Because the business secret is involved, the photo function and the photo function are empty, please forgive me! To run this function, you must download the extension package and load it under your classpath. Of course, you want to test and run this. It may cost a lot of energy. If you have only one machine, you can connect COM1 and COM2 using a RS-232 cable, run SerialExample on COM1, running Windows on COM2, and the Hyper Terminus provided by Windows. If you have two machines, you can connect the COM1 (or COM2) of the two machines using a RS-232 cable, run the routine at one end, and the other end runs the Hyper Terminal Program provided by Windows. This class has a considerable detailed note. I think it should be not difficult to understand! Import java.io. *; Import java.awt. *; Import java.awt.print. *; Import javax.print. *; Import javax.comm. *; // This is a The Java extension class is not included in the standard Java SDK. // If you have not installed this extension class, then you need to download this class library from the Sun's Java site.

class SuanKou implements Runnable {private java.io.FileOutputStream fos = null; private Thread th = null; private long shiJian = 500; // set time interval // private int duanKou = 1; // set the port number private String Msg = NULL; Private SerialBean SB = NULL; Private Int Issucces = -1; // Constructor PUBLIC SUANKOU (Long Sj, Int DK) {System.out.Println ("Constructor"); try {fos = new java.io. FileOutputStream ("c: //log.txt"); this.shijian = SJ; // Set the time //this.duankou=dk; // port number SB = New SerialBean (DK); // creation An interface object, the parameter is the port number Issucces = sb.initialize (); // Estably the specified serial port and returns the initialization result. If the initialization is successfully returned 1, otherwise returns -1 th = new three (this); // Create a thread object th.Start (); // Create this object to start the thread} catch (java.io.filenotfoundexception Fe) {Fe.PrintStackTrace ();} catch (exception e) {E.PrintStackTrace ();}}

// Take a photo function public void snape () {system.out.println ("photo function");}

// Number of photo functions public void getPicture () {system.out.println ("acquired photo function");} // RUN method for thread interface PUBLIC VOID RUN () {thread.sleep (shijian); this.Readsuankou ); // regularly to the serial port reading data and functions, if you can read the data, call the snape (), getpicture (), function, write log, print privacy void readsuankou () {// If you have successful, IF (issucces == 1) {// Read a string of the specified length from the serial port (buffer). (500) Try {msg = sb.readport (500);

/ / If you can read data from the serial port

IF (msg! = null) {this.snape (); // call camera function this.getPicture (); // call acquisition photo function java.text.SIMPLEDATEFORMAT DF = New java.text.SIMPLEDATEFORMAT ("Eeee-mmmm DD-YYYY ");

Date Date = new date ();

String DateFormat = DF.FORMAT (date); fos.write ()); // Record the current time fos.write (msg.getbytes ()) in the log file; // Write the data read Log file // Call the print method note: To print, the object to be printed must implement the java.awt.print.printable interface. That is, the object returned by getPicture must implement the interface, because I don't know what object returns, this print function cannot be implemented, but the approximate idea is / ** 1, which requires a print service object. This can be implemented in three ways: the version before JDK1.4, you must implement the java.awt.print.printable interface or through Toolkit.getDefaultToolkit (). GetPrintJob to get the print service object; you can also in JDK1.4. Locate a print service object by javax.print.printserivcelookup.

2, you need to start a print job. This also has several implementation methods: You can start printing in java.awt.print.printJob before JDK1.4, now it is rarely used, and now you can also pass Java.Awt .print.printerjob's PrintDialog Displays the print dialog box and then starts printing by the Print method; in JDK1.4, you can display the print dialog through the Javax.Print.Serviceui's PrintDialog, and then call the print method to start a print job. According to the above tips, it will not be difficult! * / Java.awt.print.printerjob pj = new java.awt.print.printerJob (); pj.printdialog (); // Display print dialog Pj.print ); // Start printing}

//Sb.writeport ("om "); if you want to return a result to the serial port to call this function?

} catch (exception e) {E.PrintStackTrace (); finally {try {fos.close (); // Stop the serial detection process and turn off the serial port. Sb.closeport ();} catch (exception e) {E.PrintStackTrace ();}}

} Else {throw runtimeException ("Error reading serial port data!");

}

// Main function public static void main (String args []) {new surange (1000, 1); // parameter is interval time, and port number

}

转载请注明原文地址:https://www.9cbs.com/read-21459.html

New Post(0)