SimpleTcpClient发送单字节并接收没有分隔符的单字节

时间:2018-10-24 12:47:15

标签: c#

我已经使用SimpleTcpClient编写了一个简单的TCP IP应用程序。 我只需要向电子设备发送单个字节,然后该设备要么向后发送单个字节,要么什么都不发送。 定时很重要,因此我还需要尽可能快地重复发送字节,并且还要定时。

使用“ Client.WriteLineAndGetReply”时,该应用程序在某种程度上可以正常工作,但是我有讨厌的定界符十六进制13,由于“ Client.WriteLineAndGetReply”,我不想发送给我的电子设备。使用此设备,我的设备会做出响应,并且可以在“ textBoxReceiveByte”中看到返回值。

当我使用“ Client.Write(bytes);”时我可以看到我只发送想要的单个字节,但是现在我没有从电子设备中读回单个字节。

我的代码:

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

namespace Client
{
    public partial class Form1 : Form
    {
        Stopwatch sendStopWatch = new Stopwatch();
        ToolTip toolTiptextBoxMessage = new ToolTip();
        TimeSpan time;
        SimpleTcpClient Client;

        public Form1()
        {
            InitializeComponent();
        }

        private void buttonConnect_Click(object sender, EventArgs e)
        {
            buttonConnect.Enabled = false;
            Client.Connect(textBoxHost.Text,
                     Convert.ToInt32(textBoxPort.Text));
            buttonDisconnect.Enabled = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Client = new SimpleTcpClient();
            //Client.StringEncoder = Encoding.UTF8;
            Client.DataReceived += Client_DataReceived;
        }

        private void Client_DataReceived(object sender, SimpleTCP.Message e)
        {
            textBoxReceiveByte.Invoke((MethodInvoker)delegate ()
            {
                textBoxReceiveByte.Text += e.MessageString;
            });
        }

        private void buttonTCPIPSend_Click(object sender, EventArgs e)
        {
            //string sendString = textBoxMessage.Text.Replace(" ",
            //       string.Empty);
            //textBoxMessage.Text = sendString;

            //byte[] bytes = Encoding.ASCII.GetBytes(textBoxMessage.Text);
            byte[] bytes = { 0 };

            sendStopWatch.Stop();
            sendStopWatch.Reset();

            for (int i = 0; i < 5; i++)
            {
                sendStopWatch.Start();

                //Client.WriteLineAndGetReply(textBoxMessage.Text,
                //    TimeSpan.FromSeconds(0));
                Client.Write(bytes);

                sendStopWatch.Stop();
                if (checkBoxReportOnOff.Checked)
                {
                    time = sendStopWatch.Elapsed;
                    textBoxStatus.Text += "Time elapsed (ms): " +
                          time.TotalSeconds * 1000 + "\r\n";
                }
                sendStopWatch.Reset();
            }        
        }

        private void buttonDisconnect_Click(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBoxStatus.Text = "";
        }

        private void textBoxMessage_MouseHover(object sender, EventArgs e)
        {
            toolTiptextBoxMessage.Show("Hex byt must be two characters with
             spaces inbetween. Example: 01 13 14 15 16 10", textBoxMessage);
        }

        private void checkBoxBytesSendAll_CheckedChanged(object sender,
                 EventArgs e)
        {
        }

        private void checkBoxDelayEach_CheckedChanged(object sender,
                           EventArgs e)
        {
            if (checkBoxDelayEach.Checked)
            {
                textBoxDelayAfterEach.Enabled = true;
            }
            else
            {
                textBoxDelayAfterEach.Enabled = false;
            }
        }

        private void checkBoxDelayAll_CheckedChanged(object sender,
                      EventArgs e)
        {
            if (checkBoxDelayAll.Checked)
            {
                textBoxDelayAfterAll.Enabled = true;
            }
            else
            {
                textBoxDelayAfterAll.Enabled = false;
            }
        }

        private void groupBoxByteSend_MouseHover(object sender, EventArgs e)
        {
            toolTiptextBoxMessage.Show("When checked all bytes in HEX text
                  box will be send. Else only first byte.", textBoxMessage);
        }

        private void buttonCommsCheck_Click(object sender, EventArgs e)
        {
            byte[] bytes = { 0 };

            //Client.WriteLineAndGetReply("0", TimeSpan.FromSeconds(0));
            Client.Write(bytes);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我不熟悉SimpleTcp(它不是.NET的一部分),但是由于没有人回答,因此我会尝试一下。通过查看source code,看来读取单个字节的唯一方法是直接获取TcpClient并使用其流:

byte [] toRead = new byte[1];
NetworkStream stream = Client.TcpClient.GetStream();
stream.Read(toRead, 0, 1);

希望这会有所帮助。