一、c++上位机与WiFi小车通讯

基于51单片机WiFi视频机器小车c++上位机开发

  1、首先,单片机、WiFi模块组装完毕后,请确保电脑可以连接小车的WiFi信号。

2、建立c++  CRL项目。

3、配置opencv2.4X,

4、项目连接小车的WiFi视频地址,IP地址和端口

const string cameraAddress = "http://192.168.1.1:8080/?action=stream&type=.mjpg";
System::String^ controlip = "192.168.1.1";
System::String^ port = "2001";

  5、获取小车视频并转化为opencv

vcap.open(cameraAddress);

Mat frame;

vcap.read(frame);

6、与小车进行通讯并发送指令

IPAddress^ ips = IPAddress::Parse(controlip);
IPEndPoint^ ipe = gcnew IPEndPoint(ips, Convert::ToInt32(port));
Socket^ c = gcnew Socket(ipe->AddressFamily, SocketType::Stream, ProtocolType::Tcp);
c->Connect(ipe);

array<Byte>^ up = { 0xFF, 0x00, 0x02, 0x00, 0xFF };//向前 

array<Byte>^ rcode = gcnew array<Byte>(100);             

下面是源码:

 

// 小车追球test2.cpp: 主项目文件。

#include "stdafx.h"


#include "stdafx.h"
#include "opencv2/opencv.hpp"
//#include "conio.h"
//#include "cv.h"
#include "highgui.h"
//#include "cxcore.h"
#include "iostream"
#include "Windows.h"
#include "Xinput.h"


#using <System.dll>


using namespace System;
using namespace System::Net;
using namespace System::Net::Sockets;
using namespace System::Text;
using namespace cv;
using namespace std;


CvCapture* capture = 0;
IplImage* image;
void Hough(IplImage*);
//int style = 0;//小车当前状态:1为停止,2位前进,3位后退,4位左转,5为右转


int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");


cvNamedWindow("camera", CV_WINDOW_AUTOSIZE);
cvMoveWindow("camera", 0, 0);


const string cameraAddress = "http://192.168.1.1:8080/?action=stream&amp;type=.mjpg";
System::String^ controlip = "192.168.1.1";
System::String^ port = "2001";


VideoCapture vcap;
Mat frame;


if (!vcap.open(cameraAddress))
{
Console::WriteLine(L"ERRER");
return -1;
}




IPAddress^ ips = IPAddress::Parse(controlip);
IPEndPoint^ ipe = gcnew IPEndPoint(ips, Convert::ToInt32(port));
Socket^ c = gcnew Socket(ipe->AddressFamily, SocketType::Stream, ProtocolType::Tcp);
c->Connect(ipe);
array<Byte>^ stop = { 0xFF, 0x00, 0x00, 0x00, 0xFF };//停止
array<Byte>^ up = { 0xFF, 0x00, 0x02, 0x00, 0xFF };//向前 
array<Byte>^ down = { 0xFF, 0x00, 0x01, 0x00, 0xFF };//向后
array<Byte>^ left = { 0xFF, 0x00, 0x03, 0x00, 0xFF };//左转
array<Byte>^ right = { 0xFF, 0x00, 0x04, 0x00, 0xFF };//右转
array<Byte>^ left_speed = { 0xFF, 0x02, 0x01, 0x09, 0xFF };//左侧速度为7
array<Byte>^ right_speed = { 0xFF, 0x02, 0x02, 0x04, 0xFF };//右侧速度为7

array<Byte>^ rcode = gcnew array<Byte>(100);
int i = 0;
while (c->Connected)
{

cvWaitKey(10);


vcap.read(frame);
//方向测试----------------------------------------------------------------------------------------------------------------------

i++;


if (i>100)
{
c->Send(stop, stop->Length, SocketFlags::None);//发送指令
}
else{
c->Send(up, up->Length, SocketFlags::None);//发送指令

}


//--------------------------------------------------------------------------------------------------------------------------------

imshow("camera", frame);
}


cvDestroyWindow("camera");
c->Close();
return 0;
}   

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值