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

来源:互联网 发布:我知女人心阮琦 编辑:程序博客网 时间:2024/05/16 00:41

基于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;
}
   

1 0
原创粉丝点击