Página 1 de 1

Comunicação serial do pic16f877 e c++ builder - não funciona

MensagemEnviado: 10 Nov 2006 17:36
por Kleber
Pessoal,

Estou trabalhando em um projeto educacional com alguns alunos e deparei-me com algo estranho.
Os alunos cofiguraram o hyperterminal do windows e enviaram um caracter para o pic (programado), este faz um motor girar para direita, em suma, existe pelo menos 6 teclas (caracteres) que controlam o motor, pelo hyperterminal tudo bem, porém quando utiliza-se o c++ builder o pic não entende nada.
Testamos o programa do builder de forma a enviar estes caracteres para o hyperterminal detalhe, funcionou perfeitamente.
Checamos as configurações da serial do pic, hyperterminal e do builder, não encontramos nada de errado.
Sugestões?

MensagemEnviado: 10 Nov 2006 18:00
por zielpunkt
Olá, Kleber.

Tenho uma sugestão: faz um debug da serial, utilizando algo do tipo Serial Monitor, que é (ou era... :cry: ) free: http://www.hhdsoftware.com/sermon.html . Tem outros do mesmo tipo, também. Dai fica bem mais fácil de comparar o que está trafegando e matar o erro, ok.

Abraço.

Re: Comunicação serial do pic16f877 e c++ builder - não func

MensagemEnviado: 11 Nov 2006 07:14
por alsell
Usei o Builder e o TComPort e não tive problemas, funcionou perfeitamente em todas as ocasiões, inclusive envio de muitas informações PC->PIC.

Serial - testarei novamente na sexta

MensagemEnviado: 11 Nov 2006 15:26
por Kleber
Obrigado pela contribuição, sexta-feira dia 17 farei novos testes.
Já me disseram também que provavelmente esqueceram de colocar o retorno de carro ou o enter no envio do caracter...possibilidades de soluções estão surgindo, veremos e postarei aqui o resultado.

Valeu!

Re: Serial - testarei novamente na sexta

MensagemEnviado: 13 Nov 2006 09:37
por andre_luis
Kleber,

Voce usa API do windows para comunicar com a serial ( a exemplo do Rogercom ), ou usa algum package/componente VCL a ser instalado no Builder ?

Se for o 1o caso, já me deparei tambem com esse problema, e posso tentar ajuda-lo.

+++

Segue o código com possível problema

MensagemEnviado: 14 Nov 2006 13:22
por Kleber
Código: Selecionar todos
#include <vcl.h>
#pragma hdrstop

#include "UnSerial.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//int a=0;
HANDLE MinhaPorta;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
        COMMTIMEOUTS tempos;
        DCB configuracao;

        MinhaPorta = CreateFile("COM1",
        GENERIC_READ + GENERIC_WRITE,
                                0,
                                NULL,
                                OPEN_EXISTING,
                                0,
                                NULL);
        if(MinhaPorta == INVALID_HANDLE_VALUE)
        {
                ShowMessage("Porta Serial Não Disponível");
        }
        //Tempo máximo em ms entre byte recebidos
        //0 Desabilita o timeout
        tempos.ReadIntervalTimeout = 0;

        //Tempo máximo por byte recebido em ms
        //0 Desabilita o timeout
        tempos.ReadTotalTimeoutMultiplier = 0;

        //Tempo máximo total por operação de leitura em ms
        //0 Desabilita o timeout
        tempos.ReadTotalTimeoutConstant = 0;

        //Tempo máximo por byte escrito em ms
        //0 Desabilita o timeout
        tempos.WriteTotalTimeoutMultiplier = 0;

        //Tempo máximo por operação de escrita em ms
        //0 Desabilita o timeout
        tempos.WriteTotalTimeoutConstant = 0;

        //Estabelece os timeouts para porta serial no Windows
        SetCommTimeouts(MinhaPorta,&tempos);

        //Le a configuração atual da porta;
        GetCommState(MinhaPorta,&configuracao);

        //Preparação da estrutura DCB
        BuildCommDCB("baud=9600 parity=N data=8 stop=1",&configuracao);

        //Membro da estrutura que contem o seu próprio tamanho
        configuracao.DCBlength = sizeof(DCB);

        //Estabelece a configuração da porta no Windows
        SetCommState(MinhaPorta,&configuracao);

}

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


void __fastcall TForm1::Button1Click(TObject *Sender)
{
        unsigned long escritos;
        unsigned char buffer[10];
        unsigned long lido;

        WriteFile(MinhaPorta,"Q",1,&escritos,NULL);
        ReadFile(MinhaPorta,buffer,1,&lido,NULL);
        if(buffer[0]=='O')
          ShowMessage("Robo Funcionou");
        else
          ShowMessage("Problema");
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
      //  unsigned char buffer[10];
        unsigned long escritos;
        /*if(a=0)
        { */
        Edit1->SetFocus();
        //a=Edit1->Text();
        WriteFile(MinhaPorta,Edit1->Text.c_str(),1,&escritos,NULL);
        Edit1->Text="";
        }
       /* if(buffer[0]=='O')
        a=0;
        else
        a=1; */

Problema resolvido.

MensagemEnviado: 17 Nov 2006 15:51
por Kleber
Código: Selecionar todos
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "UnSerial.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int a=0;
HANDLE MinhaPorta;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
        COMMTIMEOUTS tempos;
        DCB configuracao;

        MinhaPorta = CreateFile("COM1",
        GENERIC_READ + GENERIC_WRITE,
                                0,
                                NULL,
                                OPEN_EXISTING,
                                0,
                                NULL);
        if(MinhaPorta == INVALID_HANDLE_VALUE)
        {
                ShowMessage("Porta Serial Não Disponível");
        }
        //Tempo máximo em ms entre byte recebidos
        //0 Desabilita o timeout
        tempos.ReadIntervalTimeout = 0;

        //Tempo máximo por byte recebido em ms
        //0 Desabilita o timeout
        tempos.ReadTotalTimeoutMultiplier = 0;

        //Tempo máximo total por operação de leitura em ms
        //0 Desabilita o timeout
        tempos.ReadTotalTimeoutConstant = 0;

        //Tempo máximo por byte escrito em ms
        //0 Desabilita o timeout
        tempos.WriteTotalTimeoutMultiplier = 0;

        //Tempo máximo por operação de escrita em ms
        //0 Desabilita o timeout
        tempos.WriteTotalTimeoutConstant = 0;

        //Estabelece os timeouts para porta serial no Windows
        SetCommTimeouts(MinhaPorta,&tempos);

        //Le a configuração atual da porta;
        GetCommState(MinhaPorta,&configuracao);

        //Preparação da estrutura DCB
        BuildCommDCB("baud=9600 parity=N data=8 stop=1",&configuracao);

        //Membro da estrutura que contem o seu próprio tamanho
        configuracao.DCBlength = sizeof(DCB);

        //Estabelece a configuração da porta no Windows
        SetCommState(MinhaPorta,&configuracao);

}

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


void __fastcall TForm1::Button1Click(TObject *Sender)
{
        unsigned long escritos;
        unsigned char buffer[10];
        unsigned long lido;

        WriteFile(MinhaPorta,"Q",1,&escritos,NULL);
        ReadFile(MinhaPorta,buffer,1,&lido,NULL);
        if(buffer[0]=='O')
          ShowMessage("Robo Funcionou");
        else
          ShowMessage("Problema");
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
      //  unsigned char buffer[10];
        unsigned long escritos;
        /*if(a=0)
        { */
        Edit1->SetFocus();
        //a=Edit1->Text();

        if(a==1)
        {
         a=0;
         WriteFile(MinhaPorta,Edit1->Text.c_str(),1,&escritos,NULL);
         Edit1->Text="";
        }
        a=0;


        }
       /* if(buffer[0]=='O')
        a=0;
        else
        a=1; */

//      unsigned long lido;
//      char buffer[10];
//      ReadFile(MinhaPorta,buffer,1,&lido,NULL);
//      if(lido>0)
//      {
//              Memo1->Text = Memo1->Text + buffer[0];
//      }


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



void __fastcall TForm1::Edit1Change(TObject *Sender)
{
a=1;
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Timer2Timer(TObject *Sender)
{
       
        //unsigned char buffer[10];
        //unsigned long lido;

       /*if(buffer[0]=='O')
        a=0;
        else
        a=1;*/

     
}
//---------------------------------------------------------------------------

void __fastcall TForm1::SairClick(TObject *Sender)
{
        /*unsigned long escritos;
      unsigned long lido=0;
      char buffer[10];
      ReadFile(MinhaPorta,buffer,1,&lido,NULL);
      if(lido>0)
      {
              Memo1->Text = Memo1->Text + buffer[0];
              Label1->Caption=Label1->Caption+buffer[0];
      }
      lido=0;*/
       
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Eixo1Click(TObject *Sender)
{
Edit1->Text='q';
}
//---------------------------------------------------------------------------

void __fastcall TForm1::sdfClick(TObject *Sender)
{
 Edit1->Text='p';

}
//---------------------------------------------------------------------------

void __fastcall TForm1::Eixo2Click(TObject *Sender)
{
Edit1->Text='w';       
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Eixo3Click(TObject *Sender)
{
Edit1->Text='e';       
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Eixo4Click(TObject *Sender)
{
Edit1->Text='r';       
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Eixo5Click(TObject *Sender)
{
Edit1->Text='t';       
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button3Click(TObject *Sender)
{
Edit1->Text='l';       
}
//---------------------------------------------------------------------------