您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > labwindowscvi6第七章 串行口通信和网络数据传输
自动化测试与控制研究所LabWindows/CVI教程(6)串行口和网络通讯主讲:乔立岩Email:qiaoliyan@hit.edu.cn(交作业)MSN:qiaoliyan@msn.com(在线交流)Tel:86413532ext.8611(逸夫楼611)2/112019/8/161.串行口通讯•RS-232端口定义载波检测接收数据发送数据数据终端就绪信号地数据装置就绪请求发送清除发送振铃指示1234567893/112019/8/16•数据传输格式•电气特性负逻辑:1=-5V~-15V,0=+5V~+15V最高速率:20kbit/s最远距离:15m起始位奇偶校验位停止位ASCII码4/112019/8/16•打开配置和关闭intOpenComConfig(intCOM_Port,charDevice_Name[],longBaud_Rate,intParity,intData_Bits,intStop_Bits,intInput_Queue_Size,intOutput_Queue_Size);i=OpenComConfig(1,“”,14400,0,7,1,512,512);intCloseCom(intCOM_Port);i=CloseCom(1);•读写intComRd(intCOM_Port,charBuffer[],intCount);i=ComRd(1,buf,128);intComWrt(intCOM_Port,charBuffer[],intCount);i=ComWrt(1,buf,128);intComRdByte(intCOM_Port);intComWrtByte(intCOM_Port,intByte);•控制intFlushInQ(intCOM_Port);intFlushOutQ(intCOM_Port);5/112019/8/16•状态查询intGetInQLen(intCOM_Port);i=GetInQLen(1);intGetOutQLen(intCOM_Port);i=GetOutQLen(1);•事件处理intInstallComCallback(intCOM_Port,intEvent_Mask,intNotify_Count,intEvent_Character,ComCallbackPtrCallback_Function,void*Callback_Data);notifyCount=50;//Waitforatleast50bytesinqueueeventChar=13;//WaitforLFeventMask=LWRS_RXFLAG|LWRS_RECEIVE;InstallComCallback(comport,eventMask,notifyCount,eventChar,ComCallbackFunction,NULL);6/112019/8/16网络通讯2.TCP协议(TransmissionControlProtocol传输控制协议)•使用TCP进行网络通信时每个连接包括一个服务器和一个客户。TCP服务器能通过网络向客户应用程序发送数据或从客户应用程序接收数据,客户应用程序也可以向服务器应用程序发送数据或从服务器应用程序接收数据;•TCP服务器应用程序一旦向系统注册,就等待客户应用程序的连接请求;而客户应用程序只能向已经存在的服务器应用程序请求连接。•每个TCP服务器需要主机名(如:aaa.bbb.ccc)或主机IP地址(如:123.456.78.90)和这个主机的唯一端口号7/112019/8/16•服务器函数intRegisterTCPServer(unsignedintPort_Number,tcpFuncPtrCallback_Function,void*Callback_Data);i=RegisterTCPServer(200,ServerTCPCB,0);回调函数:int(*tcpFuncPtr)(unsignedhandle,intxType,interrCode,void*callbackData);•TCP_CONNECT•TCP_DISCONNECT•TCP_DATAREADYintCVICALLBACKServerTCPCB(unsignedhandle,intevent,interror,void*callbackData);intUnregisterTCPServer(unsignedintPort_Number);intServerTCPRead(unsignedintConversation_Handle,void*Data_Buffer,unsignedintData_Size,unsignedintTime_Out);dataSize=ServerTCPRead(g_hconversation,receiveBuf,dataSize,1000);8/112019/8/16intServerTCPWrite(unsignedintConversation_Handle,void*Data_Pointer,unsignedintData_Size,unsignedintTime_Out);i=ServerTCPWrite(g_hconversation,transmitBuf,strlen(transmitBuf)+1,1000);intDisconnectTCPClient(unsignedintConversation_Handle);•客户机函数intConnectToTCPServer(unsignedint*Conversation_Handle,unsignedintPort_Number,charServer_Host_Name[],tcpFuncPtrCallback_Function,void*Callback_Data,unsignedintTime_Out);i=ConnectToTCPServer(&g_hconversation,200,tempBuf,ClientTCPCB,NULL,5000);回调函数:int(*tcpFuncPtr)(unsignedhandle,intxType,interrCode,void*callbackData);intCVICALLBACKClientTCPCB(unsignedhandle,intevent,interror,void*callbackData);•TCP_DISCONNECT•TCP_DATAREADY9/112019/8/16intDisconnectFromTCPServer(unsignedintConversation_Handle);intClientTCPRead(unsignedintConversation_Handle,void*Data_Buffer,unsignedintData_Size,unsignedintTime_Out);i=ClientTCPRead(g_hconversation,receiveBuf,dataSize,1000);intClientTCPWrite(unsignedintConversation_Handle,void*Data_Pointer,unsignedintData_Size,unsignedintTime_Out);ClientTCPWrite(g_hconversation,transmitBuf,strlen(transmitBuf)+1,1000);•支持函数intGetTCPHostAddr(charBuffer[],intBuffer_Size);intGetTCPHostName(charBuffer[],intBuffer_Size);intGetTCPPeerAddr(unsignedintConnection_Handle,charBuffer[],intBuffer_Size);intGetTCPPeerName(unsignedintConnection_Handle,charBuffer[],intBuffer_Size);10/112019/8/163.DataSocket•NI公司定义的网络数据通讯协议•DataSocket库为NI的DataSocket编程提供了支持,也简化了应用程序之间动态数据交换•适用于大批量实时数据传递,简化用户编程11/112019/8/16上机作业(3选1)1.将上次上机作业产生的波形数据存储到文件中,并能够从文件中回读显示(要求用ANSIC库stdio.h中的文件打开和读写函数)2.编写一个自发自收的RS-232串口通讯程序(电缆连接2-3)3.编写网络通讯程序(可以两位同学合作,一人编写服务器程序,另一人编写客户机程序)电缆连接:1-3,2-6,其它一对一
本文标题:labwindowscvi6第七章 串行口通信和网络数据传输
链接地址:https://www.777doc.com/doc-309845 .html