comm1.parity:=None;

zhang_kingrose 2010-05-19 11:33:11
comm1.parity:=None;
我的程式里面提示 incompatible types:'SPCOMM.TPARITY 'AND 'UMAIN.TPARITY'
我看了 别人程式中的 是这样设置的,我的这样就提示错误
Comm1.parity:=Tparity(none);
这样也不行 不知道怎么搞的
高手 指点下吧!谢谢!
...全文
249 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
bdmh 2010-05-19
  • 打赏
  • 举报
回复
怎么不行,就得转一下Tparity
Kstudy 2010-05-19
  • 打赏
  • 举报
回复
main里要uses spcomm
使用示例: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, SPComm, TeEngine, Series, ExtCtrls, TeeProcs, Chart; type TForm1 = class(TForm) Comm1: TComm; ComboBox1: TComboBox; ComboBox2: TComboBox; ComboBox3: TComboBox; ComboBox4: TComboBox; ComboBox5: TComboBox; Label1: TLabel; Label2: TLabel; Label3: TLabel; Label4: TLabel; Label5: TLabel; Button1: TButton; Memo1: TMemo; procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer; BufferLength: Word); procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; Viewstring:string; i:integer; rbuf,sbuf:array[1..6] of byte; implementation {$R *.dfm} procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer; BufferLength: Word); var i:integer; begin viewstring:=' '; move(buffer^,pchar(@rbuf)^,bufferlength); for i:=1 to bufferlength do viewstring:=viewstring+inttohex(rbuf[i],2)+' '; viewstring:='接受'+viewstring; memo1.lines.add(viewstring); memo1.lines.add(' '); end; procedure TForm1.Button1Click(Sender: TObject); begin //判断按键的状态可以便面打开串口出错时,要按两次按键 if Button1.Caption = '打开串口' then begin // 串口初始化 //EnumComPorts(ComboBox1.Items);//得到串口列表 Comm1.CommName := ComboBox1.Text; Comm1.BaudRate := StrToInt(ComboBox2.Text); Comm1.Parity := None; //ComboBox3.Text Comm1.ByteSize := _8; //ComboBox4.Text Comm1.StopBits := _1; //ComboBox5.Text //########################################### Comm1.StartComm;//打开串口 Button1.Caption := '关闭串口'; ComboBox1.Enabled := false; ComboBox2.Enabled := false; ComboBox3.Enabled := false; ComboBox4.Enabled := false; ComboBox5.Enabled := false; //btnSend.Enabled := true; //ImageOff.Visible := false; //ImageOn.Visible := true; end else begin //############################################## Comm1.StopComm; // 关闭串口 Button1.Caption := '打开串口'; ComboBox1.Enabled := true; ComboBox2.Enabled := true; ComboBox3.Enabled := true; ComboBox4.Enabled := true; ComboBox5.Enabled := true; //btnSend.Enabled := false; //ImageOn.Visible := false; //ImageOff.Visible := true; end; end; end.
在ClassWizard中响应ID为~Dlg中的WM_TIMER消息。 使用SetTimer(nIDEvent,time,NULL)来建立一个定时器,关闭定时器用KillTimer(nIDEvent)函数。 然后可以响应ON_WM_TIMER消息来响应一个定时器完成一次记时后的程序。 响应方式如下: void CTimeDlg::OnTimer(UINT nIDEvent) { if(nIDEvent==1000)//间隔为5秒 { //处理事件 } elseif(nIDEvent==1001)//间隔为10秒 { //处理事件 } CDialog::OnTimer(nIDEvent); } 以下是给出一个串口通信定时检查接收数据的部分代码 void CMyDlg::OnOpenCom() { // TODO: Add your control notification handler code here if( f_open_com==true ) { f_open_com = false; GetDlgItem(IDC_OPEN_COM)->SetWindowText("打开通信端口"); CloseHandle(hComm); KillTimer(1000); /// 关闭定时器 return ; } SetTimer(1000, 1000, NULL); ///nIDEvent==1000,time=5000ms const char *ComNo; DCB dcb; string temp("COM1"); ComNo = temp.c_str(); hComm = CreateFile( ComNo , GENERIC_READ|GENERIC_WRITE , 0 , NULL , OPEN_EXISTING , 0 , 0); if( hComm==INVALID_HANDLE_VALUE ) /// 如果端口未打开 { MessageBox("打开通信端口出错!" , "Comm Error" , MB_OK); return ; } /// 将dcb地址传入,以取得通信参数 GetCommState(hComm,&dcb); /// 得知目前通信状态 dcb.BaudRate = CBR_9600; dcb.ByteSize = 8; /// 字节为8 dcb.Parity = NOPARITY; /// Parity为None dcb.StopBits = ONESTOPBIT; /// 1个停止位 if( !SetCommState( hComm , &dcb)){ MessageBox("通信端口设置出错!" , "Set Error" , MB_OK ); CloseHandle(hComm); return; } GetDlgItem(IDC_OPEN_COM)->SetWindowText("关闭通信端口"); f_open_com = true; } void CMyDlg::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default char inbuff[1024]; DWORD nBytesRead , dwError; COMSTAT cs; /// 取得状态 ClearCommError( hComm , &dwError , &cs); /// 数据是否大于所准备的缓冲区 if( cs.cbInQue > sizeof(inbuff) ) { PurgeComm(hComm , PURGE_RXCLEAR ); /// 清除通信端口数据 return ; } ReadFile(hComm , inbuff , cs.cbInQue , &nBytesRead , NULL ); //接收通信端口的数据 inbuff[cs.cbInQue] = '\0'; MessageBox("打开通信端口出错!" , "Comm Error" , MB_OK); m_Receive.Format("%s",inbuff); UpdateData(false); CDialog::OnTimer(nIDEvent); } 李杨: for(int i=0; ;i++ ) { ... Sleep(5); if(i>...) {AfxMessageBox("错误XXX"); return;} }//跳出后记得停止一些机器动作

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧