C#串口数据读取,显示不出数据,可以读到,新手求大神

白三儿 白三儿 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace WindowsFormsApplication8
{
public partial class Form1 : Form
{

//int i;
int[] str = new int[18];

public Form1()
{
InitializeComponent();
}
private void btnport_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
}
serialPort1.Open();

}

private void Form1_Load(object sender, EventArgs e)
{

foreach (string s in System.IO.Ports.SerialPort.GetPortNames())
{
comboport.Items.Add(s);
}
comboport.SelectedIndex = 3;
comboburden.SelectedIndex = 5;
combodate.SelectedIndex = 3;
combojiaoyan.SelectedIndex = 0;
combostop.SelectedIndex = 0;

serialPort1.BaudRate = 19200;
serialPort1.DataBits = 8;
serialPort1.StopBits = StopBits.One;
serialPort1.Parity = Parity.None;

}

private void btnsend_Click(object sender, EventArgs e)
{
serialPort1.WriteLine(textsend.Text); //这里有点小问题发送过去规定协议数据后不返回,只能先用串口调试助手返回
}



private void btnclose_Click(object sender, EventArgs e)
{
serialPort1.Close();
}

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{

byte[] read = new byte[36];
int count = serialPort1.Read(read, 0, read.Length);
for (int i = 0; i < read.Length; i++)
{
string str1 = null;
if (read[i] == 0xff)
{
str[0] = read[i];
str[1] = read[i + 1];
str[2] = read[i + 2];
str[3] = read[i + 3];
str[4] = read[i + 4];
str[5] = read[i + 5];
str[6] = read[i + 6];
str[7] = read[i + 7];
str[8] = read[i + 8];
str[9] = read[i + 9];
str[10] = read[i + 10];
str[11] = read[i + 11];
str[12] = read[i + 12];
str[13] = read[i + 13];
str[14] = read[i + 14];
str[15] = read[i + 15];
str[16] = read[i + 16];
str[17] = read[i + 17];
for (int h = 0; h < str.Length; h++)
{
str1 += str[h].ToString("X2")+" ";
}

richreceived.Text += str1.ToString()+"/r/n"; //显示运行到这里代码停滞,也不显示到接收的文本框,肯定出错了

}
}


}
catch (Exception)
{
string ca = "";
}


}



}
}
...全文

相关推荐

C#实现串口操作(包括读写串口,显示数据)

本文将详细讲解如何使用C#语言来实现串口操作,包括串口的打开、关闭、数据的读取与发送,以及如何将接收到的数据实时显示在屏幕上。首先,我们要了解串口的基础知识。

wghneu

C# SerialPort连接串口,读写串口数据

C# SerialPort连接串口,读写串口数据 一、起始 最近在公司做上位机,上位机要求是能够读取一款支持串口通信的传感器并且把传感器的数据用图表显示出来,且上位机能对传感器发送指令,需求有涉及到串口通信和绘制图表。 那么首先要解决的问题就是能够让电脑上的程序进行串口通信,有串口通信的编程语言有很多,C++,C#,JAVA,Python等语言都是支持串口通信的。我选择了使用C#,因为上位机主要是在Windows操作系统上运行,而C#对于winodws桌面应用的开发支持是比较方便的,功能也比较强大,尤其是W

Fred_1986

C# 中串口读取问题及解决方案

在调用 API 之前,需要定义相关的结构体和常量。[DllImporttrue[DllImporttrue在处理 C# 中的串口通信问题时,开发者需要综合考虑多个因素,包括注册表路径、驱动程序、权限设置、使用其他 API、串口助手工具、计算机重启、系统更新以及调试信息等。

趣享先生

c# 读取串口写入数据库

综上所述,"c# 读取串口写入数据库"的实现涉及C#的串口通信、SQLite数据库操作、SQL Server数据库操作以及多线程编程。

Mark_sssss

C#通过串口读取和发送数据

在IT行业中,串口通信是一种常见且重要的数据传输方式,特别是在嵌入式系统、工业设备以及部分物联网应用中。本文将详细讲解如何使用C#语言通过串口进行数据的读取和发送,这对于初学者来说是非常实用的知识点。

王招飞
评论 6
收藏 已收藏
分享
返回顶部
白三儿
C# 111,130 成员 642,541 内容