《SCL及DLL上位机编程应用实例培训教材.pdf》由会员分享,可在线阅读,更多相关《SCL及DLL上位机编程应用实例培训教材.pdf(33页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、Confidential上海安浦鸣志自动化设备有限公司ConfidentialSCL上位机编程概述编程示例C+VBC#DLL介绍Q/AConfidentialSCL语言(Serial/Stream Command Language)请求和应答均以回车符 r 作为结尾VB:CC&VBCRC/C#:CCrConfidential通讯速率每字节所需时间=1/(波特率/(起始位+数据位+停止位)发送一条指令并接收到响应所需的时间总字节数=地址+指令+参数+CheckSum+r例:1FL200000XX+回车影响数据接收时间的因素数据发送DSP处理数据接收操作系统调度波特率波特率960019200384
2、0057600115200每字节所需时间(ms)1.040.520.260.170.087ConfidentialSCL Utility(只支持COM1COM16)SCL Utility 2(开发中)串口调试助手(只支持COM1COM4)Access Port(前台+后台)Bus Hound(后台监控)Confidential设置基本参数(端口、波特率、数据位等)打开端口发送数据接收数据关闭端口Confidential引用MSComm控件Windows API使用第三方SerialPort类Confidential/*Module:SerialPort.HPurpose:Interface f
3、or an MFC wrapper class for serial portsCopyright(c)1999-2013 by PJ Naughter.All rights reserved.Copyright/Usage Details:You are allowed to include the source code in any product(commercial,shareware,freeware or otherwise)when your product is released in binary form.You are allowed to modify the sou
4、rce code in any way you want except you cannot modify the copyright details at the top of each module.If you want to distribute source code with your application,then you are only allowed to distribute versions released by the author.This is to maintain a single distribution point for the source cod
5、e.*/ConfidentialOpen 打开端口Close 关闭端口IsOpen 如果端口已经打开,返回true,否则返回falseRead 读取数据Write 写入数据Confidentialvoid CVCSampleDlg:SendCommand(CStringstrCommand)if(m_IsOpen=false)return;CString command=strCommand;if(commandcommand.GetLength()-1!=r)command+=r;LPTSTR lptCommand=new TCHARcommand.GetLength()+1;strcpy(
6、lptCommand,command);m_SerialPort.Write(lptCommand,command.GetLength();Confidentialwhile(true)int count=m_SerialPort.Read(lpbuf,1);if(count 0)for(int i=0;i (UINT)nTimeOut)break;ConfidentialConfidentialCString str;str.Format(JS%.3f,m_JogVelocity);ExecuteCommand(str);str.Format(JA%.3f,m_JogAccel);Execu
7、teCommand(str);str.Format(JL%.3f,m_JogDecel);ExecuteCommand(str);if(pMsg-hwnd=(CButton*)this-GetDlgItem(IDC_BTN_CWJOG)-m_hWnd)ExecuteCommand(_T(DI1);ExecuteCommand(_T(CJ);else if(pMsg-hwnd=(CButton*)this-GetDlgItem(IDC_BTN_CCWJOG)-m_hWnd)ExecuteCommand(_T(DI-1);ExecuteCommand(_T(CJ);ConfidentialCStr
8、ing str;str.Format(VE%.3f,m_SampleMoveVelocity);ExecuteCommand(str);str.Format(AC%.3f,m_SampleMoveAccel);ExecuteCommand(str);str.Format(DE%.3f,m_SampleMoveDecel);ExecuteCommand(str);str.Format(EG%d,m_SampleMoveElectronicGearing);ExecuteCommand(str);str.Format(DI%d,m_SampleMoveDistance);ExecuteComman
9、d(str);str.Format(FL);ExecuteCommand(str);ConfidentialConfidential初始化Private Sub Form_Load()On Error Resume NextMSComm.CommPort=1MSComm.Settings=9600,n,8,1MSComm.PortOpen=TrueIf Err.Number 0 Then打开端口失败MsgBox Fail to open Port&MSComm.CommPort&.EndEnd IfEnd SubConfidential端口关闭Private Sub Form_Unload(C
10、ancel As Integer)If MSComm.PortOpen ThenMSComm.PortOpen=FalseEnd IfEnd SubConfidential发送数据Private Sub cmd_Send_Click()Dim i As Integer,j As IntegerDim v As Variant v=Split(txt_CommandList.Text,vbCrLf)For i=0 To UBound(v)If Len(v(i)0 And Not(CStr(v(i)=vbCr Or CStr(v(i)=vbLf)ThenMSComm.Output=CStr(v(i
11、)&vbCrIf Len(txt_Write.Text)0 Thentxt_Write.Text=txt_Write.Text&vbCrLf 显示发送内容End Iftxt_Write.Text=txt_Write.Text&CStr(v(i)Delay 0.01 延时0.01秒,End IfNext iEnd SubConfidential接收数据Private Sub MSComm_OnComm()Dim data As VariantDim i As IntegerIf MSComm.CommEvent=comEvReceive Thendata=MSComm.Input显示接收内容If
12、 Len(txt_Read.Text)0 Thentxt_Read.Text=txt_Read.Text&vbCrLfEnd Iftxt_Read.Text=txt_Read.Text&dataEnd IfEnd SubConfidentialSystem.IO.Ports.SerialPort类Confidentialprivate void MainForm_Load(object sender,EventArgs e)BindSerialPort(this.cmb_Port);if(this.cmb_Port.Items.Count 0)this.cmb_Port.SelectedInd
13、ex=0;else/No COM port found.Application.Exit();this.cmb_BitRate.SelectedIndex=0;m_SerialPort.DataReceived+=new SerialDataReceivedEventHandler(SerialPort_DataReceived);Confidentialprivate void BindSerialPort(System.Windows.Forms.ComboBox ddl)string commArray=System.IO.Ports.SerialPort.GetPortNames();
14、int comNO=new intcommArray.Length;for(int i=0;i commArray.Length;i+)comNOi=Convert.ToInt16(commArrayi.Substring(3);List lstPort=new List(commArray);lstPort.Sort(delegate(string x,string y)if(x.Length y.Length)return 1;else if(x.Length y.Length)return-1;return x.CompareTo(y););for(int i=0;i 0 means t
15、he Jog Direction is CWstring command=DI20000;SendCommand(command);command=string.Format(JS0,this.nud_JogSpeed.Value);SendCommand(command);command=string.Format(JA0,this.nud_JogAccelDecel.Value);SendCommand(command);command=CJ;SendCommand(command);Confidentialprivate void SerialPort_DataReceived(obje
16、ct sender,SerialDataReceivedEventArgs e)byte buf=new bytem_SerialPort.BytesToRead;m_SerialPort.Read(buf,0,buf.Length);m_DataProcessDelegate d=new m_DataProcessDelegate(DataProcess);this.Invoke(d,new object buf);Confidentialprivate void DataProcess(byte buf)foreach(byte b in buf)char ch=(char)b;if(ch
17、=0 x0D)this.txt_History.AppendText(rn);else if(b=33&b=126)this.txt_History.AppendText(ch.ToString();else this.txt_History.AppendText(string.Format(0,b);this.txt_History.ScrollToCaret();Confidential开发工具:Microsoft Visuao Studio语言:Visual C+2010基于PJ Naughter的Serial Port类版本:32bit和64bit使用平台:VBC+C#Confiden
18、tialbool IsOpenint OpenPortint ClosePortint SendCommandint ExecuteCommandint ReadXXXXint WriteXXXXConfidentialpublic int COMMSTATUS_OK=0 x00;/OKpublic int COMMSTATUS_TIMEOUT=0 x01;/Command time out?1public int COMMSTATUS_PARAMETERISTOOLONG=0 x02;/Parameter is too long?2public int COMMSTATUS_TOOFEWPA
19、RAMETERS=0 x03;/Too few parameters?3public int COMMSTATUS_TOOMANYPARAMETERS=0 x04;/Too many parameters?4public int COMMSTATUS_PARAMETEROUTOFRANGE=0 x05;/Parameter out of range?5public int COMMSTATUS_CommandBufferFull=0 x06;/Command buffer full?6public int COMMSTATUS_CANNOTPROCESSCOMMAND=0 x07;/Canno
20、t process command?7public int COMMSTATUS_PROGRAMRUNNING=0 x08;/Program running?8public int COMMSTATUS_BADPASSWORD=0 x09;/Bad password?9public int COMMSTATUS_COMMPORTERROR=0 x0A;/Comm port error?10public int COMMSTATUS_BADCHARACTER=0 x0B;/Bad character?11public int COMMSTATUS_IOALREADYUSED=0 x0C;/IO
21、already used?12public int COMMSTATUS_INCORRECTIOCONFIGURATION=0 x0D;/Incorrect IO Configuration?13public int COMMSTATUS_INCORRECTIOFUNCTION=0 x0D;/Incorrect IO function?14public int COMMSTATUS_OPENPORTFAILED=0 x64;/Fail to OpenPortpublic int COMMSTATUS_PORTISNOTOPEN=0 x65;/Serial Port is not openpub
22、lic int COMMSTATUS_NORESPONSE=0 x66;/Drive did not responsepublic int COMMSTATUS_INCORRECTRESPONSE=0 x67;/Drives response is incorrectpublic int COMMSTATUS_CHECKSUMERROR=0 x68;/CheckSumErrorConfidentialMoonsCommLibHelper m_MoonsCommLibHelper=new MoonsCommLibHelper();byte m_Protocol=7;bool m_IsHexFor
23、mat=true;tryint port=Convert.ToInt32(cmb_SerialPort.Text).Substring(3,1);int baudrate=Convert.ToInt32(cmb_RaudRate.Text);int ret=m_MoonsCommLibHelper.OpenPort(port,baudrate,m_Protocol,m_IsHexFormat);if(ret!=m_MoonsCommLibHelper.COMMSTATUS_OK)MessageBox.Show(Open port error!);catch(System.Exception e
24、x)Confidentialdouble js=(double)num_JS.Value;double jajl=(double)nud_JAJL.Value;int di=(int)nud_DI.Value;int ret=0;ret=m_MoonsCommLibHelper.WriteDistance(Math.Abs(di);if(ret=m_MoonsCommLibHelper.COMMSTATUS_OK)trym_MoonsCommLibHelper.WriteJogSpeed(js);m_MoonsCommLibHelper.WriteJogAcceleration(jajl);m_MoonsCommLibHelper.WriteJogDeceleration(jajl);m_MoonsCommLibHelper.WriteCommenceJogging();catch(System.Exception ex)MessageBox.Show(Send Command Failed);ConfidentialConfidential