定义:
SerialPort ComDevice = new SerialPort();
在开启串口前 设置前后文本转换的字符编码
代码:ComDevice.Encoding = System.Text.Encoding.GetEncoding("GB2312");//此行非常重要 可解决接收中文乱码问题
接收代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
#region 串口相关操作 SerialPort ComDevice = new
SerialPort();
private
void
GetComList()
{
//获取可用串口列表
string [] ports = SerialPort.GetPortNames();
foreach
( string
port in
ports)
{
cbbComList.Properties.Items.Add(port);
}
if
(cbbComList.Properties.Items.Count > 0)
{
cbbComList.SelectedIndex = 0;
cbbComList.Enabled = true ;
}
}
private
void
btnComOpen_Click( object
sender, EventArgs e)
{
if
(btnComOpen.Tag.ToString() == "0" )
{
ComDevice.PortName = cbbComList.SelectedItem.ToString();
ComDevice.BaudRate = 115200;
ComDevice.Parity = (Parity)0;
ComDevice.DataBits = 8;
ComDevice.StopBits = (StopBits)1;
ComDevice.Encoding = System.Text.Encoding.GetEncoding( "GB2312" ); //此行非常重要 可解决接收中文乱码问题
try
{
ComDevice.Open();
}
catch
(Exception ex)
{
MessageBox.Show(ex.Message, "错误" , MessageBoxButtons.OK, MessageBoxIcon.Error);
return ;
}
btnComOpen.Text = "关 闭" ;
btnComOpen.Tag = "1" ;
picComStatus.Image = Properties.Resources.green;
ComDevice.DataReceived += new
SerialDataReceivedEventHandler(ComDevice_DataReceived);
}
else
{
try
{
ComDevice.Close();
}
catch
(Exception ex)
{
MessageBox.Show(ex.Message, "错误" , MessageBoxButtons.OK, MessageBoxIcon.Error);
}
btnComOpen.Text = "打 开" ;
btnComOpen.Tag = "0" ;
picComStatus.Image = Properties.Resources.red;
}
}
/// <summary>
/// 发送数据 ---此代码在发送时都是转换成十六进制进行发送
/// </summary>
private
void
Send( string
cmd, bool
HexCmd)
{
if
(cmd == null )
return ;
if
(cmd.Length > 0)
{
if
(ComDevice.IsOpen == true )
{
byte [] SendBytes = null ;
string
SendData = cmd;
if
(HexCmd == true )
{ //16进制发送
try
{
//剔除所有空格
SendData = SendData.Replace( " " , "" );
if
(SendData.Length % 2 == 1)
{ //奇数个字符
SendData = SendData.Remove(SendData.Length - 1, 1); //去除末位字符
}
List< string > SendDataList = new
List< string >();
for
( int
i = 0; i < SendData.Length; i = i + 2)
{
SendDataList.Add(SendData.Substring(i, 2));
}
SendBytes = new
byte [SendDataList.Count];
for
( int
j = 0; j < SendBytes.Length; j++)
{
SendBytes[j] = ( byte )(Convert.ToInt32(SendDataList[j], 16));
}
}
catch
{
XtraMessageBox.Show( "请输入正确的16进制数据!" , "错误" , MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
System.Text.Encoding chs = System.Text.Encoding.GetEncoding( "gb2312" );
byte [] bytes = chs.GetBytes(cmd);
string
str = "" ;
for
( int
i = 0; i < bytes.Length; i++)
{
str += string .Format( "{0:X2}" , bytes[i]);
}
List< string > SendDataList = new
List< string >();
for
( int
i = 0; i < str.Length; i = i + 2)
{
SendDataList.Add(str.Substring(i, 2));
}
SendDataList.Add( "0D" );
SendBytes = new
byte [SendDataList.Count];
for
( int
j = 0; j < SendBytes.Length; j++)
{
SendBytes[j] = ( byte )(Convert.ToInt32(SendDataList[j], 16));
}
}
ComDevice.Write(SendBytes, 0, SendBytes.Length); //发送数据
}
else
{
XtraMessageBox.Show( "请打开串口!" , "错误" , MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
#region 接收数据
private
void
ComDevice_DataReceived( object
sender, SerialDataReceivedEventArgs e)
{
UpdateRecevie(ComDevice.ReadExisting());
}
public
delegate
void
UpdateString( object
NewData);
public
void
UpdateRecevie( object
NewData)
{
if
( this .InvokeRequired) //等待异步
{
UpdateString _myInvoke = new
UpdateString(UpdateRecevie);
this .Invoke(_myInvoke, new
object [] { NewData });
}
else
{
txtComReceive.AppendText(NewData.ToString());
txtComReceive.SelectionStart = txtComReceive.Text.Length - 1;
txtComReceive.ScrollToCaret();
}
}
#endregion
|