本文介绍了通过RS232与其他设备进行串行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.
我想编写一个程序,以通过RS232与其他设备通信.
我编写了此程序(Visual C ++ 6.0),但出现以下错误:


无法在''const char *''
中转换``const int''
这是我的程序.希望您可以给我一些示例程序.



///该程序可以打开,写入和从其他设备读取
///仅用于串行通信,例如RS232


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




int main()
{
CSerial序列号;

//================================================ ==================
//打开特殊的串行端口
//================================================ ==================

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


//================================================ ==================
//设置串行端口
//================================================ ==================

//使用硬件握手设置串行端口(9600,N,8,1)

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


{
如果(serial.Open(1,9600))
AfxMessageBox(端口已经打开");
其他
AfxMessageBox(打开端口失败!");
}
//================================================ ==================
//尝试用硬件编写
//================================================ ==================

serial.Write("???????????");
CSerial序列号;
如果(serial.Open(1,9600))
{
静态char * szMessage [] ="???????";
int nBytesSent = serial.SendData(szMessage,strlen(szMessage));
ASSERT(nBytesSent == strln(szMessage));
}
其他
AfxMessageBox(打开端口失败!");

//如果
//未知的字节数.
serial.SetupReadTimeouts(CSerial :: EReadTimeoutNonblocking);

//================================================ ==================
//尝试从硬件读取
//================================================ ==================
如果(serial.Open(1,9600))
{
char * lpBuffer =新的char [500];
int nBytesRead = serial.ReadData(lpBuffer,500);
delete [] lpBuffer;
}
其他
AfxMessageBoxe(打开端口失败!");

//================================================ ==================
//关闭端口
//================================================ ==================

serial.Close();
返回0;
}

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;
}

推荐答案



<br />
virtual LONG Open (LPCTSTR lpszDevice, DWORD dwInQueue = 0, DWORD dwOutQueue = 0, bool fOverlapped = SERIAL_DEFAULT_OVERLAPPED);<br />



第一个论点是什么?是的... LPCTSTR.
函数Open没有重载.但您使用以下内容:
serial.Open(1,9600)
其中firs参数为"int":)



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" :)


这篇关于通过RS232与其他设备进行串行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 14:42