Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi everybody.
I want to write a program to communicate with other device with RS232.
I wrote this program (Visual C++ 6.0)but i become following mistakes:


can not converting 'const int' in 'const char *'

this is my Program.I hope that you can send me some sample Programs.



/// This Program can just open,write and read from other device
/// just for Serial Communication for example RS232


#define STRICT
#include <tchar.h>
#include <windows.h>
#include "Serial.h"




int main( )
{
CSerial serial;

//==================================================================
// To Open a special Serial Port
//==================================================================

serial.Open(_T("COM1"));


//==================================================================
// To setting a Serial Port
//==================================================================

// Setup the serial port (9600,N,8,1) using hardware handshaking

serial.Setup(CSerial::EBaud9600,CSerial::EData8,CSerial::EParNone,CSerial::EStop1);
serial.SetupHandshaking(CSerial::EHandshakeHardware);


{
if (serial.Open(1,9600))
AfxMessageBox("port is already open");
else
AfxMessageBox("Open Port is Faild!!!");
}
//==================================================================
// Try to write in hardware
//==================================================================

serial.Write("???????????");
CSerial serial;
if (serial.Open(1,9600))
{
static char* szMessage[] = "???????";
int nBytesSent = serial.SendData(szMessage,strlen(szMessage));
ASSERT(nBytesSent==strln(szMessage));
}
else
AfxMessageBox("Open Port is Faild!!!");

// The "non-blockimg" Syntax can be also used if the
// nummber of byts are not known.
serial.SetupReadTimeouts(CSerial::EReadTimeoutNonblocking);

//==================================================================
// Try to Read from hardware
//==================================================================
if (serial.Open(1,9600))
{
char* lpBuffer = new char[500];
int nBytesRead = serial.ReadData(lpBuffer,500);
delete[]lpBuffer;
}
else
AfxMessageBoxe("Open Port is Faild!!!");

//==================================================================
// Close the Port
//==================================================================

serial.Close();
return 0;
}
Posted

Do you use CSerial from
here [^]? If yes - there is no function "open(int, int)"
 
Share this answer
 
v2
He means there is no Open function that can support the serial.Open(1,9600) call in the code.
 
Share this answer
 
v2
pay attention to the declaration of function Open:
<br />
virtual LONG Open (LPCTSTR lpszDevice, DWORD dwInQueue = 0, DWORD dwOutQueue = 0, bool fOverlapped = SERIAL_DEFAULT_OVERLAPPED);<br />


what is the first argument? yea... LPCTSTR.
there is no overloading for function Open. but you use the following:
serial.Open(1,9600)
where the firs argument is "int" :)
 
Share this answer
 
there is a nice example with commented lines too:

<br />
// Attempt to open the serial port (COM1)<br />
<br />
serial.Open(_T("COM1"));<br />
 
Share this answer
 
that was not a command... and that was not mine... that was a declaration of the function "Open" in Serial.h file. It supposed, that you should use this function according to its declaration.

simply replace "serial.Open(1,9600)" to "serial.Open(_T("COM1"))" in the code you have posted. There should be no error for this function... but there are a lot of other errors.

It seems to me, that you are very new in c++. So it'll be better if you learn a bit about c++ :)
 
Share this answer
 
v3
Yes i use this Site:http://www.codeproject.com/KB/system/serial.aspx

But i didnt understand what you mean with Open(int,int)?
What should i write and where?
 
Share this answer
 
Thank u Jast_in

I ve use your Command:

virtual LONG Open (LPCTSTR lpszDevice, DWORD dwInQueue = 0, DWORD dwOutQueue = 0, bool fOverlapped = SERIAL_DEFAULT_OVERLAPPED);
int ShowError (LONG lError, LPCTSTR lptszMessage)

and i become this error:

'Open' : just Member-Functions and Base class can be virtual
'Open' : converting Parameter 1 from 'const int' to 'const char *' not possible

please sorry because i must translate the errors from German to English
 
Share this answer
 
Ok understanding but how should i open my serial port?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900