}
}
Public void serialEvent (SerialPortEvent event)
{
Switch (event. getEventType ())
{
Case SerialPortEvent. BI:
Case SerialPortEvent. OE:
Case SerialPortEvent. FE:
Case SerialPortEvent. PE:
Case SerialPortEvent. CD:
Case SerialPortEvent. CTS:
Case SerialPortEvent. DSR:
Case SerialPortEvent. RI:
Case SerialPortEvent. OUTPUT_BUFFER_EMPTY:
Break;
Case SerialPortEvent. DATA_AVAILABLE:
Byte [] readBuffer = new byte [200];
Try
{
String fileName = null;
While (inputStream. available ()> 0)
{
Area. append ("there are data available in the port ");
Area. append ("\ n ");
Int numBytes = inputStream. read (readBuffer );
FileName = "/root/data.txt ";
BufferedWriter bw = new BufferedWriter (new FileWriter (fileName ));
Try {
}
Catch (IOException e ){}
Area. append (new String (readBuffer ));
Bw. close ();
}
}
Catch (IOException e ){}
Break;
}
}
Public static void main (String [] args)
{
New test ();
}
} This interface runs on pc a. After selecting port and open, click the read button. When another pc B sends data, it can correctly receive and write the file.
However, after adding a write button, click this button and send the command "ask for reply" to B. After B receives the button, it sends "13900100400" to a, which means that a receives all garbled characters. The Code is as follows: package comm;
Import java. io .*;
Import java. util .*;
ReadThread = new Thread (this );
ReadThread. start ();
}
}
Public void run ()
{
Try
{
Thread. sleep (6000 );
}
Catch (InterruptedException e ){}
}
Public void serialEvent (SerialPortEvent event)
{
Switch (event. getEventType ())
{
Case SerialPortEvent. BI:
Case SerialPortEvent. OE:
Case SerialPortEvent. FE:
Case SerialPortEvent. PE:
Case SerialPortEvent. CD:
Case SerialPortEvent. CTS:
Case SerialPortEvent. DSR:
Case SerialPortEvent. RI:
Case SerialPortEvent. OUTPUT_BUFFER_EMPTY:
Break;
Case SerialPortEvent. DATA_AVAILABLE:
Byte [] readBuffer = new byte [200];
Try
{
String fileName = null;
While (inputStream. available ()> 0)
{
Area. append ("there are data available in the port ");
Area. append ("\ n ");
Int numBytes = inputStream. read (readBuffer );
FileName = "/root/data.txt ";
BufferedWriter bw = new BufferedWriter (new FileWriter (fileName ));
Try {
}
Catch (IOException e ){}
Area. append (new String (readBuffer ));
Bw. close ();
}
}
Catch (IOException e ){}
Break;
}
}
Public static void main (String [] args)
{
New test ();
}
} Only write and read in the added write button actioncommitted med, and read garbled characters when the thread is used. Can't I write and read it at the same time? Is it because thread sleep is not enough time, or is it because the test thread open port is not enough time?
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