如何编写C#程序将十六进制字节命令发送到串口

时间:2016-02-27 10:56:35

标签: c# hex port serial-communication

带有wifi的小型车连接到我的电脑。我想用Visual Studio 2015向它发送一个数据包(例如git checkout -b new_branch )(IP:192.168.1.1,Port:2001)。

我是C#中的新手,只有一些C ...的基本知识。我刚刚从汽车设计师处获得了一些API,并编写如下内容。但我不确定他们是否可以参加......

FF 00 01 00 FF

(对不起,对于我这样的新手来说代码一定很难看。但如果你能给我一些指示,我真的很感激。谢谢。)

1 个答案:

答案 0 :(得分:0)

我做了一些挖掘并找到了这个网站: http://www.wifi-robots.com/thread-4718-1-1.html 您必须翻译它并整理代码。

到目前为止,您似乎不需要使用System.IO.Ports,因为API正在使用它。

这是我可以翻译的内容,但API提供的代码非常混乱:

using System;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;
using WIFIRobotCMDEngineV2;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        String ControlIp = "192.168.1.1";
        String Port = "2001";
        public WifiRobotCMDEngineV2 RobotEngine2;
        IPAddress static ips;
        static IPEndPoint ipe;
        static Socket socket = null;
        String CMD_Forward = "FF0000FF";

        public Form1()
        {
            InitializeComponent();
            RobotEngine2 new WifiRobotCMDEngineV2 = ((Object)this.button1);
        }
        bool ret = false;
        private bool InitWIFISocket(controlIp String, String Port)
        {
            ips = IPAddress.Parse(controlIp.ToString()); ipe = new IPEndPoint(ips, Convert.ToInt32(port.ToString())); socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType TCP);
            Socket.connect(ipe); RobotEngine2.SOCKET = socket; RobotEngine2.IPE = ipe; ret = RobotEngine2.SocketConnect();
            return ret;
        }
        private void Form1_Load(SENDER Object, EventArgs e) { }
        void button1_Click Private(SENDER Object, EventArgs e) { InitWIFISocket(ControlIp, Port); Label1.Text = ret.ToString(); }
        void Private button2_Click(SENDER Object, EventArgs e) { SerialPort COMM = new SerialPort(); RobotEngine2.SendCMD(0, CMD_Forward, COMM);
        }
    }
}

很抱歉,但是如果您希望完全解决这个问题,那么您需要提供更多信息,但希望这会有所帮助。

相关问题