C # ---- Serial Port Data YSI instance

Source: Internet
Author: User

C # ---- Serial Port Data YSI instance
During the YSI instance process, the main problems are: type conversion problem, calculation Verification Code 1, different rules for different devices. The code for this instance is as follows: copy the code string serialNumber = sn; // Packet Command Payload int i1 = 0x11; // Packet Length Byte int i2 = 0xA5; // Packet Sync Byt [] string [] strarr = new string [32]; // command string strarr [0] = Convert. toString (i1, 16); // Convert fixed character strarr [1] = Convert. toString (i2, 16); // convert fixed character char [] arr = serialNumber. toCharArray (); // character array int [] ii = new int [16]; // integer array, in order to calculate the verification code ii [0] = I1; // convert fixed character ii [1] = i2; // convert fixed character for (int I = 0; I <arr. length; I ++) {strarr [2 + I] = Convert. toString (arr [I], 16); ii [2 + I] = (int) arr [I];} // calculate the check code int checksum = 0; for (int I = 0; I <ii. length; I ++) {checksum + = ii [I]; if (checksum> 256) checksum = checksum-256;} strarr [16] = Convert. toString (checksum, 16); copy the code above. First, use the forced type conversion char type to hexadecimal int, and then calculate the verification code; second, convert char to a hexadecimal string. In this way, the string array to be sent is obtained. 2. Send string data and receive data. The Code is as follows: copy the code // calculate the transmission string [] sendstr = Sendstr (sdstr ); // send byte array byte [] wbb = new byte [64]; // convert to hexadecimal byte for (int I = 0; I <sendstr. length; I ++) {wbb [I] = Convert. toByte (sendstr [I], 16);} // receives byte array byte [] rdb = new byte [64]; // opens a new serial port to connect to sp. open (); // discard the data sp from the accept buffer of the serial driver. discardInBuffer (); // discard the data sp from the transmission buffer of the serial driver. discardOutBuffer (); // write a specified number of bytes to the serial port sp. write (wbb, 0, wbb. Length); // The current thread is suspended for 500 ms System. threading. thread. sleep (500); // read data timeout sp. readTimeout = 1000; // read the received data sp. read (rdb, 0, rdb. length); copy the code above. First, it needs to be converted to bytes for sending. The device requires hexadecimal bytes. Second, after sending data, the thread can be suspended for 500 milliseconds to avoid data conflicts. In this way, the byte array rdb is returned. 3. The received byte array needs to be converted and parsed. The Code is as follows: // read records are converted to string [] rdstr = new string [100]; for (int I = 0; I <rdb. length; I ++) {rdstr [I] = rdb [I]. toString ("X2");} above, first converted to a hexadecimal string. 4. The final data to be collected is a floating point data class that is searched on the Internet for conversion between string and floating point types. The Code is as follows: copy the code class stConvertFloat {public static string FloatToStandard (double f, int style) {char [] Num = new char [32]; // 32 is the binary result plus the 1 Terminator if (f <0) // f is a negative number, and the symbol digit Num [0] is assigned 1 {Num [0] = '1 '; f = f * (-1); // convert to an integer} else Num [0] = '0'; // evaluate the exponent int exponent = 0; if (f> = 1) // The exponent is positive, that is, fabs (f) >=1 {while (f> = 2) {exponent ++; f = f/2 ;}} else // negative exponent {while (f <1) {exponent ++; f = f * 2;} exponent = (128-exponent) + 128; // (128-exponent) is the complement code, and + 128 is the index symbol position 1} exponent + = 127; // exponential conversion to the order code int I; for (I = 8; I> = 1; I --) // convert the exponent to the binary number {if (exponent % 2 = 0) num [I] = '0'; else Num [I] = '1'; exponent/= 2;} f = f-1; // convert decimals to the standard format // calculate the decimal part double s = 1; for (I = 9; I <32; I ++) {s/= 2; if (f> = s) {// MessageBox. show (Convert. toString (s); f = f-s; Num [I] = '1';} else Num [I] = '0';} if (style = 2) // binary output {string tt = new string (Num); return tt;} else // convert binary to hexadecimal {char sum; int j = 0; for (I = 0; I <32; I = I + 4, j ++) {sum = Convert. toChar (Num [I]-'0') * 8 + (Num [I + 1]-'0 ') * 4 + (Num [I + 2]-'0') * 2 + (Num [I + 3]-'0'); if (sum> 9) num [j] = Convert. toChar (sum-10) + 'A'); else Num [j] = Convert. toChar (sum + '0');} string tt = new string (Num); tt = tt. substring (0, 8); return tt;} public static double StandardToFloat (string FNum, int style) {char [] Num = new char [32]; char [] Hex = new char [8]; int I, j, value; if (style = 2) // binary {for (I = 0; I <32; I ++) Num [I] = FNum [I];} else // convert the hexadecimal format to binary {for (I = 0; I <8; I ++) {Hex [I] = FNum [I]; if (Hex [I]> = '0' & Hex [I] <= '9 ') value = Hex [I]-'0'; else {Hex [I] = Convert. toChar (Hex [I] | 0x20); // convert to lower case value = Hex [I]-'A' + 10;} for (j = 3; j> = 0; j --) {Num [I * 4 + j] = Convert. toChar (value % 2) + '0'); value/= 2 ;}} double f = 1; // f is the final floating point number result, the standard floating-point format implies the 1 double s = 1 before the decimal point; // s is the right value represented by the decimal places for (I = 9; I <32; I ++) // convert the fractional part {s/= 2; if (Num [I] = '1') {f + = s; // MessageBox. show (Convert. toString (s) ;}} int exponent = 0; // index part int d = 1; // d represents the weight of each of the index parts for (I = 8; i> = 1; I --) // calculate the order code {if (Num [I] = '1') exponent + = d; d * = 2 ;} if (exponent> = 127) exponent-= 127; // convert the order code to the index else {exponent + = 256; // The highest exponent-= 127 for the Population Index to change the order code overflow; // The order code to change the index to the complement code exponent = 128-(exponent-128 ); // (exponent-128) removes the sign bit. In the lecture, the complement code is changed to the absolute value of the index. exponent = exponent * (-1 ); // final index part} if (Num [0] = '1') // The Floating Point Symbol bit is 1, indicating that it is a negative number f = f * (-1 ); f = f * Math. pow (2, exponent); // combine the fractional part and floating point part return f ;}}

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.