//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "mojrs.h"
#include <string.h>
#include <stdio.h>
#include <iostream>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#define cbOutQueue 16 //rozmiar bufora danych wyjściowych
#define cbInQueue 16 //rozmiar bufora danych wejściowych
TForm3 *Form3;
// Add these global variables
char Buffer_O[cbOutQueue]; // bufor danych wyjściowych
char Buffer_I[cbInQueue]; // bufor danych wejściowych
DWORD Number_Bytes_Read; // liczba bajtów do czytania
AnsiString Str;
int I;
String pp;
static DCB dcb;
HANDLE hComm;
char lpBuffor_read[33], lpBuffor_write[33];
DWORD RS_ile;
bool abContinue = true;
int isRead = false;
bool CommFlag;
DWORD fdwEvtMask; // informacja o aktualnym stanie transmisji
COMSTAT Stat; // dodatkowa informacja o zasobach portu
DWORD Errors; // reprezentuje typ ewentualnego błędu
//---------------------------------------------------------------------------
__fastcall TForm3::TForm3(TComponent* Owner)
: TForm(Owner)
{
}
int __fastcall Read_Comm(HANDLE hComm,
LPDWORD lpNumberOfBytesRead, DWORD Buf_Size)
{
DWORD nNumberOfBytesToRead;
ClearCommError(hComm, &Errors ,&Stat);
if (Stat.cbInQue > 0)
{
if (Stat.cbInQue > Buf_Size)
nNumberOfBytesToRead = Buf_Size;
else
nNumberOfBytesToRead = Stat.cbInQue;
ReadFile(hComm, &Buffer_I[0], nNumberOfBytesToRead,
lpNumberOfBytesRead, NULL);
}
else
*lpNumberOfBytesRead = 0;
return TRUE;
}
//---------------------------------------------------------------------------
void __fastcall TForm3::Button1Click(TObject *Sender)
{
hComm = CreateFile( "COM2",
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
0,
0);
dcb.DCBlength = sizeof( dcb );
dcb.BaudRate = CBR_9600;
dcb.fParity = FALSE;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.ByteSize = 8;
dcb.fDtrControl = DTR_CONTROL_ENABLE;
dcb.fRtsControl = DTR_CONTROL_ENABLE;
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.fDsrSensitivity = FALSE;
dcb.fAbortOnError = FALSE;
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
dcb.fErrorChar = FALSE;
dcb.fNull = FALSE;
if( SetCommState( hComm, & dcb ) ) MessageBox( NULL, "Udało się połączyć z portem COM1", "Inf", MB_ICONINFORMATION );
else MessageBox( NULL, "Nie udało się połączyć z portem COM1", "Błąd", MB_ICONERROR );
if (hComm == INVALID_HANDLE_VALUE) {
RichEdit1->Lines->Add("Could not open comm port.");
return;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm3::Button2Click(TObject *Sender)
{
//unsigned char hexx[6]={0x01,0x01,0x00,0x13,0x00,0x25};
const char hexx[]="\x01\x01\x00\x13\x00\x25";
char hex[8] = {0x01,0x03,0x00,0x00,0x00,0x03,0x05,0xCB};
char hexo[6] = { '2', '5', '8' };
AnsiString S2(hexo);
//char hex[3]= "\x01\x03\x06";
//strcpy(lpBuffor_write, &hex[3]);// Niech to będzie przykładowe polecenie dla urządzenia
//WriteFile(hComm, lpBuffor_write, strlen(lpBuffor_write), &RS_ile, 0);
WriteFile(hComm, hex, sizeof hex, &RS_ile, 0);
}
//---------------------------------------------------------------------------
void __fastcall TForm3::Button3Click(TObject *Sender)
{
DWORD BytesRead;
while(CommFlag) {
Application->ProcessMessages();
ReadFile(hComm, lpBuffor_read, 15, &RS_ile, NULL);
if(RS_ile) {
lpBuffor_read[BytesRead] = 0;
//Memo1->Text=lpBuffor_read;
//Label1->Caption=lpBuffor_read[10];
//Edit1->Text=lpBuffor_read;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm3::Button4Click(TObject *Sender)
{
Label1->Caption=ReadFile(hComm, lpBuffor_read, 15, &RS_ile, NULL);
}
//---------------------------------------------------------------------------
void __fastcall TForm3::Timer1Timer(TObject *Sender)
{
//Write_Comm(hCommDev, strlen(Buffer_O));
Sleep(100);
//Beep();
FlushFileBuffers(hComm);
Read_Comm(hComm, &Number_Bytes_Read, sizeof(Buffer_I));
if (Number_Bytes_Read > 0)
for(int i=0;i<7;++i) Str+=Format("%0.2X ",Buffer_I[I]);
Label1->Caption=Trim(Str);
// RichEdit1->Text = Buffer_I;
//Edit1->Text = Buffer_I[1];
// Edit1->Text = S2;
//Label1->Caption=sizeof(Buffer_I);
// Label1->Caption + IntToHex(Buffer_I,2);
//Buffer_I[17]=0;
//Label1->Caption=AnsiString(Buffer_I);
// RichEdit1->Text=Label1->Caption;
//Label2->Caption=S2;
}
//---------------------------------------------------------------------------
W
void __fastcall TForm3::Button2Click(TObject *Sender)
wysyłam ramkę do RS232, a w
void __fastcall TForm3::Timer1Timer(TObject *Sender)
chcę pobrac dane z Buffer_I i wświetlić w formie hexadecymalnej np Label.
dodanie znaczników <code class="cpp">
- @furious programming