Java serial port debugging code is confusing.

Source: Internet
Author: User

 

// There are three classes in total: commf. Java sendcomm. Java readcomm. Java

// The total form. Commf. Java

Package youknow;

Import java. AWT .*;
Import java. AWT. event .*;

Import javax. Comm. serialportevent;
Import javax. Comm. serialporteventlistener;
Import javax. Swing .*;
Import javax. Swing. Border. etchedborder;
Import java. Io. inputstream;
Import java. Io. outputstream;
Import java. util. enumeration;
Import javax. Comm .*;

Public class commf extends jframe implements actionlistener, runnable, serialporteventlistener {
Private Static final long serialversionuid = 1l;


Public jbutton software description, SD, qfs, qjs, quit, Clo, qd, open;
Public jlabel L1, L2, L3, L4, L5, L6, MS, FS, JS, xs; // missing multiple select drop-down box.
Public jcombobox x1, x2, X3, X4, X5;
Public jcheckbox F16, j16, ZD; // check box
Public jtextarea F1, J1, subl;
Public jtextfield TE1, io_message;
Inputstream;
Int I = 0;
// Public String J = new string (); // defines the tag
 
Static commp ortidentifier portid;
/* Enumeration is an enumeration class, in util */
@ Suppresswarnings ("rawtypes ")
Static enumeration portlist = commp ortidentifier. getportidentifiers ();
Outputstream;
/* Serial port of RS-232 */
SerialPort;
Thread readthread;

Byte data [] = new byte [10240];
/* Set the flag to determine whether to disable the serial port */
Boolean mark;
Public commf (){
Super ("Java serial port debugging assistant ");
Software Description = new jbutton ("debugging ");
Xs = new jlabel ("display sending status :");
Io_message = new jtextfield (25 );

L1 = new jlabel ("Serial Port Number ");
L2 = new jlabel ("baud rate ");
L3 = new jlabel ("check bit ");
L4 = new jlabel ("Data bit ");
L5 = new jlabel ("Stop bit ");
X1 = new jcombobox ();

// Obtain the serial port number of the computer and add it to the form.

While (portlist. hasmoreelements ()){
Portid = (comatrix ortidentifier) portlist. nextelement ();
If (portid. getporttype () = comatrix ortidentifier. port_serial ){
X1.additem (portid. getname ());
}
}

// X1.setselectedindex (0); // jcombobox sets the default value,
// 0 indicates the first item. -1 indicates that no selection is made.
X1.setmaximumrowcount (3 );

X2 = new jcombobox ();
X2.additem ("300 ");
X2.additem ("600 ");
X2.additem ("1200 ");
X2.additem ("2400 ");
X2.additem ("4800 ");
X2.additem ("9600 ");
X2.additem ("14400 ");
X2.additem ("19200 ");
X2.additem ("28800 ");
X2.additem ("38400 ");
X2.additem ("57600 ");
X2.additem ("115200 ");
X2.setselectedindex (5 );
X2.setmaximumrowcount (3 );

X3 = new jcombobox ();
X3.additem ("NONE ");
X3.additem ("odd ");
X3.additem ("even ");
X3.additem ("mark ");
X3.additem ("space ");
X3.setmaximumrowcount (3 );

X4 = new jcombobox ();
X4.additem ("8 ");
X4.additem ("7 ");
X4.additem ("6 ");

X5 = new jcombobox ();
X5.additem ("1 ");
X5.additem ("1.5 ");
X5.additem ("2 ");

Container contentpane1 = getcontentpane (); // define container 1
Contentpane1.setlayout (New borderlayout ());
Jpanel pt1 = new jpanel (New gridlayout (5, 2 ));

Pt1.add (L1 );
Pt1.add (X1 );
Pt1.add (L2 );
Pt1.add (X2 );
Pt1.add (L3 );
Pt1.add (X3 );
Pt1.add (L4 );
Pt1.add (X4 );
Pt1.add (L5 );
Pt1.add (X5 );

SD = new jbutton ("Send manually ");
Qfs = new jbutton ("Clear sending area ");
F16 = new jcheckbox ("hexadecimal transmission", false );
Jpanel pt2 = new jpanel (New gridlayout (3, 1 ));
Pt2.add (SD );
Pt2.add (qfs );
Pt2.add (F16 );
 
Jpanel pt3 = new jpanel (New gridlayout (2, 2 ));
Clo = new jbutton ("Close serial port ");
// Open = new jbutton ("open serial port ");
Qjs = new jbutton ("Clear the receiving area ");
Quit = new jbutton ("Exit program ");
J16 = new jcheckbox ("hexadecimal receipt ");
Pt3.add (ClO );
// Pt3.add (open );
Pt3.add (qjs );
Pt3.add (quit );
Pt3.add (j16 );

FS = new jlabel ("sending area ");
ZD = new jcheckbox ("auto send", false );
L6 = new jlabel ("automatic sending cycle :");
TE1 = new jtextfield ("1000 ");
MS = new jlabel ("Ms ");
JS = new jlabel ("receiving area ");
Jpanel pt5 = new jpanel (New flowlayout ());

Pt5.add (ZD );
Pt5.add (l6 );
Pt5.add (TE1 );
Pt5.add (MS );
 
F1 = new jtextarea ();
Jscrollpane jp1 = new jscrollpane (F1); // you can specify the border of a text box.
F1.setcolumns (30 );
F1.setrows (5 );
F1.setlinewrap (true );
F1.setborder (borderfactory. createetchedborder (etchedborder. Raised); // you can specify a border.
F1.settext ("Java debugging software"); // The settext method specifies the text content in jtextfield.
// Textarea = new jtextarea (5, 15 );).
// Rows is the height of jtextarea, in the unit of action;
// Columns is the width of jtextarea, in characters.
J1 = new jtextarea ();
// Frame. setdefaclocloseoperation (jframe. exit_on_close); // window hiding
Jscrollpane JP2 = new jscrollpane (J1 );
J1.setcolumns (30 );
J1.setrows (5 );
J1.setlinewrap (true );
J1.setborder (borderfactory. createetchedborder (etchedborder. Raised); // display the text border

Jpanel pT7 = new jpanel (New gridbaglayout ());
Gridbagconstraints c = new gridbagconstraints ();

// J = f1.gettext ();

C. gridx = 0;
C. gridy = 0;
C. gridwidth = 2;
C. gridheight = 1;
Pt7.add (XS, C );


C. gridx = 2;
C. gridy = 0;
C. gridwidth = 4;
C. gridheight = 1;
Pt7.add (io_message, c); // enable or disable the serial port prompt label)

C. gridx = 8;
C. gridy = 0;
C. gridwidth = 1;
C. gridheight = 1;
Pt7.add (software description, c); // Wei Kang Electronics)

C. gridx = 0;
C. gridy = 1;
C. gridwidth = 2;
C. gridheight = 5;
Pt7.add (pt1, c); // serial port number, baud rate, etc)

C. gridx = 3;
C. gridy = 1;
C. gridwidth = 1;
C. gridheight = 5;
Pt7.add (pt2, c); // manually send

C. gridx = 1;
C. gridy = 8;
C. gridwidth = 2;
C. gridheight = 4;
Pt7.add (pt3, c); // close the serial port and clear the receiving area. (0, 8, 2, 2)

C. gridx = 4;
C. gridy = 1;
C. gridwidth = 5;
C. gridheight = 1;
Pt7.add (pt5, c); // The automatic sending part. (3, 1, 5)

C. gridx = 4;
C. gridy = 1;
C. gridwidth = 1;
C. gridheight = 1;
Pt7.add (FS, c); // sending area. (3, 2, 1, 1)

C. gridx = 4;
C. gridy = 2;
C. gridwidth = 5;
C. gridheight = 4;
Pt7.add (jp1, c); // The text field of the sending area. (3, 2, 5, 4)

C. gridx = 4;
C. gridy = 6;
C. gridwidth = 1;
C. gridheight = 1;
Pt7.add (JS, c); // receiver tag. (3, 6, 1, 1)

C. gridx = 4;
C. gridy = 8;
C. gridwidth = 5;
C. gridheight = 4;
Pt7.add (JP2, c); // The text field of the receiving area. (3, 8, 5, 4)

Contentpane1.add (pT7, borderlayout. center );
// Contentpane1.add (in_message, borderlayout. South );
Pack ();

This. setbounds (300,250,680,350); // setbounds (int x, int y, int width, int height)
Setvisible (true );
Setresizable (false); // set the fixed size of the form. That is, the form size cannot be changed.

Qfs. addactionlistener (this );
Qjs. addactionlistener (this );
SD. addactionlistener (this );
Quit. addactionlistener (this );
Software Description. addactionlistener (this );
Clo. addactionlistener (this );
// ZD. addactionlistener (this );

Qd = new jbutton ("OK ");
// QD. addactionlistener (this );




}
 
Public static void main (string ARGs []) {

New commf ();
}

@ Override
Public void actionreceivmed (actionevent E)
{
Jbutton but = (jbutton) E. getsource ();
Jframe subw = new jframe ("debugging ");
Sendcomm SC = new sendcomm ();
Readcomm rc = new readcomm ();

If (but = qfs)
F1.settext (null );
If (but = qjs)
J1.settext (null );
If (but = SD)
// J1.settext (f1.gettext (); // only once entered.
J1.append (f1.gettext (); // input n multiple times consecutively.
If (but = quit)
System. Exit (0 );
If (but = qd)
Addwindowlistener (New windowadapter ()
{
Public void windowclosing (windowevent E)
{
Dispose ();
}
});
If (but = software description)
{
This. setvisible (false );
Subl = new jtextarea ();
Subl. settext ("debugging represents everything. ");
Subl. setcolumns (17 );
Subl. setrows (5 );
Subl. setlinewrap (true );
Jpanel subj = new jpanel ();
Subj. Add (subl );
Subj. Add (Qd );
Subw. Add (subj );
Subw. setbounds (550,300,300,150 );
Subw. setresizable (false );
This. setvisible (true );
Software Description. setenabled (false );
Subw. setvisible (true );
// This. getparent (). setvisible (false); obtain the parent container and set it to invisible.
}

If (E. getsource () = Clo)
{
SerialPort. Close (); // close the serial port
Clo. setactioncommand ("Close serial port ");
Clo. settext ("enable serial port ");
Mark = true; // run () method used to abort a thread
Io_message.settext ("Serial Port COM1 has been disabled, stop sending data .");
}
Else
SC. clopen ();

If (x1.getselecteditem (). tostring (). Equals ("COM1 "))

RC. ACC ();
}
@ Override
Public void serialevent (serialportevent arg0 ){
// Todo auto-generated method stub

}

@ Override
Public void run (){
// Todo auto-generated method stub

}
}

 

// This class is the sendcomm class. Send for serial port

Package youknow;

Import java. Io. ioexception;
Import javax. Comm .*;

Public class sendcomm extends commf
{
Private Static final long serialversionuid = 1l;
Thread readthread;
Public void clopen ()
{
Clo. setactioncommand ("enable serial port ");
Clo. settext ("Close serial port ");
Mark = false;
/* Read data by byte from the shard area */
Data = f1.gettext (). getbytes ();
Clo. settext ("Close serial port ");
/* Open the serial port */
Start ();
Io_message.settext ("the serial port COM1 is enabled, and data is being sent every 2 seconds .....");
} // Sendcomm () End

 

/* Open the serial port and call the thread to send data */
Public void start ()
{
/* Obtain all communication ports in the system */
/* Use the loop structure to find the serial port */
While (portlist. hasmoreelements ()){
/* Forcibly convert to communication port type */
Portid = (comatrix ortidentifier) portlist. nextelement ();
If (portid. getporttype () = comatrix ortidentifier. port_serial ){
If (x1.getselecteditem (). tostring (). Equals ("COM1 ")){
/* Open the serial port */
Try {
SerialPort = (SerialPort) portid. Open ("readcomm", 2000 );
}
Catch (portinuseexception e ){
// This. theapp. getmessagedlg (). messagelabel. settext (portname + "Serial Port occupied ");
}
/* Set the serial output stream */
Try {
Outputstream = SerialPort. getoutputstream ();
}
Catch (ioexception e ){}
} // If end
} // If end
} // While end
/* Call the thread to send data */
Try {
Readthread = new thread (this );
// The thread is responsible for sending data every time, sleeping for 2 seconds
Readthread. Start ();
}
Catch (exception e ){}
} // Start () End

/* Send data, and re-send after 2 seconds of hibernation */
Public void run (){
/* Set serial communication parameters */
Try {SerialPort. setserialportparams (9600,
SerialPort. databits_8,
SerialPort. stopbits_1,
SerialPort. parity_none );
}
Catch (unsupportedcommoperationexception e ){}
/* Send data streams (send the data in the array data )*/
Try {
Outputstream. Write (data );
}
Catch (ioexception e ){}
/* Sleep for 2 seconds after the data is sent and resend */
Try {
Thread. Sleep (2000 );
If (Mark ){
Return; // end the run method, resulting in thread death
}
Start ();
}
Catch (interruptedexception e ){}
// Sendcomm ()
}
}

 

// The following classes are readcomm classes. Is the serial receiving data class

Package youknow;

Import java. Io. ioexception;
Import java. util. toomanylistenersexception;

Import javax. Comm .*;

Public class readcomm extends commf
{
Private Static final long serialversionuid = 1l;
Thread readthread;
Public void ACC (){
/* Obtain all communication ports in the system */
Portlist = commp ortidentifier. getportidentifiers ();
/* Use the loop structure to find the serial port */
While (portlist. hasmoreelements ()){
/* Forcibly convert to communication port type */
Portid = (comatrix ortidentifier) portlist. nextelement ();
If (portid. getporttype () = comatrix ortidentifier. port_serial ){
If (x1.getselecteditem (). tostring (). Equals ("COM1 ")){
Try {
SerialPort = (SerialPort) portid. Open ("readcomm", 2000 );
Io_message.settext ("the port COM1 is enabled and receiving data .....");
}
Catch (portinuseexception e ){}

/* Set the serial port listener */
Try {
SerialPort. addeventlistener (this );
}
Catch (toomanylistenersexception e ){}
/* Listen to the serial port for data and trigger the serial port event */
SerialPort. yonyondataavailable (true );
} // If end
} // If end
} // While end
Readthread = new thread (this );
Readthread. Start (); // The thread is responsible for every 20 seconds of Data sleep received
} // Action=med () End

/* Sleep for 20 seconds after receiving data */
Public void run (){
Try {
Thread. Sleep (20000 );
}
Catch (interruptedexception e ){}
} // Run () End

/* Set the serial communication parameters for the events triggered by the serial port listener, read the data, and write the data to the serial port listener */
Public void serialevent (serialportevent event ){
/* Set serial communication parameters: baud rate, data bit, stop bit, and parity */
Try {
SerialPort. setserialportparams (9600, SerialPort. databits_8, SerialPort. stopbits_1, SerialPort. parity_none );
}
Catch (unsupportedcommoperationexception e ){}
Byte [] readbuffer = new byte [20];
Try {
Inputstream = SerialPort. getinputstream ();
}
Catch (ioexception e ){}
Try {
/* Read data streams from the line */
While (inputstream. Available ()> 0 ){
// Int numbytes = inputstream. Read (readbuffer );
} // While end
String STR = new string (readbuffer );
/* The received data is stored in the Region */
J1.append (STR + "\ n ");
}
Catch (ioexception e ){}
} // Serialevent () End
// Class r_frame end

}
 

 

I know that extended commf in sendcomm. Java and readcomm. Java is incorrect. However, if commf is not inherited, the controls in the two classes must be defined by themselves. In this way, it cannot be connected to the control in commf.

The question is: How to Run commf. Java to trigger events in sendcomm and readcomm by touching the control in commf .?????

 

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.