Send data to the serial port and obtain the returned value

Source: Internet
Author: User

Implementation requirements: use Java code to send the command "aa00dd0000000055" to the circuit board through a serial port to obtain the returned value "aa00dd03000055" [note here, no ff]. The hardware is good after testing by the serial port debugging tool.

The proof is as follows:

 

 

Java implements its Function Code as follows:

Package COM. main2; </P> <p> Import GNU. io. commp ortidentifier; <br/> Import GNU. io. serialPort; <br/> Import GNU. io. serialportevent; <br/> Import GNU. io. serialporteventlistener; </P> <p> Import Java. io. bytearrayoutputstream; <br/> Import Java. io. inputstream; <br/> Import Java. io. outputstream; <br/> Import Java. util. enumeration; </P> <p> public class test extends thread implements serialporteventlistener {<br/> stat IC enumeration portlist; <br/> static commp ortidentifier portid; <br/> static inputstream; <br/> static outputstream; <br/> static SerialPort; <br/> static int COUNT = 0; </P> <p> Public static void main (string [] ARGs) {<br/> portlist = commp ortidentifier. getportidentifiers (); <br/> while (portlist. hasmoreelements () {<br/> portid = (comatrix ortidentifier) portlist. nextelem ENT (); <br/> If (portid. getporttype () = comatrix ortidentifier. port_serial) {<br/> If (portid. getname (). equals ("com3") {<br/> test = new test (); <br/>}</P> <p> Public test () {<br/> try {<br/> // open the communication <br/> SerialPort = (SerialPort) portid. open ("readcommapp", 2000); <br/> SerialPort. setrts (false); // This step can be omitted. It is assumed that the default value is false </P> <p> SerialPort. setserialportparams (2400, SerialPort. databit S_8, SerialPort. stopbits_1, SerialPort. parity_none); </P> <p> byte [] bt = new byte [] {(byte) 0xaa, (byte) 0x00, (byte) 0xdd, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x55 }; </P> <p> outputstream = SerialPort. getoutputstream (); <br/> outputstream. write (BT); <br/> outputstream. flush (); <br/> outputstream. close (); <br/> thread. sleep (200); // 50 ~~ 1400 </P> <p> SerialPort. setrts (true); <br/> inputstream = SerialPort. getinputstream (); </P> <p> SerialPort. addeventlistener (this); <br/> SerialPort. notifyondataavailable (true); <br/>} catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p> Public void serialevent (serialportevent event) {<br/> try {<br/> If (event. geteventtype () = event. data_available) {<br/> bytearrayoutputstream Bos = new byte Arrayoutputstream (); <br/> int ch; <br/> while (CH = inputstream. Read ())! =-1) {<br/> count ++; <br/> If (count> 2) {<br/> Bos. write (CH); <br/>}</P> <p >}< br/> byte [] bt = Bos. tobytearray (); <br/> for (byte B: Bt) {<br/> system. out. print ("B =" + B + "/t "); // B =-86 B = 0 B =-35b = 3 B = 0 B = 0 B = 85 B = 85 <br/>}< br/> Bos. close (); <br/>}< br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/>}

 

 

This code broke me for a long time and failed to get the returned value. It almost killed me. But now my life is saved. Haha. Finally, the correct return value is obtained. The returned result is: B =-86 B = 0 B =-35 B = 3 B = 0 B = 0 B = 85 B = 85.

To test whether the hexadecimal format of this digit is the same as that obtained by the serial port debugging tool, convert the hexadecimal format of the digit as follows:Int num1 =-86, num2 = 0, num3 =-35, num4 = 3, num5 = 85; <br/> system. out. println (num1 + "hexadecimal:" + integer. tohexstring (num1) + "/N" + <br/> num2 + "hexadecimal:" + integer. tohexstring (num2) + "/N" + <br/> num3 + "hexadecimal:" + integer. tohexstring (num3) + "/N" + <br/> num4 + "hexadecimal:" + integer. tohexstring (num4) + "/N" + <br/> num5 + "hexadecimal:" + integer. tohexstring (num5 ));

Result:

-86 hexadecimal: ffffffaa
Hexadecimal value of 0: 0
-Hexadecimal value of 35: ffffffdd
Hexadecimal format of 3: 3
Hexadecimal value of 85: 55

In order to get a representation with only two digits, you just need to judge it and we will not introduce it here.

 

PS:

1: No listening event was written in the previous program.

2: The previous program closed the SerialPort after obtaining the returned value. [I cannot figure out why it cannot be closed. I think it should be closed]

3: you cannot use the abbreviation to obtain the portid, that is, portid = commp ortidentifier. getportidentifier ("com3"). This is quite popular and cannot be abbreviated as nnd!

 

PS: I still like my code for two reasons: "simplified "! Reading online things is too much nonsense and depressing.

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.