实例9中,我是通过STC-ISP软件中串口助手调试单片机程序的,不太直观,能否在PC上用PASCAL写一个程序通过串口与单片机沟通呢。完全可以,可以用免费的开发环境LAZARUS开发,安装 lazSerial 后,进行快速开发。上位机和下位机,完全使用PASCAL开发,代码可以复用,非常方便。
// MainForm.pas
unit MainFrm;
{
$mode objfpc}{
$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
ComCtrls, LazSerial, LazSynaSer;
type
{
TMainForm }
TMainForm = class(TForm)
BtnOpen: TButton;
BtnClose: TButton;
BtnQuery: TButton;
BtnCtrlSerial: TButton;
CbbDev: TComboBox;
CbbBaud: TComboBox;
CbbCOM: TComboBox;
Label1: TLabel;
Label2: TLabel;
LazSerial1: TLazSerial;
MemInfo: TMemo;
Shape1: TShape;
StatusBar1: TStatusBar;
procedure BtnCloseClick(Sender: TObject);
procedure BtnCtrlSerialClick(Sender: TObject);
procedure BtnOpenClick(Sender: TObject);
procedure BtnQueryClick(Sender: TObject);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure LazSerial1RxData(Sender: TObject);
private
public
end;
var
MainForm: TMainForm;
implementation
{
$R *.lfm}
uses
MyDef;
{
TMainForm }
procedure TMainForm.BtnCtrlSerialClick(Sender: TObject);
begin
lazSerial1.BaudRate:=br__9600;
lazSerial1.Device:=CbbCom.Text;
try
lazSerial1.Active:=Not lazSerial1.Active;
except
ShowMessage('打开串口失败');
end;
if not lazSerial1.Active then BtnCtrlSerial.Caption:='打开串口'
else BtnCtrlSerial.Caption