1. 串口显示汉字的程序设计
using System ;
using System. Text ;
using System. Windows. Forms ;
namespace 汉字显示
{
public partial class Form1 : Form
{
public Form1 ( )
{
InitializeComponent ( ) ;
}
private byte [ ] StringToBytes ( string TheString)
{
Encoding FromEncoding = Encoding. GetEncoding ( "UTF-8" ) ;
Encoding ToEncoding = Encoding. GetEncoding ( "gb2312" ) ;
byte [ ] FromBytes = FromEncoding. GetBytes ( TheString) ;
byte [ ] ToBytes = Encoding. Convert ( FromEncoding, ToEncoding, FromBytes) ;
return ToBytes;
}
private string BytesToString ( byte [ ] Bytes)
{
string MyString;
Encoding FromEncoding = Encoding. GetEncoding ( "gb2312" ) ;
Encoding ToEncoding = Encoding. GetEncoding ( "UTF-8" ) ;
byte [ ] ToBytes = Encoding. Convert ( FromEncoding, ToEncoding, Bytes) ;
MyString = ToEncoding. GetString ( ToBytes) ;
return MyString;
}
private void button1_Click ( object sender, EventArgs e)
{
byte [ ] StringsToByte = StringToBytes ( textBox1. Text) ;
textBox2. Text = "" ;
foreach ( byte MyByte in StringsToByte)
{
string str = MyByte. ToString ( "x" ) . ToUpper ( ) ;
textBox2. Text += "0x" + ( str. Length == 1 ? "0" + str : str) + " " ;
}
}
private void button2_Click ( object sender, EventArgs e)
{
byte [ ] data = new byte [ textBox3. Text. Length / 2 ] ;
int i;
try
{
string buffer = textBox3. Text;
buffer = buffer. Replace ( "0x" , string . Empty) ;
buffer = buffer. Replace ( " " , string . Empty) ;
for ( i = 0 ; i < buffer. Length / 2 ; i++ )
{
data[ i] = Convert. ToByte ( buffer. Substring ( i * 2 , 2 ) , 16 ) ;
}
textBox4. Text = BytesToString ( data) ;
}
catch
{
MessageBox. Show ( "数据转换错误,请输入数字。" , "错误" ) ;
}
}
}
}
2. 多路ADC数据的波形显示
using System ;
using System. Drawing ;
using System. IO. Ports ;
using System. Windows. Forms ;
namespace Adc_Display
{
public partial class Form1 : Form
{
public Form1 ( )
{
InitializeComponent ( ) ;
serialPort1. DataReceived += new System. IO. Ports. SerialDataReceivedEventHandler ( PortDataReceivedEvent) ;
System. Windows. Forms. Control. CheckForIllegalCrossThreadCalls = false ;
}
ProgressBar[ ] GetProgressBar ( )
{
return new ProgressBar[ ] { progressBar1, progressBar2, progressBar3, progressBar4, progressBar5,
progressBar6, progressBar7, progressBar8, progressBar9, progressBar10
} ;
}
private void PortDataReceivedEvent ( object sender, SerialDataReceivedEventArgs args)
{
ProgressBar[ ] MyProgressBar = GetProgressBar ( ) ;
byte [ ] Data = new byte [ serialPort1. BytesToRead] ;
serialPort1. Read ( Data, 0 , Data. Length) ;
foreach ( byte MyData in Data)
{
for ( int i = 1 ; i < 10 ; i++ )
{
MyProgressBar[ 10 - i] . Value = MyProgressBar[ 10 - i - 1 ] . Value;
}
progressBar1. Value = ( int ) MyData;
string str = MyData. ToString ( ) ;
textBox1. Text = "0x" + ( str. Length == 1 ? "0" + str : str) ;
}
}
private void button1_Click ( object sender, EventArgs e)
{
if ( serialPort1. IsOpen)
{
groupBox2. Visible = false ;
this . Size = new Size ( 550 , 130 ) ;
serialPort1. Close ( ) ;
ProgressBar[ ] MyProgressBar = GetProgressBar ( ) ;
foreach ( ProgressBar theBar in MyProgressBar)
{
theBar. Value = 0 ;
}
button1. Text = "打开串口" ;
}
else
{
try
{
serialPort1. PortName = comboBox1. Text;
serialPort1. Open ( ) ;
groupBox2. Visible = true ;
this . Size = new Size ( 550 , 470 ) ;
button1. Text = "关闭串口" ;
}
catch
{
MessageBox. Show ( "串口打开错误" , "错误" ) ;
}
}
}
private void Form1_Load ( object sender, EventArgs e)
{
this . Size = new Size ( 550 , 130 ) ;
string Buffer;
comboBox1. Items. Clear ( ) ;
for ( int i = 1 ; i < 20 ; i++ )
{
try
{
Buffer = "COM" + i. ToString ( ) ;
serialPort1. PortName = Buffer;
serialPort1. Open ( ) ;
comboBox1. Items. Add ( Buffer) ;
serialPort1. Close ( ) ;
}
catch
{
}
}
comboBox1. SelectedIndex = 0 ;
}
}
}