需要使用C#向微控制器发送字节 [英] Need to send bytes to microcontroller using C#

查看:52
本文介绍了需要使用C#向微控制器发送字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello good people。

我想将数据发送到控制器,然后它会打开发光二极管。

IM迷茫如何发送字节,因为我得到这个命令发送字符串格式的数据,而不是字节:

 serialPort1.Write(1); 



也许我错了

任何帮助,谢谢。

下面的代码。



< b>我尝试过:



使用System; 
使用System.Collections.Generic;使用System.ComponentModel
;
使用System.Data;使用System.Drawing
;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows.Forms;

名称空间Sending_data_serial
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent() ;
}

private void button1_Click(object sender,EventArgs e)
{
serialPort1.Open();
serialPort1.Write(1);
serialPort1.Close();
}

private void button2_Click(object sender,EventArgs e)
{
serialPort1.PortName = textBox1.Text;
serialPort1.BaudRate = Convert.ToInt32(textBox2.Text);


}
}
}

解决方案

你需要看看这个函数的重载, SerialPort.Write Method(Byte [ ],Int32,Int32)(System.IO.Ports) [ ^ ],它允许您从缓冲区中写入字节。传递0和byteArray.Length作为参数,将完整的缓冲区发送到串口。



要发送一个字节,可以执行以下操作,

  //  字节假定为byte []类型,一些数据。 
serialPort1.Write(bytes, 0 1 ); // 从0开始,转到1; 1字节。



另一个很好的例子是Jochen在解决方案2中提供的解决方案,也看到了但是你可以从任何来源获得字节,就是这样字符编码,命令或其他。


您仍然可以使用接受字符串的方法来发送字节,因为默认情况下使用ASCII编码(将每个字符转换为ASCII字节值)。 />


所以二进制值0x01可以发送为

 serialPort1.Write(  \ u0001); 





但是,如果您需要发送包含空字节的数据,值> = 128,或者避免Unicode转换为ASCII,则必须使用接受字节数组的方法:

  var  dataByte =  new   byte  [] {0x00}; 
serialPort1.Write(dataByte, 0 1 );


Hello good people.
I want to send data to controller, and then its will turn a "Light-emitting diode" On.
IM confused how to send bytes , as i get this command sending data on string format, not on bytes:

serialPort1.Write("1");


Maybe im wrong
Any Help, Thanks.
Code included below.

What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sending_data_serial
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            serialPort1.Open();
            serialPort1.Write("1");
            serialPort1.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            serialPort1.PortName = textBox1.Text;
            serialPort1.BaudRate = Convert.ToInt32(textBox2.Text);


        }
    }
}

解决方案

You need to have a look at this overload of the function, SerialPort.Write Method (Byte[], Int32, Int32) (System.IO.Ports)[^], it allows you to write the bytes from a buffer. Pass 0 and byteArray.Length as the parameter to send a complete buffer to the serial port.

To send just one byte, you can do the following,

// bytes is assumed to be of byte[] type with some data.
serialPort1.Write(bytes, 0, 1); // Start from 0, go to 1; 1 byte.


Another good example is given in solution by Jochen in Solution 2, also see that but you can get the bytes from any source, be that character encoding, command or whatever.


You can still use the method accepting a string to send bytes because that uses ASCII encoding by default (converts each character to an ASCII byte value).

So the binary value 0x01 can be send as

serialPort1.Write("\u0001");



But if you need to send data containing a null byte, values >= 128, or avoid the Unicode to ASCII conversion, you must use the method accepting a byte array:

var dataByte = new byte[] { 0x00 };
serialPort1.Write(dataByte, 0, 1);


这篇关于需要使用C#向微控制器发送字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆