当前位置:文档之家› 串口接收、发送数据OK

串口接收、发送数据OK

//接收数据procedure TFrm_m1ReceiveData(Sender: TObject;Buffer: PAnsiChar; BufferLength: Word);var i:integer;rbuf,sbuf:array[1..21] of byte;tmpstr,tmphex,tmpview:string;begintmpstr:='';tmphex:='';move(buffer^,pchar(@rbuf)^,bufferlength);for i:=1 to BufferLength dobegintmpstr:=tmpstr+char(rbuf[i]); //字符型tmphex:=tmphex+inttohex(rbuf[i],2); //16进制型end;if trim(tmpstr)<>'' thenviewstring:=viewstring+tmpstr;if tmphex='0A' thenbegin//Memo1.Lines.Add(viewstring);if Edit1.Text<>viewstring thenEdit1.Text:=viewstring;viewstring:='';end;end;//发送数据procedure senddata;vari:integer;commflg:boolean;beginviewstring:='';commflg:=true;for i:=1 to high(sbuf) dobeginif not m1.writecommdata(@sbuf[i],1) thenbegincommflg:=false;break;end;//发送时字节间的延时sleep(5);viewstring:=viewstring+inttohex(sbuf[i],2)+' ';if not commflg then messagedlg('发送失败!',mterror,[mbyes],0);end;viewstring:='发送'+viewstring;Form1.memo1.lines.add(viewstring);Form1.memo1.lines.add('');end;procedure TForm1.sentcustom();var str11:string ;i,j,k:integer;tmpstr:string;beginif Edit1.Text<>'' thenbeginstr11:=Trimplace(Edit1.Text); //替换字符串中的所有空格i:=round(length(str11)/2); //获得字符串长度,除2取整后加1setlength(sbuf,i+1); //重新设定发送数组范围for j:=1 to i dobegintmpstr:=copy(str11,j*2-1,2);if tmpstr='' thentmpstr:='00';sbuf[j]:=byte(strtoint('$'+tmpstr)); //将变量转换为byte数组end;endelsebeginsbuf[1]:=byte($53); //帧头sbuf[2]:=byte($54);sbuf[3]:=byte($2C);sbuf[4]:=byte($47);sbuf[5]:=byte($53);sbuf[6]:=byte($2C);sbuf[7]:=byte($2B);sbuf[8]:=byte($20);sbuf[9]:=byte($20);sbuf[10]:=byte($20);sbuf[11]:=byte($30);sbuf[12]:=byte($2E);sbuf[13]:=byte($30);sbuf[14]:=byte($30);sbuf[15]:=byte($20);sbuf[16]:=byte($6B);sbuf[17]:=byte($67);sbuf[18]:=byte($20);sbuf[19]:=byte($20);sbuf[20]:=byte($0D);sbuf[21]:=byte($0A);end;end;varForm1: TForm1;viewstring:string;rbuf,sbuf:array of byte;implementation{$R *.dfm}//此处用于自动获取系统的串口数目以及名称,调用api函数实现相关功能procedure EnumComPorts(Ports: TStrings);varKeyHandle: HKEY;ErrCode, Index: Integer;ValueName, Data: string;ValueLen, DataLen, ValueType: DWORD;TmpPorts: TStringList;beginErrCode := RegOpenKeyEx(HKEY_LOCAL_MACHINE,'HARDWARE\DEVICEMAP\SERIALCOMM',0,KEY_READ,KeyHandle);if ErrCode <> ERROR_SUCCESS thenExit; // raise EComPort.Create(CError_RegError, ErrCode);TmpPorts := TStringList.Create;tryIndex := 0;repeatV alueLen := 256;DataLen := 256;SetLength(ValueName, ValueLen);SetLength(Data, DataLen);ErrCode := RegEnumValue(KeyHandle,Index,PChar(ValueName),Cardinal(ValueLen),nil,@ValueType,PByte(PChar(Data)),@DataLen);if ErrCode = ERROR_SUCCESS thenbeginSetLength(Data, DataLen);TmpPorts.Add(Data);Inc(Index);endelseif ErrCode <> ERROR_NO_MORE_ITEMS thenexit; //raise EComPort.Create(CError_RegError, ErrCode);until (ErrCode <> ERROR_SUCCESS) ;TmpPorts.Sort;Ports.Assign(TmpPorts);finallyRegCloseKey(KeyHandle);TmpPorts.Free;end;end;procedure TForm1.SetComPort();begin//获取计算机中的串口列表,并设置电子秤的串口号EnumComPorts(ComboBox1.Items);if ComboBox1.Items.Count>0 thenbeginComboBox1.ItemIndex:=0; //ComboBox1.Items.Count-1;mName:=ComboBox1.Text;//Comm1.StopComm; //打开之前先关闭串口//Comm1.StartComm; //打开串口;if Comm1.PortOpen=true thenLabel1.Caption:='串口状态:'+ComboBox1.Text+'串口已打开'elseLabel1.Caption:='串口状态:'+ComboBox1.Text+'串口已关闭'//上面可使用循环向各个串口接收或发送数据来判断电子秤的串口号endelsebeginLabel1.Caption:='串口状态:'+ComboBox1.Text+'串口已关闭';end;//showmessage(inttostr(ComboBoxComList.Items.Count));end;//替换字符串中的空格函数function Trimplace(str: string): string;vartmp: string;p : Integer;begintmp := Trim(str);while Pos(' ',tmp)>0 dobeginp := Pos(' ',tmp);tmp := Copy(tmp,1,p-1)+Copy(tmp,p+1,Length(tmp)-p);end;Result := tmp;end;//自定义发送数据过程procedure senddata;vari:integer;commflg:boolean;beginviewstring:='';commflg:=true;//showmessage(inttostr(high(sbuf)));for i:=1 to high(sbuf) dobeginif not m1.writecommdata(@sbuf[i],1) thenbegincommflg:=false;break;end;//发送时字节间的延时sleep(5);viewstring:=viewstring+inttohex(sbuf[i],2)+' ';if not commflg then messagedlg('发送失败!',mterror,[mbyes],0);end;viewstring:='发送'+viewstring;Form1.memo1.lines.add(viewstring);Form1.memo1.lines.add('');end;function strToHexStr(str:string):string;varc:char;ss:string;i:integer;beginwhile str<>'' do beginc:=str[1];ss:=ss+format('%0x',[ord(c)]);delete(str,1,1);end;strtohexStr:= ss;end;procedure TForm1.sentcustom();var str11:string ;i,j,k:integer;tmpstr:string;beginif Edit1.Text<>'' thenbeginstr11:=Trimplace(Edit1.Text); //替换字符串中的所有空格i:=round(length(str11)/2); //获得字符串长度,除2取整后加1setlength(sbuf,i+1); //重新设定发送数组范围for j:=1 to i dobegintmpstr:=copy(str11,j*2-1,2);if tmpstr='' thentmpstr:='00';sbuf[j]:=byte(strtoint('$'+tmpstr)); //将变量转换为byte数组end;endelsebeginsbuf[1]:=byte($53); //帧头sbuf[2]:=byte($54);sbuf[3]:=byte($2C);sbuf[4]:=byte($47);sbuf[5]:=byte($53);sbuf[6]:=byte($2C);sbuf[7]:=byte($2B);sbuf[8]:=byte($20);sbuf[9]:=byte($20);sbuf[10]:=byte($20);sbuf[11]:=byte($30);sbuf[12]:=byte($2E);sbuf[13]:=byte($30);sbuf[14]:=byte($30);sbuf[15]:=byte($20);sbuf[16]:=byte($6B);sbuf[17]:=byte($67);sbuf[18]:=byte($20);sbuf[19]:=byte($20);sbuf[20]:=byte($0D);sbuf[21]:=byte($0A);end;end;procedure TForm1.FormCreate(Sender: TObject); //设置串口号beginSetComPort();end;procedure TForm1.Button1Click(Sender: TObject); //打开串口beginComm1.StopComm;Comm1.StartComm;if Comm1.PortOpen=true thenLabel1.Caption:='串口状态:'+ComboBox1.Text+'串口已打开'elseLabel1.Caption:='串口状态:'+ComboBox1.Text+'串口已关闭';end;procedure TForm1.Button2Click(Sender: TObject); //关闭串口beginComm1.StopComm;if Comm1.PortOpen=true thenLabel1.Caption:='串口状态:'+ComboBox1.Text+'串口已打开'elseLabel1.Caption:='串口状态:'+ComboBox1.Text+'串口已关闭'end;procedure TForm1.Button4Click(Sender: TObject);beginMemo1.Clear;end;procedure TForm1.Button3Click(Sender: TObject);beginif Edit1.Text='' thenbeginsbuf[1]:=byte($53); //帧头sbuf[2]:=byte($54);sbuf[3]:=byte($2C);sbuf[4]:=byte($47);sbuf[5]:=byte($53);sbuf[6]:=byte($2C);sbuf[7]:=byte($2B);sbuf[8]:=byte($20);sbuf[9]:=byte($20);sbuf[10]:=byte($20);sbuf[11]:=byte($30);sbuf[12]:=byte($2E);sbuf[13]:=byte($30);sbuf[14]:=byte($30);sbuf[15]:=byte($20);sbuf[16]:=byte($6B);sbuf[17]:=byte($67);sbuf[18]:=byte($20);sbuf[19]:=byte($20);sbuf[20]:=byte($0D);sbuf[21]:=byte($0A);end;senddata;//调用发送函数end;procedure TForm1.Button5Click(Sender: TObject); //打开串口,开始连续发送数据beginsentcustom();Comm1.StopComm;Comm1.StartComm;Timer1.Enabled:=true;if Comm1.PortOpen=true thenLabel1.Caption:='串口状态:'+ComboBox1.Text+'串口已打开'elseLabel1.Caption:='串口状态:'+ComboBox1.Text+'串口已关闭';end;procedure TForm1.Button6Click(Sender: TObject); //停止发送数据,关闭串口beginTimer1.Enabled:=false;Comm1.StopComm;if Comm1.PortOpen=true thenLabel1.Caption:='串口状态:'+ComboBox1.Text+'串口已打开'elseLabel1.Caption:='串口状态:'+ComboBox1.Text+'串口已关闭';end;procedure TForm1.FormShow(Sender: TObject);begin//SetLength(@sbuf,1..22);end;end.。

相关主题