delphi中用mscomm做串口通讯 怎么选择串口?

来源:百度知道 编辑:UC知道 时间:2024/05/26 08:57:15
我用checkbox1添加了com1 com2 可是程序运行以后没法用checkbox1选串口
改怎么办啊?

procedure TForm1.FormCreate(Sender: TObject);
begin
begin
checkbox1.Checked:=true;

combobox1.Items.add('com1');
combobox1.Items.add('com2');
combobox1.Text:=combobox1.Items[1];

i:=combobox1.Text;

if i='com1' then
mscomm1.CommPort:=1;
if i='com2' then
mscomm1.CommPort:=2;
end;
if mscomm1.portopen=false then
begin
mscomm1.Settings:='9600,n,8,1';
mscomm1.portopen:=true;
end;
mscomm1.OutBufferCount:=0;
mscomm1.inBufferCount:=0;

end;

谢谢
错了 是用combobox1添加的 我打错了

又是你啊?和MSCOMM较上了?
那就在combobox1的onselect事件里面写上
if mscomm1.portopen=true then
mscomm1.portopen:=false;
if combobox1.Text='com1' then
mscomm1.CommPort:=1;
if combobox1.Text='com2' then
mscomm1.CommPort:=2;
mscomm1.portopen:=true;
就是判断下MSCOMM是不是打开的,打开的就先关一下,然后根据选择的内容改变串口号,最后启用MSCOMM

MSCOMM没用过,我一直都用SPCOMM,感觉不错..

我用SPCOMM,感觉一直不错