91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 0.03M
    文件類型: .7z
    金幣: 1
    下載: 0 次
    發(fā)布日期: 2024-05-03
  • 語言: C#
  • 標(biāo)簽:

資源簡介

 串口讀取電子秤的重量

資源截圖

代碼片段和文件信息

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;
using?System.Threading.Tasks;
using?System.Windows.Forms;

namespace?SerialPortDemo
{
????public?partial?class?Form1?:?Form
????{
????????private?SerialPort?com;
????????public?Form1()
????????{
????????????InitializeComponent();
????????}

????????private?void?GetAutoCom()
????????{
????????????//獲取本地配置文件(注冊表文件)????????
????????????Microsoft.Win32.RegistryKey?hklm?=?Microsoft.Win32.Registry.LocalMachine;
????????????//讀取HARDWARE節(jié)點(diǎn)的鍵值
????????????Microsoft.Win32.RegistryKey?software11?=?hklm.OpenSubKey(“HARDWARE“);
????????????//打開“HARDWARE“子鍵
????????????Microsoft.Win32.RegistryKey?software?=?software11.OpenSubKey(“DEVICEMAP“);
????????????Microsoft.Win32.RegistryKey?sitekey?=?software.OpenSubKey(“SERIALCOMM“);
????????????//獲取當(dāng)前子鍵
????????????string[]?Str2?=?sitekey.GetValueNames();
????????????//獲得當(dāng)前子鍵存在的鍵值
????????????int?i;
????????????for?(i?=?0;?i?????????????{
????????????????cmbCOMPort.Items.Add(sitekey.GetValue(Str2[i]));
????????????}
????????}

????????private?void?Form1_Load(object?sender?EventArgs?e)
????????{
????????????GetAutoCom();
????????????com?=?new?SerialPort(cmbCOMPort.Text);?//實(shí)例化SerialPort并設(shè)置COM口
????????????com.BaudRate?=?9600;//波特率
????????????com.Parity?=?Parity.None;//無奇偶校驗(yàn)位
????????????com.StopBits?=?StopBits.Two;//兩個(gè)停止位
????????????com.Handshake?=?Handshake.RequestToSend;//控制協(xié)議
????????????com.ReceivedBytesThreshold?=?13;//設(shè)置?DataReceived?事件發(fā)生前內(nèi)部輸入緩沖區(qū)中的字節(jié)數(shù)我這里是13字節(jié)為一組
????????????com.Open();?????????????????//打開串口??
????????????com.DataReceived?+=?new?SerialDataReceivedEventHandler(Com_DataReceived);
????????}

????????///?
????????///?監(jiān)聽串口數(shù)據(jù)線程
????????///?

????????///?
????????///?

????????private?void?Com_DataReceived(object?sender?SerialDataReceivedEventArgs?e)

????????{
????????????Thread.Sleep(500);//線程休眠500毫秒,方便接收串口的全部數(shù)據(jù)
????????????try
????????????{
????????????????if?(com.IsOpen)
????????????????{
????????????????????byte[]?readBuffer?=?new?byte[com.ReadBufferSize?+?1];
????????????????????try
????????????????????{
????????????????????????int?count?=?com.Read(readBuffer?0?com.ReadBufferSize);????????//讀取串口數(shù)據(jù)(監(jiān)聽)
????????????????????????String?SerialIn?=?System.Text.Encoding.ASCII.GetString(readBuffer?0?count);//將字節(jié)數(shù)組解碼為字符串
????????????????????????if?(count?!=?0)
????????????????????????{
????????????????????????????//這里強(qiáng)調(diào)一下線程里不可以直接對UI進(jìn)行賦值,只能使用委托操作控件
????????????????????????????this.BeginInvoke(new?System.Threading.ThreadStart(delegate?()
????????????????????????????{
????????????????????????????????richTextBox1.Text?=?SerialIn;
????????????????????????????}));
????????????????????????}
????????????????????}
????????????????????

評論

共有 條評論

相關(guān)資源