求一段串口通信的C#代码!

来源:百度知道 编辑:UC知道 时间:2024/06/03 19:23:07
用什么方法通信都可以尽量写的详细点以前没有做过关于串口通信这一块的东西,只要实现很简单的东西就好.
1.想串口发送一串16进制的数字 如: 0a 32 30 34 30 32 30 30 30 0d 0d
2.读取设备返回的信息 截取字符 转换为10进制 就好
mail:tcliu1986@126.com
用拖控件或SerialPort类的 方式都行 只要能接收数据 并能以10进制显示出来就好

public static void Main()
{
string name;
string message;
StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
Thread readThread = new Thread(Read);

// Create a new SerialPort object with default settings.
_serialPort = new SerialPort();

// Allow the user to set the appropriate properties.
_serialPort.PortName = SetPortName(_serialPort.PortName);
_serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate);
_serialPort.Parity = SetPortParity(_serialPort.Parity);
_serialPort.DataBits = SetPortDataBits(_serialPort.DataBits);
_serialPort.StopBits = SetPortStopBits(_serialPort.StopBits);
_serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);

// Set the read/write timeouts
_serialPort.ReadTimeout = 500;
_serialPort.WriteTimeout = 500;

_serialPort.Open();
_continue = true;