Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Serial Port debugging tool in C #

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

< len; i++) { crc = (ushort)(crc ^ (data[i])); for (int j = 0; j < 8; j++) { crc = (crc & 1) != 0 ? (ushort)((crc >

> 1) ^ 0xA001): (ushort) (crc > > 1);}} byte hi = (byte) ((crc & 0xFF00) > > 8); / / High position byte lo = (byte) (crc & 0x00FF); / / low position return new byte [] {hi, lo} } return new byte [] {0,0};} # endregion # region ToCRC16 public static string ToCRC16 (string content) {return ToCRC16 (content, Encoding.UTF8);} public static string ToCRC16 (string content, bool isReverse) {return ToCRC16 (content, Encoding.UTF8, isReverse) } public static string ToCRC16 (string content, Encoding encoding) {return ByteToString (CRC16 (encoding.GetBytes (content)), true);} public static string ToCRC16 (string content, Encoding encoding, bool isReverse) {return ByteToString (CRC16 (encoding.GetBytes (content)), isReverse) } public static string ToCRC16 (byte [] data) {return ByteToString (CRC16 (data), true);} public static string ToCRC16 (byte [] data, bool isReverse) {return ByteToString (CRC16 (data), isReverse) } # endregion # region ToModbusCRC16 public static string ToModbusCRC16 (string s) {return ToModbusCRC16 (s, true);} public static string ToModbusCRC16 (string s, bool isReverse) {return ByteToString (CRC16 (StringToHexByte (s)), isReverse);} public static string ToModbusCRC16 (byte [] data) {return ToModbusCRC16 (data, true) } public static string ToModbusCRC16 (byte [] data, bool isReverse) {return ByteToString (CRC16 (data), isReverse);} # endregion # region ByteToString public static string ByteToString (byte [] arr, bool isReverse) {try {byte hi = arr [0], lo = arr [1] Return Convert.ToString (isReverse? Hi + lo * 0x100: hi * 0x100 + lo, 16). ToUpper (). PadLeft (4,'0');} catch (Exception ex) {throw (ex);}} public static string ByteToString (byte [] arr) {try {return ByteToString (arr, true) } catch (Exception ex) {throw (ex);}} # endregion # region StringToHexString public static string StringToHexString (string str) {StringBuilder s = new StringBuilder (); foreach (short c in str.ToCharArray ()) {s.Append (c.ToString ("X4")) } return s.ToString ();} # endregion # region StringToHexByte private static string ConvertChinese (string str) {StringBuilder s = new StringBuilder () Foreach (short c in str.ToCharArray ()) {if (c = 127) {s.Append (c.ToString ("X4"));} else {s.Append ((char) c) }} return s.ToString ();} private static string FilterChinese (string str) {StringBuilder s = new StringBuilder () Foreach (short c in str.ToCharArray ()) {if (c > 0 & & c < 127) {s.Append ((char) c);}} return s.ToString () } / string to hexadecimal character array / public static byte [] StringToHexByte (string str) {return StringToHexByte (str, false) } / string to hexadecimal character array / public static byte [] StringToHexByte (string str, bool isFilterChinese) {string hex = isFilterChinese? FilterChinese (str): ConvertChinese (str); / / clear all spaces hex = hex.Replace ("", "); / / if the number of characters is odd, add 0 hex + = hex.Length% 2! = 0?" 0 ":"; byte [] result = new byte [hex.Length / 2] For (int I = 0, c = result.Length; I < c; iTunes +) {result [I] = Convert.ToByte (hex.Substring (I * 2,2), 16);} return result;} # endregion} WPF technical points:

1. Custom style button

two。 The converter is used to bind buttons

Public class FontConverters: IValueConverter {public object Convert (object value, Type targetType, object parameter, CultureInfo culture) {if (value! = null & & bool.TryParse (value.ToString (), out bool result)) {if (result) {return "close serial port" }} return "open serial port";} public object ConvertBack (object value, Type targetType, object parameter, CultureInfo culture) {throw new NotImplementedException () }} / / the color state used to bind UI shows public class ColorConverters: IValueConverter {public object Convert (object value, Type targetType, object parameter, CultureInfo culture) {if (value! = null & & bool.TryParse (value.ToString ()) Out bool result) {if (result) {return new SolidColorBrush ((Color) System.Windows.Media.ColorConverter.ConvertFromString ("# 2E8B57")) } return new SolidColorBrush ((Color) System.Windows.Media.ColorConverter.ConvertFromString ("# FF6347"));} public object ConvertBack (object value, Type targetType, object parameter, CultureInfo culture) {throw new NotImplementedException ();}}

3. Reference font

4. Bind commands and elements

Write at the end

The structure diagram of the main project is as follows:

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report