我有一个带2个按钮(BIT和数据)的图形用户界面。当我按下BIT按钮时,它会将十六进制数据发送到与设备连接的端口。该设备重播另一个十六进制。我用port.DataReceived函数读取这个十六进制,在textBox中显示它并检查每个字节中的每个位并在文本框中显示值的位。这认为工作正常。当我按下数据按钮时,我将不同的十六进制发送到同一设备,并使用不同的十六进制重放。现在我只想显示重播而不检查它。我不知道如何制作它,因为当按下数据按钮发送十六进制时,接收来自设备的响应并且每个位都是ckeck。我不想这样做,我只想用十六进制显示它。如果有人可以帮助我,我会在这里发布我的代码。
我尝试使用2种不同的功能来读取设备的重放,但我认为这种方式并不好。我读了一些关于旗帜的内容,我希望这是解决问题的更好方法,我想寻求帮助。
我也会在这里发布我的代码。
答案 0 :(得分:0)
为什么不分离出readFromDevice()代码,并将接收到的值传递回调用方法?这样您就可以根据需要以多种不同的方式处理返回的值。
编辑:
我猜这个设计会异步回复(即它会在你发送数据后回复一段时间)?如果是这种情况,则需要向com端口添加事件处理程序,然后处理收到的数据。
您还需要跟踪点击的按钮。你可以使用一个简单的布尔值:
//In the data buttons click handler
DataButtonClicked = true;
BitButtonClicked = false;
//add an event handler to the port
String RecievedData;
SerialPort mySerialPort = new SerialPort("COM1");
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
//code for the event handler
private static void DataReceivedHandler(
object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
RecievedData = sp.ReadExisting();
if(DataButtonClicked)
{
//display the value one way
}
if(BitbuttonClicked)
{
//display the value another way
}
}
我的基础是我在下面发布的MSDN链接。
答案 1 :(得分:0)
namespace read_display_data
{ 公共部分类Form1:表格 {
SerialPort port = new SerialPort("COM2", 19200, Parity.None, 8, StopBits.One);
public Form1()
{
InitializeComponent();
port.Open();
}
private void Form1_Load(object sender, EventArgs e)
{
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceivedBIT);
// port.ReadTimeout = 500;
}
private void BITbutton_Click(object sender, EventArgs e)
{
port.Write(new byte[] { 0x10 0x04, 0x06 }, 0, 3);
byte[] buffer1 = new byte[] { 0x10 0x04, 0x06 };
LogSent(LogMsgType.Outgoing, DateTime.Now + " -- " + ByteArrayToHexString(buffer1) + "\n");
//port.Close();
}
private void Databutton_Click(object sender,EventArgs e) {
port.Write(new byte[] { 0x11, 0x43, 0x0D}, 0, 3);
byte[] buffer1 = new byte[] {0x11, 0x43, 0x0D };
LogSent(LogMsgType.Outgoing, DateTime.Now + " -- " + ByteArrayToHexString(buffer1) + "\n");
}
private Color[] LogMsgTypeColor = { Color.Blue, Color.Green, Color.Black, Color.Orange, Color.Red };
public enum LogMsgType { Incoming, Outgoing, Normal, Warning, Error };
void port_DataReceivedBIT(object sender, SerialDataReceivedEventArgs e)
{
//port.Open();
this.Invoke(new EventHandler(AddReceiveBIT));
}
private void AddReceiveBIT ( object s, EventArgs e)
{
Thread.Sleep(100);
byte[] buffer = new byte[port.BytesToRead];
port.Read(buffer, 0, buffer.Length);
LogReceive(LogMsgType.Incoming, DateTime.Now + " -- " + ByteArrayToHexString(buffer) + "\n");
Log(LogMsgType.Incoming, checkBit(buffer) + "\n");
}
private string checkBit(byte[] buffer)
{
StringBuilder message = new StringBuilder();
var responseByte = buffer[7];
if ((responseByte & (1 << 0)) == 0)
{
message.AppendFormat("rezerved ok ");
}
else
{
message.AppendFormat("rezerved error ");
}
if ((responseByte & (1 << 1)) == 0)
{
message.AppendFormat("rezerved ok ");
}
else
{
message.AppendFormat("rezerved error ");
}
if ((responseByte & (1 << 2)) == 0)
{
message.AppendFormat("rezerved ok ");
}
else
{
message.AppendFormat("rezerved error ");
}
if ((responseByte & (1 << 3)) == 0)
{
message.AppendFormat("rezerved ok ");
}
else
{
message.AppendFormat("rezerved error ");
}
if ((responseByte & (1 << 4)) == 0)
{
message.AppendFormat("rezerved ok ");
}
else
{
message.AppendFormat("rezerved error ");
}
if ((responseByte & (1 << 5)) == 0)
{
message.AppendFormat("rezerved ok ");
}
else
{
message.AppendFormat("rezerved error ");
}
if ((responseByte & (1 << 6)) == 0)
{
message.AppendFormat("Serial Communication ok ");
}
else
{
message.AppendFormat("Serial Communication error ");
}
if ((responseByte & (1 << 7)) == 0)
{
message.AppendFormat("Optical Zoom ok ");
}
else
{
message.AppendFormat("Optical Zoom error ");
}
return message.ToString();
}
private string ByteArrayToHexString(byte[] PortBuffer)
{
StringBuilder sb = new StringBuilder(PortBuffer.Length * 3);
foreach (byte b in PortBuffer)
sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));
return sb.ToString().ToUpper();
}
private void LogReceive(LogMsgType msgtype, string msg)
{
richTextBox1.Invoke(new EventHandler(delegate
{
richTextBox1.SelectedText = string.Empty;
richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, FontStyle.Bold);
richTextBox1.SelectionColor = LogMsgTypeColor[(int)msgtype];
richTextBox1.AppendText(msg);
richTextBox1.ScrollToCaret();
}
));
}
private void LogSent(LogMsgType msgtype, string msg)
{
textBox2.Invoke(new EventHandler(delegate
{
textBox2.SelectedText = string.Empty;
textBox2.AppendText(msg);
textBox2.ScrollToCaret();
}
));
}
private void Log(LogMsgType msgtype, string msg)
{
textBox3.Invoke(new EventHandler(delegate
{
textBox3.Clear();
textBox3.SelectedText = string.Empty;
textBox3.AppendText(msg);
textBox3.ScrollToCaret();
}
));
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}