A Java class that reads serial port values

Source: Internet
Author: User

 

This is a class that reads values from the serial port through Java extension package (javax. Comm). This class library (javax. Comm) mainly includes:
Serialbean. Java (interface with other applications ),
Serialbuffer. Java (the buffer used to save the data received from the serial port ),
Readserial. Java (the program that reads data from the serial port ).


The overall design concept of the class is:
The implementation of a thread has been listening to the serial port for data transfer, if there is a start to take photos, and get the photos, directly print out. Because commercial secrets are involved, the photo taking function and the photo obtaining function use an empty function. Sorry!

To run this function, you must download the extension package and load it to your classpath. Of course, if you want to test and run this class. It may take a lot of effort.

If you only have one machine, you can use a RS-232 cable to connect COM1 and com2, run serialexample on COM1, and run Windows-provided HyperTerminal program on com2.

If you have two machines, you can use a RS-232 cable to connect the COM1 (or com2) of the two machines and run the routine at one end, the other end runs the Super Terminal Program provided by windows.
 
This class has very detailed annotations. I guess it's hard to understand.

!

Import Java. io. *; import Java. AWT. *; import Java. AWT. print. *; import javax. print. *; import javax. comm. *; // This is a Java extension class library, not included in the standard Java SDK. // If you have not installed this extension class, you need to download this class library class suankouimplements runnable {private Java at Sun's Java site. io. fileoutputstream Fos = NULL; private thread th = NULL; private long Shijian = 500; // set the 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 listening port time // This. duankou = dk; // port number sb = new serialbean (DK); // create an interface object with the parameter issucces = sb. initialize (); // specify the initial serial port and return the initialization result. If the initialization is successful, 1 is returned. Otherwise,-1 th = new thread (this) is returned. // create a thread object th. start (); // create the suankou object and start the startup thread} catch (Java. io. filenotfoundexception Fe) {Fe. printstacktrace ();} catch (exception e) {e. printstacktrace () ;}// the public void Snape () {system. out. println ("");} // you can specify public void getpicture () {system. out. println ("get photo function");} // run method implementing the thread interface public void run () {thread. sleep (Shijian); this. readsuankou ();} // regular To read data and functions through the serial port, if you can read data, call Snape (), getpicture (), function, write log, print private void readsuankou () {// If the initialization is 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 the photo function this. getpicture (); // call the Java function for obtaining photos. text. simpledateformat df = newjava. text. simpledateformat ("eeee-Mmmm-dd-YYYY"); Date = new date (); string dateformat = DF. format (date); FOS. write (dateformat. getbytes (); // record the current time in the log file FOS. write (MSG. getbytes (); // write the read data to the log file // call the print method. Note: to print the data, you must implement Java. AWT. print. printable interface. That is, the getpicture // returned object must implement this interface. because you do not know what object getpicture returns, this printing function cannot be implemented, but the general idea is/** 1, A print service object is required. This can be implemented in three ways: Java must be implemented in versions earlier than jdk1.4. AWT. print. printable interface or through toolkit. getdefatooltoolkit (). getprintjob to get the print service object. In jdk1.4, you can also use javax. print. printserivcelookup is used to locate a printing service object. 2. You need to start a printing job. There are also several implementation methods: Before jdk1.4, you can use Java. AWT. print. printjob (provided by jdk1.1, which is rarely used now) calls the print or printall method to start printing. You can also use Java. AWT. print. the printdialog of printerjob displays the Print dialog box and starts printing using the print method. In jdk1.4, you can use javax. print. the printdialog of serviceui displays the Print dialog box, and then calls the print method to start printing. According to the above prompts, it is not very difficult! */Java. AWT. print. printerjob PJ = new Java. AWT. print. printerjob (); PJ. printdialog (); // display the Print dialog box PJ. print (); // start printing} // sb. writeport ("OK"); can I call this function to return a result to the serial port?} Catch (exception e) {e. printstacktrace () ;}finally {try {FOS. Close (); // stop the serial port detection process and close the serial port. SB. closeport () ;}catch (exception e) {e. printstacktrace () ;}} else {Throw runtimeexception ("An error occurred while reading serial data! ") ;}}// Main function public static void main (string ARGs []) {New suankou (); // The parameter is the interval and port number }}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.