hotpower 发表于 2004-12-3 22:04:06

Delphi+MSComm控件串口收发程序

//Delphi+MSComm控件串口收发程序

//HotPower@126.com



procedure TForm1.FormCreate(Sender: TObject);

begin

if MSComm1.PortOpen then MSComm1.PortOpen := false;//关闭端口

MSComm1.CommPort := 2;//设置端口2

MSComm1.InBufferSize := 256;//设置接收缓冲区为256个字节

MSComm1.OutBufferSize := 256;//设置发送缓冲区为256个字节

MSComm1.Settings := '9600,n,8,1';//9600波特率,无校验,8位数据位,1位停止位

MSComm1.InputLen := 0;//读取缓冲区全部内容(32个字节)

MSComm1.InBufferCount := 0;// 清除接收缓冲区

MSComm1.OutBufferCount:=0;// 清除发送缓冲区

MSComm1.RThreshold := 32;//设置接收32个字节产生OnComm 事件

//MSComm1.InputMode := comInputModeText;//文本方式

MSComm1.InputMode := comInputModeBinary;//二进制方式

MSComm1.PortOpen := true;//打开端口

end;



procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);

begin

if MSComm1.PortOpen then MSComm1.PortOpen := false;;//关闭端口

end;



procedure TForm1.MSComm1Comm(Sender: TObject);

var

buffer: Olevariant;//MSComm1.InputMode = comInputModeBinary

str: string;//MSComm1.InputMode = comInputModeText

i: integer;

begin

case MSComm1.CommEvent of

    comEvReceive: //串行接收事件处理

      begin

      if MSComm1.InputMode = comInputModeText then //字符方式读取

          str := MSComm1.Input//读出后会自动清除接收缓冲区,str~str

      else //二进制方式读取

          buffer := MSComm1.Input;//读出后会自动清除接收缓冲区,buffer~buffer

      Edit3.Text := '';

      for i := 0 to MSComm1.RThreshold - 1 do //32字节Hex转换

      begin

          if MSComm1.InputMode = comInputModeText then //单行字符转换

            Edit3.Text := Edit3.Text + inttohex(byte(str), 2) + ' '

          else //单行二进制数据转换

            Edit3.Text := Edit3.Text + inttohex(buffer, 2) + ' ';

      end;

      Memo2.Lines.Add(Edit3.Text);//加入一行显示

      end;

end;

end;



procedure TForm1.BitBtn1Click(Sender: TObject);

var

i: integer;

begin

Edit4.Text := '';

for i := 0 to 31 do

begin

    MSComm1.Output := char(i);//发送一个字符

    Edit4.Text := Edit4.Text + inttohex(i, 2);//以十六进制字符显示

end;

Memo1.Lines.Add(Edit4.Text);//加入一行显示



end;

armok 发表于 2004-12-4 02:50:40

谢谢!

hotpower 发表于 2004-12-4 08:54:38

瞎玩...过几天发表一下SPCOMM控件的修改方法...



我在21IC还没发表过...



哈哈...最近一直在努力做winavr菜鹅...



http://www.21icbbs.com/club/bbs/ShowAnnounce.asp?id=1457434

zhoujibing1 发表于 2005-1-21 14:05:23

请这位大哥请快同我联系,有事请教关天串口打印问题,会有重谢.13911062711

armok 发表于 2005-1-21 17:07:20

zhoujibing1 不用急,hotpower 总在你睡着的时候就会让论坛。你先去睡觉吧 :)

hotpower 发表于 2005-1-21 21:31:30

"串口打印"???



搞个串口打印机再要几张打印纸我想问题不大...估计就是比我们习惯的串口通讯多几根握手线罢了...



Delphi+MSComm或Delphi+SPComm都不错...

sam2004 发表于 2005-1-22 07:44:59

我用Delphi+CPort

semilog 发表于 2005-6-14 03:08:06

“瞎玩...过几天发表一下SPCOMM控件的修改方法...”是什么意思啊?

dbjdeycl 发表于 2006-2-15 19:43:41

油菜农,到处都是油啊!支持,严重支持,不是梦露是甘露。

zheng8648 发表于 2006-2-15 19:50:52

我觉得用Delphi+Spcomm 较好,可以做成绿色软件.

rogetxu 发表于 2006-3-9 15:34:51

mscomm不好,在高速率时会掉数据,对0x00处理也不容易。

用文件方式不难。

SkyOffice 发表于 2006-4-9 14:05:38

Delphi+Spcom 比较绿色,但在98下有点问题。

用MSCOM可能比较稳定,但发布麻烦。

computer00 发表于 2006-4-20 00:50:58

俺用VB来搞…………

hotpower 发表于 2006-4-25 01:39:19

哈哈...SPCOMM比MSCOMM好玩~~~

chen 发表于 2006-12-27 12:41:48

把酷帖顶起!把酷帖顶齐!

national6 发表于 2007-11-7 23:50:06

大家不要学DELPHI了,,它和GCC是有冲突的,

Error makefile 451: Command syntax error
Error makefile 452: Command syntax error
Error makefile 453: Command syntax error

Error makefile 454: Command syntax error
Error makefile 457: Command syntax error
Error makefile 458: Command syntax error
Error makefile 460: Command syntax error
Error makefile 461: Command syntax error
Error makefile 463: Command syntax error
Error makefile 465: Command syntax error
Error makefile 466: Command syntax error
Error makefile 521: Colon expected
Error makefile 530: Colon expected
Error makefile 552: Too many rules for target 'obj/%.o'
Error makefile 562: Too many rules for target '%.s'
Error makefile 569: Too many rules for target 'obj/%.o'
Error makefile 598: Command syntax error

usbfish 发表于 2007-11-7 23:51:55

没事,我用的D7lite版本,没有冲突。晕死,不能这么诽谤Delphi啊

Feeling_MCU 发表于 2007-11-8 09:39:06

我用C++builder +Pcomm控件 做的,很稳定哦,感觉很顺手

alex_pan 发表于 2007-12-15 13:30:29

使用控件在数据速率很高时,会导致接受不到数据的!
最好是能对端口直接操作,但是在WINDOWS2000以上的系统是不允许的,只能在98的系统。

dhyana 发表于 2009-3-5 10:15:34

直接端口操作可以做到的。
另外,现在很多软件由于访问串口使用文件模式,COM1 ,COM2等等,结果到了COM10就不行了,应为文件名不同了。

wygood123 发表于 2009-3-20 09:34:11

刚开始学,顶起……

tomy 发表于 2009-6-28 01:22:43

mark

TSXYZ 发表于 2010-4-17 13:34:31

学习了

newlong 发表于 2011-3-22 13:02:36

回复【楼主位】hotpower 菜农
-----------------------------------------------------------------------

老大怎么用mobus协议实现上下位机的串口通讯啊,也是用MSComm控件做的

betbet 发表于 2011-4-29 12:20:12

回复【19楼】dhyana
-----------------------------------------------------------------------

aaaaa.............

你的头像怎么长得跟我这麽像。。
页: [1]
查看完整版本: Delphi+MSComm控件串口收发程序