简体   繁体   中英

How to write a C# program to send hex bytes command to a serial port

A small car with wifi is connected to my computer. I wanna send a data package in hex (eg FF 00 01 00 FF ) to it (IP: 192.168.1.1, Port:2001) with Visual Studio 2015.

I'm a Newbie in C# only with some basic knowledge of C..I just got some API form the car designer and write something as below. But I'm not sure if they are ok to run...

serialPort1.PortName = "COM1"; 
serialPort1.BaudRate = 9600; 
serialPort1.Open(); 
byte [] data= CreateData(0x01, 0x02, 0x03);    //design a hex package 
comm=this.SerialPort1;                     //I don't know what the "this" mean ?
RobotEngine2.SendCMD(0, byteData, comm);    //this sentence got from the API 

(I'm sorry that the code must be ugly for me such a newbie. But I would really appreciate it if you can help give me some directions. Thank you.)

I did some digging and found this website: http://www.wifi-robots.com/thread-4718-1-1.html You will have to translate it and sort out the code.

So far it looks like you don't need to use the System.IO.Ports because it is being used by the API.

This is what i could translate but the code provided by the API was very messy:

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);
        }
    }
}

Sorry mate but you will need to provide more information if you would want this question solved completely but hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM