C# 2, 10, 16 and 16 and their ASCII codes --------//Conversion of various character types in C# for serial port communication------------
Understanding of character length string s = "I am a 2 soldier"; int len = s.Length; 6 characters byte[] sarr = System.Text.Encoding.Default.GetBytes(s); len = sarr. Length; 11 bytes
It makes no sense to convert decimal to string, and it must be converted to the corresponding ASCII code int t1 = 81; string s1 = t1. ToString(); This decimal conversion corresponds to ASCII characters to make sense. s1 = ((char)t1). ToString();
Hexadecimal to string: This doesn't make sense. It's tostring. int intAB = 0x16; s1 = intAB.ToString(); Hexadecimal to ASCII characters: byte babb = 0x45; string ass = ((char)babb). ToString();
ASCII strings to decimal numbers string tr = "2Ab Liu"; string d =""; for (int i = 0; i < tr. Length; i++) { int ii = (int)Convert.ToChar(tr. Substring(i, 1)); d = d +" "+ ii.ToString(); }
ASCII string to hexadecimal number string s2 = "2Ab Liu"; byte[] ba = System.Text.ASCIIEncoding.Default.GetBytes(s2); StringBuilder sb = new StringBuilder(); foreach (byte b in ba) { sb. Append(b.ToString("x") + " "); }
Hexadecimal to decimal int intA = 0x16; When defining, it must be 0x string strA = "16"; Strings can be without int intA1 = Convert.ToInt32(intA); int intA2 = Convert.ToInt32(strA, 16);
10 to 16 strA = Convert.ToString(intA2, 16);
Decimal to binary, hexadecimal is similar int int10 = 10; string str2 = Convert.ToString(int10,2);
Binary to decimal int10 = Convert.ToInt32(str2,2);
//-------------------------------------------------------------------------------
How does c# serialport receive a set of hexadecimal numbers How does C# SerialPort receive a set of hexadecimal numbers, and how should the program be written? Best answer System.IO.Ports.SerialPort sp = new System.IO.Ports.SerialPort("COM1"); int buffersize = 6; The size of the hexadecimal number (let's say 6Byte) Byte[] buffer = new Byte[buffersize]; Create a buffer sp. Read(buffer, 0, buffersize); Read from COM1
This set of hexadecimal numbers is processed
C# serial port sends data Bounty points: 5 - Resolution time: 2009-10-19 21:15 I want to send a hex command to the serial port, the command string is: "00 00 00 1B 54 59 55 54 00 00 00 00 00 00 00 00 00 00 00 FF E1 00 05 00 0D 0A" How can I convert the above string numerically so that the computer serial port knows that the string I sent is hexadecimal? Question Supplement: Who has such a sample code, please help me, I am a hardware manufacturer, not familiar with software, I hope the master can give a detailed code, thank you! Questioner: daishuanqing - Intern Level 1 Best answer Add a reference: Microsoft.VisualBasic (is C# code, but it needs to be referenced.) )
Traverse the SerialPortNames with the following to get all the serial ports Microsoft.VisualBasic.Devices.Computer pc = new Microsoft.VisualBasic.Devices.Computer(); foreach (string s in pc. Ports.SerialPortNames)
{ this.comboBox1.Items.Add(s);
}
Then the serial port name comName displayed is used to obtain the operation object. You can open this serial port:
System.IO.Ports.SerialPort com = new System.IO.Ports.SerialPort(comName); com. Open();
Data can then be written to the serial port
Sending data to serial port: com. WriteLine(this.textBox1.Text); byte arrays can also be written com. Write(bArray,0,5); It refers to the 5 bytes starting from 0 that write the byte array bArray
Data can also be accepted: com. ReadLine() It is also possible to read bytes com. ReadByte();
Finally, it is closed: com. Close();
Also, convert the hexadecimal string to byte byte b = Convert.ToByte("1A",16); C# serial port sends data Bounty points: 5 - Resolution time: 2009-10-19 21:15 I want to send a hex command to the serial port, the command string is: "00 00 00 1B 54 59 55 54 00 00 00 00 00 00 00 00 00 00 00 FF E1 00 05 00 0D 0A" How can I convert the above string numerically so that the computer serial port knows that the string I sent is hexadecimal? Question Supplement: Who has such a sample code, please help me, I am a hardware manufacturer, not familiar with software, I hope the master can give a detailed code, thank you! Questioner: daishuanqing - Intern Level 1 Best answer Add a reference: Microsoft.VisualBasic (is C# code, but it needs to be referenced.) )
Traverse the SerialPortNames with the following to get all the serial ports Microsoft.VisualBasic.Devices.Computer pc = new Microsoft.VisualBasic.Devices.Computer(); foreach (string s in pc. Ports.SerialPortNames)
{ this.comboBox1.Items.Add(s);
}
Then the serial port name comName displayed is used to obtain the operation object. You can open this serial port:
System.IO.Ports.SerialPort com = new System.IO.Ports.SerialPort(comName); com. Open();
Data can then be written to the serial port
Sending data to serial port: com. WriteLine(this.textBox1.Text); byte arrays can also be written com. Write(bArray,0,5); It refers to the 5 bytes starting from 0 that write the byte array bArray
Data can also be accepted: com. ReadLine() It is also possible to read bytes com. ReadByte();
Finally, it is closed: com. Close();
Also, convert the hexadecimal string to byte byte b = Convert.ToByte("1A",16); |