gpt4 book ai didi

C#串口写字节

转载 作者:行者123 更新时间:2023-11-30 14:35:43 25 4
gpt4 key购买 nike

在 C# 中的 .NET 4.0 中将单个字节写入串行端口会导致

InvalidOperationException was unhandled by user code

每发送一个字节到串口。

如何向串口写入一个字节?

    //Serial Init
//Full fledged constuctor
public NetCommManager(String portName, TransmissionType trans, String baud, String parity, String stopBits, String dataBits)
{
nc_baudRate = baud;
nc_parity = parity;
nc_stopBits = stopBits;
nc_dataBits = dataBits;
nc_portName = portName;
nc_transType = trans;

//now add an event handler
comPort.DataReceived += new SerialDataReceivedEventHandler(netComm_DataReceived);
}

配置:

       _commManger = new NetCommManager(commPortNumber,                        
NetCommManager.TransmissionType.Text, "19200", "None", "One", "8");

要写入的字节:

_commManager.WriteByte(Convert.ToByte( 0x7B));

WriteByte函数是:

public void WriteByte(byte data)
{
//change data to array
//byte[] dataArray = new byte[1];
var dataArray = new byte[] {data};
//dataArray[0] = data;
comPort.Write(dataArray, 0, 1); // <-- Exception is thrown here
}

NetCommManager 类非常基于 this example

最佳答案

关于C#串口写字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11680730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com