Reading Serial Port Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
mglowinsk93
In need of some credit
In need of some credit
Posts: 7
Joined: Sat Jan 13, 2018 5:08 pm

Reading Serial Port

Post by mglowinsk93 »

Hi guys,
I'm trying to write an application which is writing/reading serial port. So far I'm quite good, but got stuck on one point.

I would like to do an automatic refresh of screened data when something will be readed on defined port. Any tip:)? For this moment I did it, but I need to press an button.
Code:

Code: Select all

///-----------------------------------------------------------------
///
/// @file      ArduinoCommunicationFrm.cpp
/// @author    Mateusz
/// Created:   1/13/2018 1:32:43 PM
/// @section   DESCRIPTION
///            ArduinoCommunicationFrm class implementation
///
///------------------------------------------------------------------
#include "ArduinoCommunicationFrm.h"
//Do not add custom headers between
//Header Include Start and Header Include End
//wxDev-C++ designer will remove them
////Header Include Start////Header Include End

//----------------------------------------------------------------------------
// ArduinoCommunicationFrm
//----------------------------------------------------------------------------
//Add Custom Events only in the appropriate block.
//Code added in other places will be removed by wxDev-C++
////Event Table Start
BEGIN_EVENT_TABLE(ArduinoCommunicationFrm,wxFrame)
	////Manual Code Start
	////Manual Code End
	
	EVT_CLOSE(ArduinoCommunicationFrm::OnClose)
	EVT_BUTTON(ID_WXBUTTON3,ArduinoCommunicationFrm::WxButton3Click)
	EVT_BUTTON(ID_WXBUTTON2,ArduinoCommunicationFrm::WxButton2Click)
	
	EVT_TEXT(ID_WXMEMO1,ArduinoCommunicationFrm::WxMemo1Updated)
	EVT_BUTTON(ID_WXBUTTON1,ArduinoCommunicationFrm::WxButton1Click)
END_EVENT_TABLE()
////Event Table End

ArduinoCommunicationFrm::ArduinoCommunicationFrm(wxWindow *parent, wxWindowID id, const wxString &title, const wxPoint &position, const wxSize& size, long style)
: wxFrame(parent, id, title, position, size, style)
{
	CreateGUIControls();
}

ArduinoCommunicationFrm::~ArduinoCommunicationFrm()
{
}

void ArduinoCommunicationFrm::CreateGUIControls()
{
	//Do not add custom code between
	//GUI Items Creation Start and GUI Items Creation End
	//wxDev-C++ designer will remove them.
	//Add the custom code before or after the blocks
	////GUI Items Creation Start

	WxPanel1 = new wxPanel(this, ID_WXPANEL1, wxPoint(0, 0), wxSize(417, 365));

	WxButton1 = new wxButton(WxPanel1, ID_WXBUTTON1, _("Wyslij dane do\n mikrokontrolera"), wxPoint(7, 311), wxSize(117, 43), 0, wxDefaultValidator, _("WxButton1"));

	WxMemo1 = new wxTextCtrl(WxPanel1, ID_WXMEMO1, wxEmptyString, wxPoint(-1, 85), wxSize(417, 124), wxTE_MULTILINE, wxDefaultValidator, _("WxMemo1"));
	WxMemo1->SetMaxLength(0);
	WxMemo1->SetFocus();
	WxMemo1->SetInsertionPointEnd();

	WxStaticText1 = new wxStaticText(WxPanel1, ID_WXSTATICTEXT1, _("WPISZ DANE DO WYSLANIA DO MIKROKONTROLERA : "), wxPoint(0, 66), wxDefaultSize, 0, _("WxStaticText1"));

	WxStaticText2 = new wxStaticText(WxPanel1, ID_WXSTATICTEXT2, _("DANE ODEBRANE Z MIKROKONTROLERA :"), wxPoint(0, 209), wxDefaultSize, 0, _("WxStaticText2"));

	WxButton2 = new wxButton(WxPanel1, ID_WXBUTTON2, _("Odbierz dane z\n mikrokontrolera"), wxPoint(270, 314), wxSize(119, 47), 0, wxDefaultValidator, _("WxButton2"));

	WxButton3 = new wxButton(WxPanel1, ID_WXBUTTON3, _("Polacz z COM3"), wxPoint(3, 5), wxSize(105, 42), 0, wxDefaultValidator, _("WxButton3"));

	WxStaticText3 = new wxStaticText(WxPanel1, ID_WXSTATICTEXT3, _("Status Polaczenia"), wxPoint(132, 5), wxDefaultSize, 0, _("WxStaticText3"));

	WxStaticText4 = new wxStaticText(WxPanel1, ID_WXSTATICTEXT4, _("Tutaj sie pojawia"), wxPoint(2, 229), wxDefaultSize, 0, _("WxStaticText4"));

	SetTitle(_("ArduinoCommunication"));
	SetIcon(wxNullIcon);
	SetSize(8,8,434,403);
	Center();
	
	////GUI Items Creation End
}
/*Zmienne do polaczenia sie z mikrokontrolerem*/
#include "SerialClass.h"
Serial* SP =0;
char incomingData[256] = "";			
int dataLength = 255;
int readResult = 0;

void ArduinoCommunicationFrm::OnClose(wxCloseEvent& event)
{
    delete SP;
	Destroy();
}

/*
 * WxMemo1Updated
 */
void ArduinoCommunicationFrm::WxMemo1Updated(wxCommandEvent& event)
{
	// insert your code here
}

/*
 * WxButton1Click
 */
void ArduinoCommunicationFrm::WxButton1Click(wxCommandEvent& event)
{
	SP->WriteData(WxMemo1->GetValue(), WxMemo1->GetValue().size());
}

/*
 * WxButton3Click
 */
void ArduinoCommunicationFrm::WxButton3Click(wxCommandEvent& event)
{
SP =0;
 SP = new Serial("COM3"); 
  if (SP->IsConnected())
	  {WxStaticText3->SetLabel("Polaczono");}
      else
      {WxStaticText3->SetLabel("Error");}
		
}

/*
 * WxButton2Click
 */
void ArduinoCommunicationFrm::WxButton2Click(wxCommandEvent& event)
{
	if(SP->IsConnected())
	{
        
		readResult = SP->ReadData(incomingData,dataLength);
	
                incomingData[readResult] = 0;
                WxStaticText4->SetLabel(incomingData[readResult]);
        	printf("%s",incomingData);

		Sleep(500);
		
	}
}



Thanks for help in advance,
Mateusz
Attachments
Aplikacja.png
Aplikacja.png (28.63 KiB) Viewed 2995 times
User avatar
eranon
Can't get richer than this
Can't get richer than this
Posts: 867
Joined: Sun May 13, 2012 11:42 pm
Location: France
Contact:

Re: Reading Serial Port

Post by eranon »

Hello,

It all depends on your Serial class, if it emits an event on receive or not. It there's an event, you just handle it. If there's no event, you have to go through a timer...

Maybe of some help: https://www.codeproject.com/Articles/81 ... s-in-wxWid
[Ind. dev. - wxWidgets 3.0/3.1 under "Win 7 64-bit, TDM64-GCC" + "OS X 10.9, LLVM Clang"]
mglowinsk93
In need of some credit
In need of some credit
Posts: 7
Joined: Sat Jan 13, 2018 5:08 pm

Re: Reading Serial Port

Post by mglowinsk93 »

Hi Eranon,
thank you a lot! I solved a problem with wxTimers.

Regards,
Mateusz
Post Reply