Java Write serial port

Source: Internet
Author: User

Java host computer to the microcontroller to write serial instructions, the simple code is as follows:

Import java.io.inputstream;import java.io.outputstream;import javax.comm.*;p ublic class Test {public     static void Main (String args[]) throws Exception    {        Commportidentifier Serialportid = Commportidentifier.getportidentifier ("COM3");           SerialPort  port = (SerialPort) serialportid.open ("Read", +);          Open the COM3 serial port, where 30 is the timeout time to open the serial port          port.setserialportparams (9600, 8, 1, 0);//Set COM2 baud rate, data bit, stop bit, check mode          // Get input output stream from the string         byte[] buffer = {0x0f,0x01,0x00};//controlling command        outputstream out = Port.getoutputstream ( );           Out. write (buffer);            Out. Flush ();           InputStream in = Port.getinputstream ();           In.read (data); Data is a byte[]    }}
Reference

[1].http://blog.csdn.net/pigteacher/article/details/5619955

Java Write serial port

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.