|
。.PNG (23.06 KB, 下載次數: 91)
下載附件
界面
2020-6-17 19:11 上傳
界面設計:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<GroupBox x:Name="groupBox" Header="串口設置" FontSize="18" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="23,33,0,0" Height="261" Grid.RowSpan="3" Width="241" />
<Label Content="串口" FontSize="18" VerticalAlignment="Center" Width="100" HorizontalAlignment="Center" HorizontalContentAlignment="Right" Margin="23,0.5,137,71" Grid.Row="1" Height="36"/>
<Label Content="波特率" FontSize="18" VerticalAlignment="Center" HorizontalAlignment="Center" Width="100" Grid.Row="1" HorizontalContentAlignment="Right" Margin="23,55,135,10" Height="42"></Label>
<ComboBox x:Name="cmbChuanKou" Width="100" Height="30" Margin="148,6,10,71" Grid.Row="1"/>
<ComboBox x:Name="cmbBaud" Width="100" Height="30" Margin="148,55,10,22" Grid.Row="1"/>
<Button x:Name="btnOpenSerialPort" FontSize="18" Grid.Row="2" Height="30" Width="100" Content="打開串口" Margin="36,27,122,49" Click="btnOpenSerialPort_Click"></Button>
<Button x:Name="btnCloseSerialPort" FontSize="18" Grid.Row="2" Height="30" Width="100" Content="關閉串口" Margin="148,27,10,49" Click="btnCloseSerialPort_Click"></Button>
<GroupBox x:Name="groupBox1" Header="控制LED" FontSize="18" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="9.5,33,0,0" Height="261" Grid.RowSpan="3" Width="255" Grid.Column="1" />
<Label Content="請選擇LED" FontSize="18" VerticalAlignment="Center" HorizontalAlignment="Center" RenderTransformOrigin="0.603,0.654" Grid.Column="1" Margin="10,38,142,22" Grid.Row="1" Height="47" Width="107"></Label>
<ComboBox x:Name="cmbLed" Grid.Column="1" Height="30" Width="100" Margin="149,38,10,39" Grid.Row="1" ></ComboBox>
<Button x:Name="btnOpenLed" FontSize="18" Grid.Row="2" Height="30" Width="100" Content="開燈" Grid.Column="1" Margin="17,27,142,49" Click="btnOpenLed_Click"></Button>
<Button x:Name="btnCloseLed" FontSize="18" Grid.Row="2" Height="30" Width="100" Content="關燈" Grid.Column="1" Margin="149,27,10,49" Click="btnCloseLed_Click"></Button>
</Grid>
</Window>
后臺代碼:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
foreach (string s in SerialPort.GetPortNames())
{
cmbChuanKou.Items.Add(s);
}
List<string> Baud = new List<string>(new string[] { "600", "1200", "2400", "4800", "9600", "14400", "19200", "115200" });
cmbBaud.ItemsSource = Baud;
cmbLed.Items.Add("全部");
for (int i = 1; i < 17; i++)
{
cmbLed.Items.Add(string.Format("{0}號燈", i));
}
cmbChuanKou.SelectedIndex = 0;
cmbLed.SelectedIndex = 0;
cmbBaud.SelectedIndex = 0;
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
}
private void btnOpenSerialPort_Click(object sender, RoutedEventArgs e)
{
mySerialPort.PortName = cmbChuanKou.Text;
mySerialPort.BaudRate = Convert.ToInt32(cmbBaud.Text);
try
{
mySerialPort.Open();
btnOpenSerialPort.IsEnabled = false;
btnCloseSerialPort.IsEnabled = true;
cmbChuanKou.IsEnabled = false;
cmbBaud.IsEnabled = false;
}
catch
{
MessageBox.Show("打開端口出錯,請檢查串口", "錯誤");
}
}
private void btnCloseSerialPort_Click(object sender, RoutedEventArgs e)
{
try
{
mySerialPort.Close();
btnOpenSerialPort.IsEnabled = false;
btnCloseSerialPort.IsEnabled = true;
cmbChuanKou.IsEnabled = true;
cmbBaud.IsEnabled = true;
}
catch
{
}
}
private byte sum(byte[] d)
{
byte s = 0;
for (int i = 0; i < d.Length - 1; i++)
{
s += d[ i];[ i]
}
return s;
}
private void btnOpenLed_Click(object sender, RoutedEventArgs e)
{
byte[] data = new byte[5];
data[0] = 0x55;
data[1] = 0xaa;
data[2] = (byte)cmbLed.SelectedIndex;
data[3] = 0x00;
data[4] = sum(data);
mySerialPort.Write(data, 0, 5);
}
private void btnCloseLed_Click(object sender, RoutedEventArgs e)
{
byte[] data = new byte[5];
data[0] = 0x55;
data[1] = 0xaa;
data[2] = (byte)cmbLed.SelectedIndex;
data[3] = 0xFF;
data[4] = sum(data);
mySerialPort.Write(data, 0, 5);
}
}
}
|
-
51hei.png
(5.67 KB, 下載次數: 144)
下載附件
2020-6-17 22:17 上傳
-
-
聯合調試-LED.zip
2020-6-17 19:03 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
73.18 KB, 下載次數: 42, 下載積分: 黑幣 -5
評分
-
查看全部評分
|