|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这个是通过连接WIFI控制 视频小车的,建立tcp连接到192.168.1.1,端口2001.通过socket来发送
上位机--》路由器--》单片机--》驱动马达
这个程序是别人的,是人家开源的,请问用Delphi 7 怎么写呢,?
在Delphi 7 中 只需要 Internet 中的一个TcpClient 控件吗?还需要其他的什么东西吗?
我学Delphi 7 主要是来搞这个上位机的,实现的功能较少,呵呵,才学快一个月,还有很多东西不懂,
给点稍微详细点的思路或 给个框架 我自己来完善 可以把,呵呵呵,谢谢大家
这里不能上传图片,请下载源程序,内有源码,谢谢
程序下载: http://pan.baidu.com/s/1c0nBZiw
Config.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WifiVideo
{
public partial class Config : Form
{
public Config()
{
InitializeComponent();
}
public string FileName;
[DllImport("kernel32")]
private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);
private void Config_Load(object sender, EventArgs e)
{
GetIni();
}
private void button1_Click(object sender, EventArgs e)
{
WriteIni("VideoUrl","videourl",this.textBoxVideo.Text);
WriteIni("ControlUrl", "controlUrl", this.textControlURL.Text);
WriteIni("ControlPort", "controlPort", this.textBoxControlPort.Text);
WriteIni("ControlCommand", "CMD_Forward", this.txtForward.Text);
WriteIni("ControlCommand", "CMD_Backward", this.txtBackward.Text);
WriteIni("ControlCommand", "CMD_TurnLeft", this.txtLeft.Text);
WriteIni("ControlCommand", "CMD_TurnRight", this.txtRight.Text);
WriteIni("ControlCommand", "CMD_Stop", this.txtStop.Text);
WriteIni("ControlCommand", "CMD_EngineUp", this.txtEngineUp.Text);
WriteIni("ControlCommand", "CMD_EngineDown", this.txtEngineDown.Text);
MessageBox.Show("配置成功!请重启程序以使配置生效。", "配置信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Close();
}
public void WriteIni(string Section, string Ident, string Value)
{
if (!WritePrivateProfileString(Section, Ident, Value, FileName))
{
throw (new ApplicationException("写入配置文件出错"));
}
}
public string ReadIni(string Section, string Ident, string Default)
{
Byte[] Buffer = new Byte[65535];
int bufLen = GetPrivateProfileString(Section, Ident, Default, Buffer, Buffer.GetUpperBound(0), FileName);
string s = Encoding.GetEncoding(0).GetString(Buffer);
s = s.Substring(0, bufLen);
return s.Trim();
}
private void GetIni()
{
FileName = Application.StartupPath + "";
this.textBoxVideo.Text = ReadIni("VideoUrl", "videourl", "");
this.textControlURL.Text = ReadIni("ControlUrl", "controlUrl", "");
this.textBoxControlPort.Text = ReadIni("ControlPort", "controlPort", "");
this.txtForward.Text = ReadIni("ControlCommand", "CMD_Forward", "");
this.txtBackward.Text = ReadIni("ControlCommand", "CMD_Backward", "");
this.txtLeft.Text = ReadIni("ControlCommand", "CMD_TurnLeft", "");
this.txtRight.Text = ReadIni("ControlCommand", "CMD_TurnRight", "");
this.txtStop.Text = ReadIni("ControlCommand", "CMD_Stop", "");
this.txtEngineUp.Text = ReadIni("ControlCommand", "CMD_EngineUp", "");
this.txtEngineDown.Text = ReadIni("ControlCommand", "CMD_EngineDown", "");
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
2、
Form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using Microsoft.Win32;
using System.Runtime.InteropServices;
namespace WifiVideo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("kernel32")]
private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);
static string FileName = Application.StartupPath + "";
public string ReadIni(string Section, string Ident, string Default)
{
Byte[] Buffer = new Byte[65535];
int bufLen = GetPrivateProfileString(Section, Ident, Default, Buffer, Buffer.GetUpperBound(0), FileName);
string s = Encoding.GetEncoding(0).GetString(Buffer);
s = s.Substring(0, bufLen);
return s.Trim();
}
string CameraIp = "";
string ControlIp = "192.168.1.1";
string Port = "81";
string CMD_Forward = "", CMD_Backward = "", CMD_TurnLeft = "", CMD_TurnRight = "", CMD_Stop = "", CMD_EngineUp="",CMD_EngineDown="";
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
pictureBox1.ImageLocation = CameraIp;// "http://192.168.1.1:8080/?action=snapshot";
}
void SendData(string data)
{
try
{
IPAddress ips = IPAddress.Parse(ControlIp.ToString());//("192.168.2.1");
IPEndPoint ipe = new IPEndPoint(ips, Convert.ToInt32(Port.ToString()));
Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个Socket
c.Connect(ipe);//连接到服务器
byte[] bs = Encoding.ASCII.GetBytes(data);
c.Send(bs, bs.Length, 0);//发送测试信息
c.Close();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
SendData(CMD_Forward);
}
private void button5_Click(object sender, EventArgs e)
{
SendData(CMD_Backward);
}
private void button3_Click(object sender, EventArgs e)
{
SendData(CMD_TurnLeft);
}
private void button4_Click(object sender, EventArgs e)
{
SendData(CMD_TurnRight);
}
private void button6_Click(object sender, EventArgs e)
{
Config cfg = new Config();
cfg.ShowDialog();
}
private void Form1_Load(object sender, EventArgs e)
{
GetIni();
buttonForward.BackColor = Color.LightBlue;
buttonBackward.BackColor = Color.LightBlue;
buttonLeft.BackColor = Color.LightBlue;
buttonRight.BackColor = Color.LightBlue;
buttonStop.BackColor = Color.LightBlue;
btnEngineUp.BackColor = Color.LightBlue;
btnEngineDown.BackColor = Color.LightBlue;
}
private void GetIni()
{
CameraIp = ReadIni("VideoUrl", "videoUrl", "");
ControlIp = ReadIni("ControlUrl", "controlUrl", "");
Port = ReadIni("ControlPort", "controlPort", "");
CMD_Forward = ReadIni("ControlCommand", "CMD_Forward", "");
CMD_Backward = ReadIni("ControlCommand", "CMD_Backward", "");
CMD_TurnLeft = ReadIni("ControlCommand", "CMD_TurnLeft", "");
CMD_TurnRight = ReadIni("ControlCommand", "CMD_TurnRight", "");
CMD_Stop = ReadIni("ControlCommand", "CMD_Stop", "");
CMD_EngineUp = ReadIni("ControlCommand", "CMD_EngineUp", "");
CMD_EngineDown = ReadIni("ControlCommand", "CMD_EngineDown", "");
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W)
{
buttonForward.BackColor = Color.DarkGray;
buttonForward.PerformClick();
}
else if (e.KeyCode == Keys.S)
{
buttonBackward.BackColor = Color.DarkGray;
buttonBackward.PerformClick();
}
else if (e.KeyCode == Keys.A)
{
buttonLeft.BackColor = Color.DarkGray;
buttonLeft.PerformClick();
}
else if (e.KeyCode == Keys.D)
{
buttonRight.BackColor = Color.DarkGray;
buttonRight.PerformClick();
}
else if (e.KeyCode == Keys.X)
{
buttonStop.BackColor = Color.DarkGray;
buttonStop.PerformClick();
}
else if (e.KeyCode == Keys.I)
{
btnEngineUp.BackColor = Color.DarkGray;
btnEngineUp.PerformClick();
}
else if (e.KeyCode == Keys.K)
{
btnEngineDown.BackColor = Color.DarkGray;
btnEngineDown.PerformClick();
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
buttonStop.PerformClick();
buttonForward.BackColor = Color.LightBlue;
buttonBackward.BackColor = Color.LightBlue;
buttonLeft.BackColor = Color.LightBlue;
buttonRight.BackColor = Color.LightBlue;
btnEngineUp.BackColor = Color.LightBlue;
btnEngineDown.BackColor = Color.LightBlue;
}
private void btnEngineUp_Click(object sender, EventArgs e)
{
SendData(CMD_EngineUp);
}
private void btnEngineDown_Click(object sender, EventArgs e)
{
SendData(CMD_EngineDown);
}
private void buttonStop_Click(object sender, EventArgs e)
{
SendData(CMD_Stop);
}
}
}
3、Config.ini文件结构:
[VideoUrl]
videourl=http://192.168.1.1:8080/?action=snapshot
[ControlUrl]
controlUrl=192.168.1.1
[ControlPort]
controlPort=2001
[ControlCommand]
CMD_Forward=3
CMD_Backward=1
CMD_TurnLeft=2
CMD_TurnRight=4
CMD_Stop=0
CMD_EngineUp=5
CMD_EngineDown=6 |
|