C#.NET??????????????+???????
???????????? ???????[ 2013/10/23 10:45:45 ] ????????
private void button1USARTstart_Click(object sender?? EventArgs e)
{
if(USARTstate)//??????
{
button1USARTstart.Text = "??";
serialPort1.PortName = comboBox1PortName.Text;
serialPort1.BaudRate = Convert.ToInt32(comboBox2BaudRate.Text);
try
{
serialPort1.Open();
USARTstate = false;//??????
comboBox1PortName.Enabled = false;
comboBox2BaudRate.Enabled = false;
timer1.Enabled = true;
}
catch (System.Exception ex)
{
MessageBox.Show("??????????
:"+ex.Message);
}
}
else
{
button1USARTstart.Text = "???";
try
{
serialPort1.Close();
USARTstate = true;//???????
comboBox1PortName.Enabled = true;
comboBox2BaudRate.Enabled = true;
timer1.Enabled = false;
}
catch (System.Exception ex)
{
MessageBox.Show("??????????
:"+ex.Message);
}
}
}
//public static string ByteArrayToHexString(byte[] data)//?????????????????????
//{
// StringBuilder sb = new StringBuilder(data.Length * 3);
// foreach (byte b in data)
// sb.Append(Convert.ToString(b?? 16).PadLeft(2?? '0').PadRight(3?? ' '));
// return sb.ToString().ToUpper();
//}
public static string ByteArrayToString(byte[] data)//?????????????????????
{
StringBuilder sb = new StringBuilder(data.Length * 3);
foreach (byte b in data)
sb.Append(Convert.ToChar(b));
//return sb.ToString().ToUpper();
return sb.ToString();
}
public static byte[] HexStrToHexByte(string hexStr)
{
hexStr = hexStr.Replace(" "?? "");//?????????????е???
if (hexStr.Length % 2 != 0) hexStr += " ";
byte[] returnBytes = new byte[hexStr.Length / 2];//??????byte???飬???16????byte
for (int i = 0; i < returnBytes.Length; i++)
{ returnBytes[i] = Convert.ToByte(hexStr.Substring(i * 2?? 2).Trim()?? 16); }
return returnBytes;
}
private void button2SendData_Click(object sender?? EventArgs e)//????????
{
if (serialPort1.IsOpen)
{
if (checkBox.Checked)//send hex
{
//byte[] data = Encoding.ASCII.GetBytes(textBox1Tx.Text);
byte[] data = HexStrToHexByte(textBox1Tx.Text);
//string str = ByteArrayToHexString(data);
serialPort1.Write(data?? 0?? data.Length);
//serialPort1.Write(str);
TxNum += data.Length;
}
else
{
string str = textBox1Tx.Text.Trim();// ???
serialPort1.Write(str);
TxNum += str.Length;
}
labelTxNum.Text = "TX:" + TxNum.ToString();
}
else
return;
}
private void timer1_Tick(object sender?? EventArgs e)//??????????????????????????????????????
{
if (serialPort1.IsOpen == false) return;
int receiveNumber = serialPort1.BytesToRead;
if (receiveNumber == 0) return;
string str = null;
byte[] bs = new byte[receiveNumber];
serialPort1.Read(bs?? 0?? receiveNumber);
if (checkBox.Checked)
{
foreach (byte b in bs)
str += b.ToString("x2") + " ";
}
else
{
str = ByteArrayToString(bs);
}
textBox2Rx.AppendText(str);
RxNum += receiveNumber;
labelRxNum.Text = "RX:" + RxNum.ToString();
}
private void buttonClear_Click(object sender?? EventArgs e)// ????????textbox?е????
{
textBox2Rx.Clear();
TxNum = 0;
RxNum = 0;
labelTxNum.Text = "TX:" + TxNum.ToString();
labelRxNum.Text = "RX:" + RxNum.ToString();
}
}
}
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11