SerialPorts:以字节或字符发送
本文关键字:字符 字节 SerialPorts | 更新日期: 2023-09-27 18:02:37
我正在使用串行端口应用程序发送具有Unicode控制字符的命令,并且没有成功。当我在超级终端或Putty中发送相同的命令时,通信工作,所以我很好奇这两个程序是否写出字节数组或Unicode字符甚至字符串。如果我模仿他们的做法,我可能会成功。我还注意到,我在Visual Studio中从工具箱中取出的串口属性被列为使用我在代码中编写的不同COM端口,这是为什么我没有得到响应还是我的代码覆盖它?在进行环回测试时,我也得到了带有控制字符的正确命令的输出,因此我不知道我做错了什么。这是。net 2.0框架,供参考
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
namespace SimpleSerial
{
public partial class Form1 : Form
{
string RxString;
public Form1()
{
InitializeComponent();
}
private void buttonStart_Click(object sender, EventArgs e)
{
serialPort1.PortName = "COM3";
serialPort1.BaudRate = 9600;
serialPort1.Parity = Parity.None;
serialPort1.DataBits = 8;
serialPort1.StopBits = StopBits.One;
serialPort1.Handshake = Handshake.None;
serialPort1.ReadTimeout = 500;
serialPort1.WriteTimeout = 500;
serialPort1.Open();
if (serialPort1.IsOpen)
{
buttonStart.Enabled = false;
buttonStop.Enabled = true;
textBox1.ReadOnly = false;
}
}
const char STX = ''u0002';
const char ETX = ''u0003';
readonly string pull_shelf_104 = string.Format("{0}01P00104##{1}" , STX, ETX);
private byte[] WrapString(string pull_shelf_104)
{
return System.Text.Encoding.ASCII.GetBytes(pull_shelf_104);
}
private void linkLabel_HC1_100_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
if (serialPort1.IsOpen)
{
byte[] data = WrapString(pull_shelf_104);
serialPort1.Write(data, 0, data.Length);
}
}
private void buttonStop_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
buttonStart.Enabled = true;
buttonStop.Enabled = false;
textBox1.ReadOnly = true;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (serialPort1.IsOpen) serialPort1.Close();
}
private void DisplayText(object sender, EventArgs e)
{
textBox1.AppendText(RxString);
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
RxString = serialPort1.ReadExisting();
this.Invoke(new EventHandler(DisplayText));
}
}
}
上面的评论帮我解决了问题,谢谢你的帮助!我需要在我的代码中同时启用RTS和DTS,列出COM端口不是问题,尽管我确信在某些情况下它是。这是我的代码给任何有类似问题的人。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
namespace SimpleSerial
{
public partial class Form1 : Form
{
string RxString;
public Form1()
{
InitializeComponent();
}
private void buttonStart_Click(object sender, EventArgs e)
{
serialPort1.PortName = "COM3";
serialPort1.BaudRate = 9600;
serialPort1.Parity = Parity.None;
serialPort1.DataBits = 8;
serialPort1.StopBits = StopBits.One;
serialPort1.Handshake = Handshake.None;
serialPort1.RtsEnable = true;
serialPort1.DtrEnable = true;
serialPort1.ReadTimeout = 2000;
serialPort1.WriteTimeout = 2000;
serialPort1.Open();
if (serialPort1.IsOpen)
{
buttonStart.Enabled = false;
buttonStop.Enabled = true;
textBox1.ReadOnly = false;
}
}
const char STX = ''u0002';
const char ETX = ''u0003';
readonly string pull_shelf_104 = string.Format("{0}01P00204##{1}" , STX, ETX);
private byte[] WrapString(string pull_shelf_104)
{
return System.Text.Encoding.ASCII.GetBytes(pull_shelf_104);
}
private void linkLabel_HC1_100_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
if (serialPort1.IsOpen)
{
byte[] data = WrapString(pull_shelf_104);
serialPort1.Write(data, 0, data.Length);
}
}
private void buttonStop_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
buttonStart.Enabled = true;
buttonStop.Enabled = false;
textBox1.ReadOnly = true;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (serialPort1.IsOpen) serialPort1.Close();
}
private void DisplayText(object sender, EventArgs e)
{
textBox1.AppendText(RxString);
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
RxString = serialPort1.ReadExisting();
this.Invoke(new EventHandler(DisplayText));
}
}
}