Estou precisando muito de um código fonte em c/c++ que faz comunicação com o arduino que roda no console application Visual Studio C++

Quem tiver ai, por favor, me ajude.

Exibições: 1450

Responder esta

Respostas a este tópico

Amigão, o codeblocs tem uma versão especifica para arduino. Deve vir algum exemplo no projeto.

http://sourceforge.net/projects/arduinodev/

http://www.arduinodev.com/codeblocks-arduino-edition-updated-with-a...

Felipe... eu fiz usando o VS.NET... veja se resolve se problema...

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Threading;

namespace Tradutor
{
class Tradutor
{
static bool continuarLendo;
static SerialPort portaSerial;
static string dataStart = DateTime.Now.ToString();

public static void Main()
{
Console.Title = "Console v1";
Console.Clear();
Console.ForegroundColor = ConsoleColor.Cyan;
Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.Clear();

portaSerial = new SerialPort();

portaSerial.PortName = "COM15";
portaSerial.BaudRate = 9600;
portaSerial.Parity = Parity.None;
portaSerial.DataBits = 8;
portaSerial.StopBits = StopBits.One;
portaSerial.Handshake = Handshake.None;

portaSerial.ReadTimeout = 10000;
portaSerial.WriteTimeout = 10000;

portaSerial.Open();
continuarLendo = true;

Thread threadLeitura = new Thread(acessarSerial);
threadLeitura.Start();

threadLeitura.Join();
portaSerial.Close();
}

public static void acessarSerial()
{
while (continuarLendo)
{

try
{
string msg = portaSerial.ReadLine();
if (msg.StartsWith("online:")) ////arduino deve dizer se está online ou não, usar Serial.println("online");
{
portaSerial.WriteLine("Texto pro Arduino");
}
tela(msg);
}
catch (Exception ex)
{
tela("Erro: " + ex.Message.ToString() + "\nFeche o programa e execute-o novamente para continuar.");
}
}
}

public static void tela(string p)
{
Console.Clear();
Console.WriteLine("Módulo ativo desde " + dataStart + ".");
Console.WriteLine("Agora:" + DateTime.Now.ToString());
Console.WriteLine("Status : " + p);
}


}
}

Estou tentando desse jeito no  Win32 Console Application

#include "stdafx.h"
#include <Windows.h>
#include <stdio.h>
#include <conio.h>

int _tmain(int argc, _TCHAR* argv[])
{
char test[] = "1";

HANDLE hDevice = CreateFile(L"COM3",GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,0);

if (hDevice !=INVALID_HANDLE_VALUE)
{
printf("Port opened! \n");
DCB lpTest;
GetCommState(hDevice,&lpTest);
lpTest.BaudRate = CBR_9600;
lpTest.ByteSize = 8;
lpTest.Parity = NOPARITY;
lpTest.StopBits = ONESTOPBIT;
SetCommState(hDevice,&lpTest);

DWORD btsIO;


//int isWritten = WriteFile(m_hCommPort, &data,(DWORD) sizeof(data), &dwBytesWritten, NULL);
WriteFile(hDevice, test, sizeof(test), &btsIO, NULL);
//WriteFile(hDevice, test,strlen(test), &btsIO, NULL);

getchar();
CloseHandle(hDevice);
}
_getch();


return 0;
}

Mais não da certo.

Só abre o a porta mais não envia. 

No CLR Console Application funcionou muito bem.

#include "stdafx.h"

using namespace System;
using namespace System::IO::Ports;

int main(array<System::String ^> ^args)
{

String^ answer;
String^ portName;
int baudRate=9600;
Console::WriteLine("Type in a port name and hit ENTER");
portName=Console::ReadLine();
// arduino settings
SerialPort^ arduino;
arduino = gcnew SerialPort(portName, baudRate);
// open port
try
{
arduino->Open();

do
{
// ask on or off
Console::WriteLine("digite a ou b");
// get answer
answer=Console::ReadLine();
//check that user typed one of the options
if(String::Compare(answer,"a")==0)
arduino->WriteLine("180"); // send 180 to arduino
else if(String::Compare(answer,"b")==0)
arduino->WriteLine("0"); // send 0 to arduino
else
Console::WriteLine(answer+" was not an option");
// ask user if he wants to continue
Console::WriteLine("Try again? yes/no");
// get answer
answer=Console::ReadLine();
// clear the screen
Console::Clear();
}while(String::Compare(answer,"yes")==0);
// close port to arduino
arduino->Close();
}
catch (IO::IOException^ e )
{
Console::WriteLine(e->GetType()->Name+": Port is not ready");
}
catch (ArgumentException^ e)
{
Console::WriteLine(e->GetType()->Name+": incorrect port name syntax, must start with COM/com");
}

controla o servo motor muito bem, só colocar o numero 0 a 180.

Gostaria de usar o arduino no Dev-C++

Tem como?

Gostaria de fazer um login para entrar no programa que eu fiz em DevC que usa botões controlador por microcontrolador.

RSS

© 2024   Criado por Marcelo Rodrigues.   Ativado por

Badges  |  Relatar um incidente  |  Termos de serviço