导读:
--------------------------------------------------------------------------------
需要采用二进制方式通信,看参考书上VC里可以用CByteArray,>>COleVariant等数据类型,方便的采用二进制方式通信,C++ Builder似乎没有这些类型吧?按照VC的方法试过了不行MSDN里介绍的也是VC里的一些方法,没有这方面的资料,哪为达人有,介绍一下。。。。。谢谢了
--------------------------------------------------------------------------------
二进制?
我给你个例子,也许对你有帮助。
//串口打印函数
void __fastcall TfrmMain::MSCPrint( AnsiString DetailText )
{
String s = DetailText;
if( MSComm->PortOpen == false )
MSComm->PortOpen = true;
MSComm->Output = StringToOleStr( AnsiString(char(28)) + AnsiString(char(38)) ); //输出中文
MSComm->Output = StringToOleStr( DetailText );
MSComm->Output = StringToOleStr( AnsiString(char(10) ));
MSComm->Output = StringToOleStr( AnsiString(char(10) ));
MSComm->Output = StringToOleStr( AnsiString(char(10) ));
MSComm->Output = StringToOleStr( AnsiString(char(10) ));
MSComm->Output = StringToOleStr( AnsiString(char(10) ));
MSComm->Output = StringToOleStr( AnsiString(char(10) ));
MSComm->Output = StringToOleStr( AnsiString(char(10) ));
MSComm->Output = StringToOleStr( AnsiString(char(10) ));
MSComm->Output = StringToOleStr( AnsiString(char(27)) + AnsiString(char(109)) );
}
//---------------------------------------------------------------------------
以前做过这方面的程序,给你个代码段参考一下:
OleVariant RxBuff;
int bytenum;
char c1;
AnsiString st1; //保存串口传送来的字符
if(MSComm1->InBufferCount>0 &&MSComm1->InputMode==1)
{
RxBuff=MSComm1->Input;
bytenum=RxBuff.ArrayHighBound(1);
int i=0;
while(i<=bytenum)
{
c1=RxBuff.GetElement(i);
st1=st1+c1;
i++;
}
}
--------------------------------------------------------------------------------
更正一下:
OleVariant RxBuff;
int bytenum;
char c1;
AnsiString st1=""; //保存串口传送来的字符
if(MSComm1->InBufferCount>0 &&MSComm1->InputMode==1)
{
RxBuff=MSComm1->Input;
bytenum=RxBuff.ArrayHighBound(1);
int i=0;
while(i {
c1=RxBuff.GetElement(i);
st1=st1+c1;
i++;
}
}
--------------------------------------------------------------------------------
if (MSComm->PortOpen)
return;
MSComm->CommPort= 2;
MSComm->Settings= "115200, n, 8, 1";
MSComm->InBufferSize= 4096;
MSComm->OutBufferSize= 2048;
MSComm->RThreshold= 1;
MSComm->SThreshold= 1;
MSComm->InputMode= comInputModeBinary;
try {
MSComm->PortOpen= true;
MSComm->InputLen= 0;
MSComm->InBufferCount= 0;
MSComm->OutBufferCount= 0;
ShowStatueMessage(1);
Log(1, "连接成功");
}
catch (...){
ShowStatueMessage(0);
}
..........
//---------------------------------------------------------------------------
void __fastcall TFrmMain::MSCommComm(TObject *Sender)
{
char szBuf[RxBUFFERSIZE]={0};
int i, j;
OleVariant RxBuff;
switch (MSComm->CommEvent)
{
/* case comEvSend:
ShowMessage("Send!!!!");
case comEvCTS:
ShowMessage("CTS!!!!");
case comEvDSR:
ShowMessage("DSR!!!!");
case comEvCD:
ShowMessage("CD!!!!");
*/
///*
case comEvReceive:
if (MSComm->InBufferCount >0) {
RxBuff= MSComm->Input;
j= RxBuff.ArrayHighBound(1);
for (i=0; i<=j; i++)
szBuf= RxBuff.GetElement(i);
szBuf= NULL;
#ifdef _DEBUG
RichEdit1->Lines->Add(AnsiString(szBuf));
#endif
if (strstr(szBuf, "+CMTI") != 0) // 收到短消息了
{
// ShowMessage("来短消息了");
ProcessSMS(szBuf);
}
}
break;
//*/
case comEvRing:
ShowMessage("来电话了!!!!");
break;
}
}
你可能主要的麻烦在于 VC和BCB对于 OleVariant支持的不同。
--------------------------------------------------------------------------------
DWORD __fastcall TFrmMain::WriteComm(char *aTxBuff, int BuffSize)
{
OleVariant TxBuff;
int i;
if (MSComm->PortOpen) {
if (BuffSize >0) {
TxBuff= VarArrayCreate(OPENARRAY(int, (0, BuffSize-1)), varByte);
for (i=0; i TxBuff.PutElement(aTxBuff, i);
MSComm->Output= TxBuff;
}
}
return 0;
}
本文转自
http://soa.5d6d.com/redirect.php?fid=11&tid=47&goto=nextnewset
--------------------------------------------------------------------------------
需要采用二进制方式通信,看参考书上VC里可以用CByteArray,>>COleVariant等数据类型,方便的采用二进制方式通信,C++ Builder似乎没有这些类型吧?按照VC的方法试过了不行MSDN里介绍的也是VC里的一些方法,没有这方面的资料,哪为达人有,介绍一下。。。。。谢谢了
--------------------------------------------------------------------------------
二进制?
我给你个例子,也许对你有帮助。
//串口打印函数
void __fastcall TfrmMain::MSCPrint( AnsiString DetailText )
{
String s = DetailText;
if( MSComm->PortOpen == false )
MSComm->PortOpen = true;
MSComm->Output = StringToOleStr( AnsiString(char(28)) + AnsiString(char(38)) ); //输出中文
MSComm->Output = StringToOleStr( DetailText );
MSComm->Output = StringToOleStr( AnsiString(char(10) ));
MSComm->Output = StringToOleStr( AnsiString(char(10) ));
MSComm->Output = StringToOleStr( AnsiString(char(10) ));
MSComm->Output = StringToOleStr( AnsiString(char(10) ));
MSComm->Output = StringToOleStr( AnsiString(char(10) ));
MSComm->Output = StringToOleStr( AnsiString(char(10) ));
MSComm->Output = StringToOleStr( AnsiString(char(10) ));
MSComm->Output = StringToOleStr( AnsiString(char(10) ));
MSComm->Output = StringToOleStr( AnsiString(char(27)) + AnsiString(char(109)) );
}
//---------------------------------------------------------------------------
以前做过这方面的程序,给你个代码段参考一下:
OleVariant RxBuff;
int bytenum;
char c1;
AnsiString st1; //保存串口传送来的字符
if(MSComm1->InBufferCount>0 &&MSComm1->InputMode==1)
{
RxBuff=MSComm1->Input;
bytenum=RxBuff.ArrayHighBound(1);
int i=0;
while(i<=bytenum)
{
c1=RxBuff.GetElement(i);
st1=st1+c1;
i++;
}
}
--------------------------------------------------------------------------------
更正一下:
OleVariant RxBuff;
int bytenum;
char c1;
AnsiString st1=""; //保存串口传送来的字符
if(MSComm1->InBufferCount>0 &&MSComm1->InputMode==1)
{
RxBuff=MSComm1->Input;
bytenum=RxBuff.ArrayHighBound(1);
int i=0;
while(i {
c1=RxBuff.GetElement(i);
st1=st1+c1;
i++;
}
}
--------------------------------------------------------------------------------
if (MSComm->PortOpen)
return;
MSComm->CommPort= 2;
MSComm->Settings= "115200, n, 8, 1";
MSComm->InBufferSize= 4096;
MSComm->OutBufferSize= 2048;
MSComm->RThreshold= 1;
MSComm->SThreshold= 1;
MSComm->InputMode= comInputModeBinary;
try {
MSComm->PortOpen= true;
MSComm->InputLen= 0;
MSComm->InBufferCount= 0;
MSComm->OutBufferCount= 0;
ShowStatueMessage(1);
Log(1, "连接成功");
}
catch (...){
ShowStatueMessage(0);
}
..........
//---------------------------------------------------------------------------
void __fastcall TFrmMain::MSCommComm(TObject *Sender)
{
char szBuf[RxBUFFERSIZE]={0};
int i, j;
OleVariant RxBuff;
switch (MSComm->CommEvent)
{
/* case comEvSend:
ShowMessage("Send!!!!");
case comEvCTS:
ShowMessage("CTS!!!!");
case comEvDSR:
ShowMessage("DSR!!!!");
case comEvCD:
ShowMessage("CD!!!!");
*/
///*
case comEvReceive:
if (MSComm->InBufferCount >0) {
RxBuff= MSComm->Input;
j= RxBuff.ArrayHighBound(1);
for (i=0; i<=j; i++)
szBuf= RxBuff.GetElement(i);
szBuf= NULL;
#ifdef _DEBUG
RichEdit1->Lines->Add(AnsiString(szBuf));
#endif
if (strstr(szBuf, "+CMTI") != 0) // 收到短消息了
{
// ShowMessage("来短消息了");
ProcessSMS(szBuf);
}
}
break;
//*/
case comEvRing:
ShowMessage("来电话了!!!!");
break;
}
}
你可能主要的麻烦在于 VC和BCB对于 OleVariant支持的不同。
--------------------------------------------------------------------------------
DWORD __fastcall TFrmMain::WriteComm(char *aTxBuff, int BuffSize)
{
OleVariant TxBuff;
int i;
if (MSComm->PortOpen) {
if (BuffSize >0) {
TxBuff= VarArrayCreate(OPENARRAY(int, (0, BuffSize-1)), varByte);
for (i=0; i TxBuff.PutElement(aTxBuff, i);
MSComm->Output= TxBuff;
}
}
return 0;
}
本文转自
http://soa.5d6d.com/redirect.php?fid=11&tid=47&goto=nextnewset