vb-串口程序 Posted on 2012-06-15 | In Program | | Visitors: 介绍 使用 vb 编写,这类工具很多,练手用 使用 MSComm 串口调试工具 支持多语言 没有更多功能 相关 GetSystemDefaultLCID(实现多语言的切换) Timer(定时器,刷新数据) 代码123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148Option ExplicitDim portopen As IntegerDim cmd_open As StringDim cmd_close As StringDim msg As StringPrivate Declare Function GetSystemDefaultLCID Lib "kernel32" () As LongPrivate Sub Form_Load()Dim localid As Longlocalid = GetSystemDefaultLCIDSelect Case localidCase &H404 '繁体语言cmd_open = "打開"cmd_close = "關閉"msg = "串口未打開!"Form1.Caption = "串口調試(KB_Vision)"Label1.Caption = "端口號"Label2.Caption = "波特率"Label3.Caption = "數據位"Label4.Caption = "停止位"Label5.Caption = "較驗位"Command1.Caption = cmd_closeCommand2.Caption = "發送"send.Text = "發送區"receive.Text = "接收區"Check1.Caption = "自動發送"Case &H804 '简体语言cmd_open = "打开"cmd_close = "关闭"msg = "串口没打开!"Form1.Caption = "串口调试(KB_Vision)"Label1.Caption = "端口号"Label2.Caption = "波特率"Label3.Caption = "数据位"Label4.Caption = "停止位"Label5.Caption = "校验位"Command1.Caption = cmd_closeCommand2.Caption = "发送"send.Text = "发送区"receive.Text = "接收区"Check1.Caption = "自动发送"Case &H409 'Englishcmd_open = "Open"cmd_close = "Close"msg = "No comport open"Form1.Caption = "Comdebug(KB_Vision)"Label1.Caption = "Comport"Label2.Caption = "Baudrate"Label3.Caption = "Databits"Label4.Caption = "Stopbits"Label5.Caption = "Parity"Command1.Caption = cmd_closeCommand2.Caption = "Send"send.Text = "SendText"receive.Text = "ReceiveText"Check1.Caption = "Auto Send"End Selecttip.Caption = ""comport.ListIndex = 0sendTimer.Enabled = FalsereceTimer.Enabled = FalseEnd SubPrivate Sub Command1_Click()Dim baudrateset As StringDim parityset As StringDim databitset As StringDim stopbitset As StringDim comset As Stringbaudrateset = baudrate.Textparityset = Left(parity.Text, 1)databitset = databits.Textstopbitset = stopbits.Textcomset = baudrateset + "," + parityset + "," + databitset + "," + stopbitsetOn Error GoTo showerrIf (portopen = 0) ThenCommand1.Caption = cmd_openportopen = 1MSComm.CommPort = comport.ListIndex + 1MSComm.Settings = comset'Print MSComm.SettingsMSComm.InputLen = 0MSComm.InBufferSize = 1024MSComm.InputMode = 0MSComm.RThreshold = 1MSComm.RTSEnable = TrueMSComm.SThreshold = 0MSComm.portopen = Truetip.Caption = "Ok"tip.ForeColor = RGB(0, 255, 0)receTimer.Enabled = Truesend.Text = ""receive.Text = ""Elseportopen = 0Command1.Caption = cmd_closetip.Caption = ""tip.ForeColor = RGB(0, 0, 0)receTimer.Enabled = FalseMSComm.portopen = FalseCheck1.Value = 0End IfExit Sub'err tipshowerr:If (portopen = 1) Thentip.Caption = "Err"tip.ForeColor = RGB(255, 0, 0)Elsetip.Caption = ""tip.ForeColor = RGB(0, 0, 0)End IfEnd SubPrivate Sub Command2_Click()If (tip.Caption = "Ok") ThenMSComm.Output = send.TextElseMsgBox (msg)End IfEnd SubPrivate Sub Check1_Click()If Check1.Value = 1 ThensendTimer.Enabled = TruesendTimer.Interval = Time.TextElsesendTimer.Enabled = FalseEnd IfEnd SubPrivate Sub sendTimer_Timer()If (tip.Caption = "Ok") ThenMSComm.Output = send.TextElseMsgBox (msg)sendTimer.Enabled = FalseCheck1.Value = 0End IfEnd SubPrivate Sub receTimer_Timer()If (tip.Caption = "Ok") Thenreceive.Text = receive.Text + MSComm.Inputreceive.SelStart = Len(receive.Text)End IfDoEventsEnd Sub 坚持原创技术分享,您的支持将鼓励我继续创作! Donate WeChat Pay Alipay Post author: tosobright Post link: http://www.soshare.cn/2012/06/15/120615-vb-comm/ Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.