串口配置

  • RS232Open
  • RS232Configure
  • RS232Send
  • RS232Receive
  • RS232Close
  • RS232SetHandshake


CAPL串口的API就如下图那么多,下面分别介绍API怎么用。


RS232串口通讯 android rs232串口软件_RS232串口通讯 android

RS232Open

  • dword RS232Open( dword port )

打开串口,当串口不存在或者被其它的应用占用,返回值为0,打开成功,则返回为1.

RS232串口通讯 android rs232串口软件_stm32_02

我这里没有RS232的硬件,就用虚拟串口虚拟了两个串口 COM5和COM6

RS232串口通讯 android rs232串口软件_linux_03


上面串口创建成功后,在设备管理器里面看以看得到。

RS232串口通讯 android rs232串口软件_串口_04


下面我们通过CAPL代码演示下:

on key 'g'
{
	long res;   
	byte Port_Num = 6 ;
	res = RS232Open(Port_Num) ;
	if(res==1)
	{
	  write("opened port sucess");
	}

输出:
opened port sucess //打开串口6成功

RS232Configure

  • dword RS232Configure( dword port, dword baudrate, dword numberOfDataBits, dword numberOfStopBits, dword parity ); // form 1

配置串口的波特率,数据比特的长度,和校验位等 ,默认配置是(9600,8,1,0)。

RS232串口通讯 android rs232串口软件_串口_05

RS232Send

  • dword RS232Send( dword port, byte buffer[], dword number )
    发送一串byte类型数据。如果发送成功返回值为1,失败返回值为0;而且这个API有两个回调函数,当发送成功的时候会调用 RS232OnSend(),发送失败的话,会调用 RS232OnError()
  • RS232OnSend( dword port, byte buffer[], dword number )

RS232串口通讯 android rs232串口软件_单片机_06

  • RS232OnError( dword port, dword errorFlags )

RS232串口通讯 android rs232串口软件_单片机_07


下面通过CAPL演示 发送 “hello world !”字符串到COM6:

/*@!Encoding:ASCII*/
on key 'g'
{
	long res;   
	char text[20] = "Hello World !";
	byte buffer[20];
	int i;
	int length;
	byte Port_Num = 6 ;
	res = RS232Open(Port_Num) ;//打开串口
  	if(res==1)
   	 write("opened port success.");
 	
	res=RS232Configure(Port_Num,9600,8,1,0);//配置串口
 	if(res==1)
 	  write("Configure Port6 Success.");

	 
	length=strlen(text)+1;
	for (i=0;i<length;i++)
	  buffer[i]=text[i];	
	res = RS232Send(Port_Num,buffer,length); //发送数据。
	if ( res==1)
	   write("send successs."); 
 }                                                           
                                                  
RS232OnSend(dword port, byte buffer[], dword number) 
{
 //发送函数成功的回调函数
  char timeStamp[30];
  char text[20];
  int i;
  getLocalTimeString(timeStamp);
  snprintf(text,elCount(text),"");
  for(i=0;i<number;i++)   
  {
    snprintf(text,elCount(text),"%s%c",text,buffer[i]);
  }  
  write("%s : >>>>%s",timeStamp,text);
}

RS232OnError(dword port, dword errorFlags)
{ //发送函数失败的回调函数
   if ( errorFlags & 1 )
      writeLineEx(0,3,"send failed");
   if ( errorFlags & 2 )
      writeLineEx(0,3,"receive failed");
}

输出结果如下:

RS232串口通讯 android rs232串口软件_串口_08

另一方面我们也通过串口工具监控下,我们是否真的发送成功了。

RS232串口通讯 android rs232串口软件_串口_09

RS232Receive

  • RS232Receive( dword port, byte buffer[], dword size )

用法同RS232send,如果接收成功返回值为1,失败返回值为0;而且这个API有两个回调函数,当发送成功的时候会调用 RS232OnReceive(),发送失败的话,会调用 RS232OnError()
调用过RS232send()就可以直接调用RS232Receive()

RS232串口通讯 android rs232串口软件_RS232串口通讯 android_10


RS232串口通讯 android rs232串口软件_单片机_11


下面通过CAPL演示 发送 “hello COM5!”字符串到COM6 ;然后我们通过COM5发送“hello COM6!”

/*@!Encoding:ASCII*/
on key 'g'
{
	long res;   
	char text[20] =  “hello COM5!”
	byte buffer[20];
	int i;
	int length;
	byte Port_Num = 6 ;
	res = RS232Open(Port_Num) ;//打开串口
  	if(res==1)
   	 write("opened port success.");
 	
	res=RS232Configure(Port_Num,9600,8,1,0);//配置串口
 	if(res==1)
 	  write("Configure Port6 Success.");

	 
	length=strlen(text)+1;
	for (i=0;i<length;i++)
	  buffer[i]=text[i];	
	res = RS232Send(Port_Num,buffer,length); //发送数据。
	if ( res==1)
	   write("send successs."); 
	   
    res = RS232Receive(Port_Num,buffer,length); //注册接收事件
	if ( res==1)
 	  write("Receive successs.");
 }                                                           
                                                  
RS232OnSend(dword port, byte buffer[], dword number) 
{
 //发送函数成功的回调函数
  char timeStamp[30];
  char text[20];
  int i;
  getLocalTimeString(timeStamp);
  snprintf(text,elCount(text),"");
  for(i=0;i<number;i++)   
  {
    snprintf(text,elCount(text),"%s%c",text,buffer[i]);
  }  
  write("%s : >>>>%s",timeStamp,text);
}

RS232OnError(dword port, dword errorFlags)
{ //发送函数失败的回调函数
   if ( errorFlags & 1 )
      writeLineEx(0,3,"send failed");
   if ( errorFlags & 2 )
      writeLineEx(0,3,"receive failed");
}


RS232Close

  • dword RS232Close( dword port )
    良好的编程习惯要有始有终

    在脚本中添加下列代码,当工程停止的时候,程序自动回收当前串口的。
on preStop
{
  RS232Close(portNum); 
}

RS232SetHandshake

  • dword RS232SetHandshake( dword port, dword handshake, dword XonLimit,
    dword XoffLimit, dword XonChar, dword XoffChar )

设置通信握手方式,一般情况无需设置。

RS232串口通讯 android rs232串口软件_RS232串口通讯 android_12