Sterownik - Kopia.zip

Re: [Atmega16] [C++/WinAPI/BASCOM] [RS232/UART] - Aplikacja w C++ i WINAPI

Odkryłem coś ciekawego, mianowicie gdy uruchomię swój program, on się poprawnie wykona i napiszę coś w terminalu, to AVR "dokleja" mi do zwróconej wiadomości literę 'w', którą wysyłam programem w C++. Żeby zobrazować to zjawisko, zamieszę tekst z terminala: cos wcos UPDATE: ----------------------------------------------------------------- Wygląda na to, że się udało. Program przeniosłem do z DevC++ do C++ Buildera, żebym lepiej widział działanie programu. Docelowo miał być ukończony w Builderze. W terminalu wyświetliłem sobie dane w hexach i ujrzałem kod <0D>, który przenosił kursor do nowej linijki, także po każdej transmisji znaku/tekstu wysyłam tenże znak w heksie: void __fastcall TForm1::Button1Click(TObject *Sender) { data = 0x65; ///bufor zawiera literę "e" Label7->Caption = WriteUart(data, 1); //wysyłam bufor, Label7 pokazuje mi, czy funkcja zwróciła 1, taka kontrolka :) data = 0x0D; //bufor zawiera znak 0D Label7->Caption = WriteUart(data, 1); //i też go wysyłam } Po tym zabiegu po kliknięciu przycisku, w polu tekstowym odbieram bez problemu wysłany znak, teraz pobawię się w C++ i myślę, że jak mi się uda napisać program, to zamieszczę go tutaj, razem z kodem :) Pozdrawiam UPDATE: ----------------------------------------------------------------- Zgodnie z obietnicą przesyłam swój program napisany w C++ Builder (początkowa wersja, ale pewnie się komuś przyda)

  • Sterownik - Kopia.zip
    • Project1.~cpp
    • Uart.cpp
    • Unit1.h
    • Unit1.obj
    • Unit1.~cpp
    • Project1.cpp
    • Project1.exe
    • Project1.res
    • Project1.bpr
    • Unit1.~dfm
    • Unit1.cpp
    • Uart.~h
    • Unit1.~h
    • Project1.~bpr
    • Project1.tds
    • Project1.obj
    • Uart.h
    • Unit1.dfm


Pobierz plik - link do postu

Sterownik - Kopia.zip > Uart.cpp

#include &quot; UART.h &quot;

DCB PortDCB;
COMMTIMEOUTS CommTimeouts;
HANDLE hPort;


void SerialTest() { //temp routine for testing.
unsigned char data[10];

data[0] = 0x01;
data[1] = 0x02;
data[2] = 0x03;
data[3] = 0x04;

WriteUart(data, 4);
}


/****************************************************************************
* SetupUart()
*
* This function initialises the serial port and opens it ready to send and
* receive data.
*
* The default setup is: (define in Uart.h)
* COM1 9600,8,1,NOPARITY
*
* Usage for default setup: SetupUart();
* Or usage for specific parameters: SetupUart( &quot; COM2 &quot; ,2400,8,1,NOPARITY);
*
* Parity can be: Bitsize can be:
* EVENPARITY 5 - 8
* MARKPARITY
* NOPARITY
* ODDPARITY
* SPACEPARITY
*
* Will return 0 if the function fails
******************************************************************************/
int SetupUart(char *Port, int baud, int Bitsize, int StopBits, int Parity) {
int STOPBITS;

if(StopBits == 1) STOPBITS = ONESTOPBIT;
// if(StopBits == 1.5) STOPBITS = ONE5STOPBITS;
if(StopBits == 2) STOPBITS = TWOSTOPBITS;

// Open the serial port.
hPort = CreateFile(TEXT(Port), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

// If it fails to open the port, return 0.
if (hPort == INVALID_HANDLE_VALUE) {
return 0;
}

//Get the default port setting information.
GetCommState (hPort, &amp; PortDCB);

// Change the settings.
PortDCB.BaudRate = baud; // BAUD Rate
PortDCB.ByteSize = Bitsize; // Number of bits/byte, 5-8
PortDCB.Parity = Parity; // 0-4 = no, odd, even, mark, space
PortDCB.StopBits = STOPBITS; // StopBits
PortDCB.fNull = 0; // Allow NULL Receive bytes

// Re-configure the port with the new DCB structure.
if (!SetCommState(hPort, &amp; PortDCB)) {
CloseHandle(hPort);

return 0;
}

// Retrieve the time-out parameters for all read and write operations
// on the port.
GetCommTimeouts(hPort, &amp; CommTimeouts);
memset( &amp; CommTimeouts, 0x00, sizeof(CommTimeouts));

CommTimeouts.ReadIntervalTimeout = 10;
CommTimeouts.ReadTotalTimeoutConstant = 10;
CommTimeouts.WriteTotalTimeoutConstant = 10;

// Set the time-out parameters for all read and write operations on the port.
if (!SetCommTimeouts(hPort, &amp; CommTimeouts)) {
CloseHandle(hPort);

return 0;
}

// Clear the port of any existing data.
if (PurgeComm(hPort, PURGE_TXCLEAR | PURGE_RXCLEAR) == 0) {
CloseHandle(hPort);

return 0;
}

return 1; //SERIAL SETUP OK
}

/****************************************************************************
* WriteUart()
*
* This function Writes data to the serial for receiving device
*
* Input: *buf - pointer to unsigned char array containing data
* len - length of data to be sent
*
* Usage: WriteUart( &quot; HELLO COMS &quot; ,strlen( &quot; HELLO COMS &quot; ));
*
* Will return 0 if the function fails
******************************************************************************/
int WriteUart(unsigned char *buf, int len) {
DWORD dwNumBytesWritten;

WriteFile(hPort, buf, len, &amp; dwNumBytesWritten, NULL);

if (dwNumBytesWritten &amp; gt; 0)
return 1; //Transmission was success
else
return 0; //Error transmitting?
}

/****************************************************************************
* ReadUart()
*
* This function reads data from the serial port
*
* Input: *buf - pointer to unsigned char which will contain the received data
* len - size of buffer for receiving data
*
* Usage: ReadUart(indatabuffer,sizeof(indatabuffer));
*
* Will return 0 if no data available
******************************************************************************/
int ReadUart(unsigned char *buf, int len) {
BOOL ret;
unsigned long retlen;

ret = ReadFile(hPort, buf, len, &amp; retlen, NULL);

if (retlen &amp; gt; 0) //If we have data
return (int)retlen; //return the length
else
return 0; //else no data has been read
}

/****************************************************************************
* CloseUart()
*
* Closes connection to the serial port
*
******************************************************************************/
int CloseUart() {
CloseHandle(hPort);

return 1;
}


int main(){
//string UART_data = &quot; 10 &quot; ;

//SetupUart( &quot; COM6 &quot; ,2400,8,1,NOPARITY);

//WriteUart(UART_data, lstrlen(UART_data));

}


Sterownik - Kopia.zip > Unit1.h

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

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include &amp; lt; Classes.hpp &amp; gt;
#include &amp; lt; Controls.hpp &amp; gt;
#include &amp; lt; StdCtrls.hpp &amp; gt;
#include &amp; lt; Forms.hpp &amp; gt;
#include &amp; lt; ExtCtrls.hpp &amp; gt;
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TButton *Button2;
TButton *Button3;
TButton *Button4;
TButton *Button5;
TTimer *Timer1;
TLabel *Label1;
TLabel *Label2;
TButton *Button6;
TLabel *Label3;
TLabel *Label4;
TLabel *Label6;
TLabel *Label8;
TLabel *Label9;
TShape *Shape1;
TShape *Shape2;
void __fastcall Button6Click(TObject *Sender);
void __fastcall FormCreate(TObject *Sender);
void __fastcall Timer1Timer(TObject *Sender);
void __fastcall FormClose(TObject *Sender, TCloseAction &amp; Action);
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif


Sterownik - Kopia.zip > Project1.cpp

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

#include &amp; lt; vcl.h &amp; gt;
#pragma hdrstop
//---------------------------------------------------------------------------
USEFORM( &quot; Unit1.cpp &quot; , Form1);
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
Application- &amp; gt; Initialize();
Application- &amp; gt; CreateForm(__classid(TForm1), &amp; Form1);
Application- &amp; gt; Run();
}
catch (Exception &amp; exception)
{
Application- &amp; gt; ShowException( &amp; exception);
}
catch (...)
{
try
{
throw Exception( &quot; &quot; );
}
catch (Exception &amp; exception)
{
Application- &amp; gt; ShowException( &amp; exception);
}
}
return 0;
}
//---------------------------------------------------------------------------


Sterownik - Kopia.zip > Unit1.cpp

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

#include &amp; lt; vcl.h &amp; gt;
#include &quot; UART.h &quot;
#pragma hdrstop

#include &quot; Unit1.h &quot;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource &quot; *.dfm &quot;
TForm1 *Form1;


DCB PortDCB;
COMMTIMEOUTS CommTimeouts;
HANDLE hPort;
bool connected;

char znak_odebrany;
int licznik, transmisja;
unsigned char data[32];
unsigned char data_resp[32];

int SetupUart(char *Port, int baud, int Bitsize, int StopBits, int Parity) {
int STOPBITS;

if(StopBits == 1) STOPBITS = ONESTOPBIT;
// if(StopBits == 1.5) STOPBITS = ONE5STOPBITS;
if(StopBits == 2) STOPBITS = TWOSTOPBITS;

// Open the serial port.
hPort = CreateFile(TEXT(Port), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

// If it fails to open the port, return 0.
if (hPort == INVALID_HANDLE_VALUE) {
return 0;
}

//Get the default port setting information.
GetCommState (hPort, &amp; PortDCB);

// Change the settings.
PortDCB.BaudRate = baud; // BAUD Rate
PortDCB.ByteSize = Bitsize; // Number of bits/byte, 5-8
PortDCB.Parity = Parity; // 0-4 = no, odd, even, mark, space
PortDCB.StopBits = STOPBITS; // StopBits
PortDCB.fNull = 0; // Allow NULL Receive bytes
PortDCB.fDtrControl = DTR_CONTROL_DISABLE;
PortDCB.fRtsControl = RTS_CONTROL_DISABLE;
// Re-configure the port with the new DCB structure.
if (!SetCommState(hPort, &amp; PortDCB)) {
CloseHandle(hPort);

return 0;
}

// Retrieve the time-out parameters for all read and write operations
// on the port.
GetCommTimeouts(hPort, &amp; CommTimeouts);
memset( &amp; CommTimeouts, 0x00, sizeof(CommTimeouts));

CommTimeouts.ReadIntervalTimeout = 10;
CommTimeouts.ReadTotalTimeoutConstant = 10;
CommTimeouts.WriteTotalTimeoutConstant = 10;

// Set the time-out parameters for all read and write operations on the port.
if (!SetCommTimeouts(hPort, &amp; CommTimeouts)) {
CloseHandle(hPort);

return 0;
}

// Clear the port of any existing data.
if (PurgeComm(hPort, PURGE_TXCLEAR | PURGE_RXCLEAR) == 0) {
CloseHandle(hPort);

return 0;
}

return 1; //SERIAL SETUP OK
}

/****************************************************************************
* WriteUart()
*
* This function Writes data to the serial for receiving device
*
* Input: *buf - pointer to unsigned char array containing data
* len - length of data to be sent
*
* Usage: WriteUart( &quot; HELLO COMS &quot; ,strlen( &quot; HELLO COMS &quot; ));
*
* Will return 0 if the function fails
******************************************************************************/
int WriteUart(unsigned char *buf, int len) {
DWORD dwNumBytesWritten;

WriteFile(hPort, buf, len, &amp; dwNumBytesWritten, NULL);
buf[len] = 0; //konczymy string
// buf; //wyswietlamy pojedynczy odebrany znak
if (dwNumBytesWritten &amp; gt; 0)
return 1; //Transmission was success
else
return 0; //Error transmitting?
}

/****************************************************************************
* ReadUart()
*
* This function reads data from the serial port
*
* Input: *buf - pointer to unsigned char which will contain the received data
* len - size of buffer for receiving data
*
* Usage: ReadUart(indatabuffer,sizeof(indatabuffer));
*
* Will return 0 if no data available
******************************************************************************/
int ReadUart(unsigned char *buf, int len) {
BOOL ret;
unsigned long retlen;

ret = ReadFile(hPort, buf, len, &amp; retlen, NULL);
//buf[retlen] = 0; //konczymy string
//cout &amp; lt; &amp; lt; buf; //wyswietlamy pojedynczy odebrany znak

if (retlen &amp; gt; 0) //If we have data
return (int)retlen; //return the length
//return buf;
else
return 0; //else no data has been read
}


int PrintReadUart(unsigned char *buf, int len) {
BOOL ret;
unsigned long retlen;

ret = ReadFile(hPort, buf, len, &amp; retlen, NULL);
buf[retlen] = 0; //konczymy string


if (retlen &amp; gt; 0){ //If we have data
znak_odebrany = *buf; //wyswietlamy pojedynczy odebrany znak
//return buf;
return 1;
} else {
znak_odebrany = 0;
return 0; //else no data has been read
}
}

/****************************************************************************
* CloseUart()
*
* Closes connection to the serial port
*
******************************************************************************/
int CloseUart() {
CloseHandle(hPort);

return 1;
}


//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button6Click(TObject *Sender)
{
if(!connected){
if(SetupUart( &quot; COM6 &quot; ,1200,8,1,NOPARITY)){
Button6- &amp; gt; Caption = &quot; Rozlacz &quot; ;
connected = true;
Label3- &amp; gt; Caption = &quot; Podlaczony &quot; ;
}
} else {
CloseUart();
Button6- &amp; gt; Caption = &quot; Podlacz &quot; ;
Label3- &amp; gt; Caption = &quot; Rozlaczony &quot; ;
connected = false;

}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
connected = false;
licznik = 0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
if(connected){
PrintReadUart(data_resp, 1);
if(znak_odebrany &amp; gt; 0){

if(licznik &amp; gt; = 10){
Label2- &amp; gt; Caption = &quot; &quot; ;
licznik=0;
}
if(licznik &amp; lt; 10){
Label2- &amp; gt; Caption = Label2- &amp; gt; Caption+znak_odebrany;
licznik++;
}
}

if(PrintReadUart(data_resp, 1))
{
Shape1- &amp; gt; Brush- &amp; gt; Color = clLime;
} else {
Shape1- &amp; gt; Brush- &amp; gt; Color = clGreen;
}




Shape2- &amp; gt; Brush- &amp; gt; Color = clMaroon;
Label9- &amp; gt; Caption = licznik;

}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &amp; Action)
{
if(connected) CloseUart();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
data[0] = 0x65;

if(WriteUart(data, 1))
Shape2- &amp; gt; Brush- &amp; gt; Color = clRed;


data[0] = 0x0D;
if(WriteUart(data, 1))
Shape2- &amp; gt; Brush- &amp; gt; Color = clRed;


}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
data[0] = 0x64;
if(WriteUart(data, 1))
Shape2- &amp; gt; Brush- &amp; gt; Color = clRed;
}
//---------------------------------------------------------------------------


Sterownik - Kopia.zip > Uart.h

#include &amp; lt; windows.h &amp; gt;
#include &amp; lt; commctrl.h &amp; gt;

int SetupUart(char Port[5], int baud=9600, int Bitsize=8, int StopBits=1, int Parity=NOPARITY);
int WriteUart(unsigned char *buf, int len);
int ReadUart(unsigned char *buf, int len);
int CloseUart();