-
大小: 48KB文件類(lèi)型: .rar金幣: 2下載: 1 次發(fā)布日期: 2021-05-20
- 語(yǔ)言: C#
- 標(biāo)簽: C#??腳踏開(kāi)關(guān)??輸入信號(hào)??
資源簡(jiǎn)介
在上位機(jī)開(kāi)發(fā)中,時(shí)常會(huì)引入輸入信號(hào)作為啟動(dòng)信號(hào),此Demo完美解決使用腳踏開(kāi)關(guān)作輸入信號(hào)的問(wèn)題

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.IO.Ports;
using?System.Linq;
using?System.Text;
using?System.Threading.Tasks;
using?System.Windows.Forms;
namespace?RTS
{
????public?partial?class?Form1?:?Form
????{
????????//將ComDevice.RtsEnable置為T(mén)rue,表示啟用發(fā)送請(qǐng)求(RST)信號(hào),將請(qǐng)求發(fā)送/可以發(fā)送?(RTS/CTS)[第7和第8針腳]硬件握手時(shí)[腳踏短接],會(huì)觸發(fā)ComDevice.PinChanged事件及ComDevice.CtsHolding的Bool值變化。
????????public?Form1()
????????{
????????????InitializeComponent();
????????}
????????SerialPort?sp?=?new?SerialPort();
????????
????????private?void?Form1_Load(object?sender?EventArgs?e)
????????{
????????????sp.RtsEnable=true;
????????????cmb_JtCom.Items.AddRange(SerialPort.GetPortNames());
????????????//sp.DataReceived?+=?new?SerialDataReceivedEventHandler(sp_DataReceived)?;
????????????sp.PinChanged?+=?new?SerialPinChangedEventHandler(sp_DataReceived);
????????}
????????private?void?btn_Open_Click(object?sender?EventArgs?e)
????????{
????????????if(cmb_JtCom.Items.Count<=0)
????????????{
????????????????MessageBox.Show(“沒(méi)有發(fā)現(xiàn)串口,請(qǐng)重試“);
????????????????return;
????????????????
????????????}
????????????if?(sp.IsOpen==false)
????????????{
????????????????sp.PortName?=?cmb_JtCom.SelectedItem.ToString();
????????????????sp.BaudRate?=?9600;
????????????????sp.DataBits?=?8;
????????????????sp.StopBits?=?StopBits.One;
????????????????sp.Parity?=?Parity.None;
????????????????try
????????????????{
????????????????????sp.Open();
????????????????????timer1.Enabled?=?true;
????????????????????
????????????????}
????????????????catch(Exception?ex)
????????????????{
????????????????????MessageBox.Show(ex.Message?“錯(cuò)誤“?MessageBoxButtons.OK?MessageBoxIcon.Error);
????????????????}
????????????????btn_Open.Text?=?“關(guān)閉串口“;
????????????}
????????????else
????????????{
????????????????try
????????????????{
????????????????????sp.Close();
????????????????????timer1.Enabled?=?false;
????????????????}
????????????????catch?(Exception?ex)
????????????????{
????????????????????MessageBox.Show(ex.Message?“錯(cuò)誤“?MessageBoxButtons.OK?MessageBoxIcon.Error);
????????????????}
????????????????btn_Open.Text?=?“打開(kāi)串口“;
????????????}
????????????cmb_JtCom.Enabled?=?!sp.IsOpen;
????????}
????????bool?b;
????????void?sp_DataReceived(object?sender?SerialPinChangedEventArgs?e)
????????{
????????????MessageBox.Show(“RTS觸發(fā)成功“);
??????????
????????}
????????private?void?timer1_Tick(object?sender?EventArgs?e)
????????{
????????????label1.Text?=?sp.CtsHolding.ToString();
????????????
????????}
????????
????}
}
?屬性????????????大小?????日期????時(shí)間???名稱(chēng)
-----------?---------??----------?-----??----
?????文件????????187??2018-03-15?10:40??RTS\RTS\App.config
?????文件??????10240??2018-03-20?17:12??RTS\RTS\bin\Debug\RTS.exe
?????文件????????187??2018-03-15?10:40??RTS\RTS\bin\Debug\RTS.exe.config
?????文件??????24064??2018-03-20?17:12??RTS\RTS\bin\Debug\RTS.pdb
?????文件??????24224??2018-03-20?17:11??RTS\RTS\bin\Debug\RTS.vshost.exe
?????文件????????187??2018-03-15?10:40??RTS\RTS\bin\Debug\RTS.vshost.exe.config
?????文件????????490??2013-03-18?17:00??RTS\RTS\bin\Debug\RTS.vshost.exe.manifest
?????文件???????2824??2018-03-20?17:12??RTS\RTS\Form1.cs
?????文件???????3503??2018-03-15?13:25??RTS\RTS\Form1.Designer.cs
?????文件???????6011??2018-03-15?13:25??RTS\RTS\Form1.resx
?????文件????????865??2018-03-15?10:40??RTS\RTS\obj\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件???????6959??2018-03-15?10:44??RTS\RTS\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件????????627??2018-03-20?17:11??RTS\RTS\obj\Debug\RTS.csproj.FileListAbsolute.txt
?????文件????????977??2018-03-15?13:25??RTS\RTS\obj\Debug\RTS.csproj.GenerateResource.Cache
?????文件???????2211??2018-03-15?11:12??RTS\RTS\obj\Debug\RTS.csprojResolveAssemblyReference.cache
?????文件??????10240??2018-03-20?17:12??RTS\RTS\obj\Debug\RTS.exe
?????文件????????180??2018-03-15?13:25??RTS\RTS\obj\Debug\RTS.Form1.resources
?????文件??????24064??2018-03-20?17:12??RTS\RTS\obj\Debug\RTS.pdb
?????文件????????180??2018-03-15?11:12??RTS\RTS\obj\Debug\RTS.Properties.Resources.resources
?????文件??????????0??2018-03-15?10:40??RTS\RTS\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
?????文件??????????0??2018-03-15?10:40??RTS\RTS\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
?????文件??????????0??2018-03-15?10:40??RTS\RTS\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
?????文件????????515??2018-03-15?10:40??RTS\RTS\Program.cs
?????文件???????1348??2018-03-15?10:40??RTS\RTS\Properties\AssemblyInfo.cs
?????文件???????2858??2018-03-15?10:40??RTS\RTS\Properties\Resources.Designer.cs
?????文件???????5612??2018-03-15?10:40??RTS\RTS\Properties\Resources.resx
?????文件???????1090??2018-03-15?10:40??RTS\RTS\Properties\Settings.Designer.cs
?????文件????????249??2018-03-15?10:40??RTS\RTS\Properties\Settings.settings
?????文件???????3786??2018-03-15?11:12??RTS\RTS\RTS.csproj
?????文件????????978??2018-03-15?10:40??RTS\RTS.sln
............此處省略12個(gè)文件信息
- 上一篇:C#多線程進(jìn)度條
- 下一篇:采集卡 C# Demo
評(píng)論
共有 條評(píng)論