注册 登录
编程论坛 Delphi论坛

[求助]关于delphi使用mscomm串口通讯的问题

luperfect 发布于 2005-04-23 00:34, 3678 次点击
向高手请教: 我在Delphi中使用了微软的MSCOMM.oxc控件进行访问串口通讯.在定义的字节数组后,不知如何向mscomm.output赋值.我是这样做的, iniCOM; buf[0:=byte($16); buf[1]:=inttobcd(strtoint(copy(meterId,11,2))); buf[2]:=inttobcd(strtoint(copy(meterID,9,2))); buf[3]:=inttobcd(strtoint(copy(meterId,7,2))); buf[4]:=inttobcd(strtoint(copy(meterID,5,2))); buf[5]:=inttobcd(strtoint(copy(meterId,3,2))); buf[6]:=inttobcd(strtoint(copy(meterID,1,2))); buf[7]:=byte($68); buf[8]:=byte($04); buf[9]:=byte($13); buf[10]:=byte($D4); buf[11]:=byte($33); buf[12]:=byte($33); buf[13]:=byte($33); buf[14]:=byte($33); buf[15]:=arrsellcount[0]; buf[16]:=arrsellcount[1]; buf[17]:=arrsellcount[2]; buf[18]:=arrsellcount[3]; buf[19]:=arrsellcount[4]; buf[20]:=byte(selltimes); buf[21]:=byte(selltimes div $100); buf[23]:=byte($16); Sum_CS:=0; for h:=0 to 21 do sum_Cs:= sum_Cs + Buf[h]; buf[22]:=byte(Sum_CS); S:=''; for h:=0 to 23 do S:=S + chr(Buf[h]); mscomm1.Output :=S ; sleep(10); closecom; 对于串口参数设置\字节数组的取值和校验都正确,不知发送处错在何处! 请教高手!谢谢!
6 回复
#2
luperfect2005-05-01 22:54
我已找到原因了,是因为MSCOMM控件的BUG,改用Delphi的Tcomm就可以了
#3
hlxjw20012005-05-07 15:17
你好,我现在也在用MSComm串口编程,我有点问题想请教你。procedure TForm1.Button1Click(Sender: TObject); begin if mscomm1.PortOpen=false then begin mscomm1.PortOpen:=true; end; mscomm1.Settings:='9600,n,8,1'; mscomm1.InBufferSize:=1024; mscomm1.InBufferCount:=0; mscomm1.InputMode:=1; mscomm1.InputLen:=0; mscomm1.Output:='AT+CSCA=+8613800311500'; mscomm1.Output:='AT+CMGF=1'; mscomm1.Output:='AT+CMGS=+8613722347848'; mscomm1.Output:='successfull'; end; 但是一单击按钮不是弹出“Project project1.exe raised exception class EOleException with message 'port already open',process stopped,use step or run to continue."就是弹出“Project project1.exe raised exception class EOleException with message 'The devic is not open',process stopped,use step or run to continue." 请问一下这问题出在什么地方。我这是用数据线把手机和电脑连起来,用上面的程序实现发短信的功能。
#4
luperfect2005-05-21 22:19

你的问题很简单,因为你先打开串口而后对MSCOMM进行设置的,正确的方法是先设置,后打开串口.

if not mscomm1.PortOpen then mscomm1.PortOpen:=true; 放到最后,就OK了,试一试.

#5
一切皆在掌握中2008-07-07 13:34
掌中技术论坛,各位兄弟姐妹帮忙顶一下, 多多支持小弟。大家顺便帮我提点意见,发布一些技术文章,也可以免费下载相关资料,大家互相交流、学习。有需要行情连接的,也可以跟我联系,谢谢.http://bbs.
#6
tangrongyong2016-05-16 16:55
楼主能发一份资料给我吗?刚刚学!
#7
tangrongyong2016-05-16 16:55
楼主能发一份资料给我吗?刚刚学!
1