I need help with this Serial Port communication code.

I am trying to write a program which sends text from a Form's Rich Edit Boxes via 2 serial ports( COM3 and COM 4 have 2 rich edit boxes each for incoming and outgoing text) using Borland C++ Builder 2006. This program uses some C++ serial port communication library code I downloaded. I have also created a buffer for each of them. The serial ports are opened successfully when I run this program. However, I can't get the rich edit boxes to display the texts their serial ports have received.

[code]//---------------------------------------------------------------------------

#include
#pragma hdrstop

#include "serialcomform.h"
#include "rlserial.h"
#include "rldefine.h"
#include "rlthread.h"
#include "rlwthread.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmSerialPort *frmSerialPort;
//---------------------------------------------------------------------------
__fastcall TfrmSerialPort::TfrmSerialPort(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------


void __fastcall TfrmSerialPort::FormDestroy(TObject *Sender)
{
delete mySerial3, mySerial4;
}
//---------------------------------------------------------------------------

void __fastcall TfrmSerialPort::btnsend2COM4Click(TObject *Sender)
{
// mySerial3->writeChar((char)edCOM4->Lines->Text.c_str());

// Assign the pointers to their respective buffers.
// ptr_buf_COM3_Out = buf_COM3_Out;
// ptr_buf_COM4_In = buf_COM4_In;

//*ptr_buf_COM3_Out = (char)edCOM3Out->Lines->Text.c_str();
// ptr_buf_COM4_In = ptr_buf_COM3_Out; //This code only simulates the ports channels in a situation where they were on different computers.


// COM3 writes to COM4's ptr_buf_COM4_In.
*ptr_buf_COM4_In = (unsigned char)edCOM3Out->Lines->Text.c_str();

int buf_COM4_In_byte_size = sizeof(*ptr_buf_COM4_In);

//Write contents of COM4's buffer to edCOM4
if (mySerial3->writeBlock((const unsigned char*)*ptr_buf_COM4_In, buf_COM4_In_byte_size) != 1)
{
//Clear edCOM3Out of it's contents (text) after it has sent it's contents to COM4.
edCOM3Out->Lines->Text = ' ';
}



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

void __fastcall TfrmSerialPort::Timer1Timer(TObject *Sender)
{
// Declaration of the TimeOut Duration (constants).
const unsigned int COMTimeOut = 500;


if (mySerial3->readLine(buf_COM3_In, MAXLENGTH, COMTimeOut) != -1)
{
edCOM3In->Lines->Text = (String) *ptr_buf_COM3_In;
edCOM3In->Lines->Add("
");
}

if (mySerial4->readLine(buf_COM4_In, MAXLENGTH, COMTimeOut) != -1)
{
edCOM4In->Lines->Add(*ptr_buf_COM4_In);
edCOM4In->Lines->Add("
");
}
}
//---------------------------------------------------------------------------

void __fastcall TfrmSerialPort::FormCreate(TObject *Sender)
{
int com3_status, com4_status;
mySerial3 = new rlSerial();
com3_status = mySerial3->openDevice("COM3", 9600, 1, 1, 8, 1, 0);
if ( com3_status == 0)
{ this->lblCOM3_Status->Caption = "COM3: Opening operation successful.";}
else
{ this->lblCOM3_Status->Caption = "COM3: Opening operation failed!";}

mySerial4 = new rlSerial();
com4_status = mySerial4->openDevice("COM4", 9600, 1, 1, 8, 1, 0);
if ( com4_status == 0)
{ this->lblCOM4_Status->Caption = "COM4: Opening operation successful.";}
else
{ this->lblCOM4_Status->Caption = "COM4: Opening operation failed!";}

// Assign the pointers to their respective buffers.
ptr_buf_COM4_In = buf_COM4_In;
ptr_buf_COM3_In = buf_COM3_In;
}
//---------------------------------------------------------------------------

void __fastcall TfrmSerialPort::btnSend2COM3Click(TObject *Sender)
{
// Assign the pointers to their respective buffers.
//ptr_buf_COM4_Out = buf_COM4_Out;
//ptr_buf_COM3_In = buf_COM3_In;

//*ptr_buf_COM4_Out = (char)edCOM4Out->Lines->Text.c_str();
//ptr_buf_COM3_In = ptr_buf_COM4_Out; //This code only simulates the ports' channels exchange between two computers.

// COM4 writes to COM3's ptr_buf_COM3_In.
// int buf_COM3_In_byte_size = sizeof(*ptr_buf_COM3_In);
// mySerial4->writeBlock(ptr_buf_COM3_In, buf_COM3_In_byte_size);

*ptr_buf_COM3_In = (unsigned char)edCOM4Out->Lines->Text.c_str();

int buf_COM3_In_byte_size = sizeof(*ptr_buf_COM3_In);

//Write contents of COM3's buffer to edCOM3In
if (mySerial4->writeBlock((const unsigned char*)*ptr_buf_COM3_In, buf_COM3_In_byte_size) != 1)
{
//Clear edCOM4Out of it's text after it has sent it's contents to COM3.
edCOM4Out->Lines->Text = ' ';
}




}
//---------------------------------------------------------------------------
[/code]
The header file is as follows:
[code]//---------------------------------------------------------------------------

#ifndef serialcomformH
#define serialcomformH
//---------------------------------------------------------------------------
#include
#include
#include
#include
#include
#include "rlserial.h"
#include "rldefine.h"
#include "rlthread.h"
#include "rlwthread.h"
#include
#define MAXLENGTH 512
//---------------------------------------------------------------------------
class TfrmSerialPort : public TForm
{
__published: // IDE-managed Components
TButton *btnsend2COM4;
TButton *btnSend2COM3;
TTimer *Timer1;
TLabel *lblCOM3;
TLabel *lblCOM4;
TRichEdit *edCOM3In;
TRichEdit *edCOM3Out;
TRichEdit *edCOM4In;
TRichEdit *edCOM4Out;
TLabel *lblCOM3In;
TLabel *lblCOM3Out;
TLabel *lblCOM4In;
TLabel *lblCOM4Out;
TLabel *lblCOM3_Status;
TLabel *lblCOM4_Status;
void __fastcall FormDestroy(TObject *Sender);
void __fastcall btnsend2COM4Click(TObject *Sender);
void __fastcall Timer1Timer(TObject *Sender);
void __fastcall FormCreate(TObject *Sender);
void __fastcall btnSend2COM3Click(TObject *Sender);
private: // User declarations

// Instances of Serial Ports (COM3 & COM4)
rlSerial *mySerial3, *mySerial4;

// Arrays are used as original In & Out Buffers for COM3 & COM4
// so that enough memory can be allocated to the buffers.
unsigned char buf_COM3_In[MAXLENGTH], buf_COM4_In[MAXLENGTH];
//unsigned char buf_COM3_Out[MAXLENGTH], buf_COM4_Out[MAXLENGTH];

// Pointers for the buffers - Character arrays cannot be assigned to at run-time
// except for the use of the strcpy function.
//unsigned char *ptr_buf_COM3_Out, *ptr_buf_COM4_Out;
unsigned char *ptr_buf_COM3_In, *ptr_buf_COM4_In;

public: // User declarations
__fastcall TfrmSerialPort(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TfrmSerialPort *frmSerialPort;
//---------------------------------------------------------------------------
#endif[/code]


Please find below the link for the library files.
[link=http://pvbrowser.de/pvbrowser/sf/manual/rllib/html/classrlSerial.html]rlSerial_Libraries[leftbr/link].

Thanks!

NB: I'm sorry if I haven't posted this message according to the forums required format. I have done it to the best of my understanding as this is my first time.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories