W78E516B单片机串口通信上位机程序谁帮忙做一些解释。
时间:10-02
整理:3721RD
点击:
最近用W78E516B做一个实验板,要用串口写LDROM程序和APROM程序,我有上位机程序,由于不是C编的,我不太能看懂上位机程序,不知道怎么写下位机程序,求知道的大神们做一些解释,最好能帮我编个C语言下位机程序。小妹在这里谢谢大家了。
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, OleCtrls, ExtCtrls, MSCommLib_TLB, ComCtrls;
type
TForm1 = class(TForm)
OpenDialog1: TOpenDialog;
Button1: TButton;
Label3: TLabel;
Label4: TLabel;
Label1: TLabel;
Edit1: TEdit;
Label2: TLabel;
Edit2: TEdit;
ProgressBar1: TProgressBar;
Button2: TButton;
Memo1: TMemo;
MSComm1: TMSComm;
procedure Button1Click(Sender: TObject);
procedure MSComm1Comm(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure writecomm(buf :array of byte);
procedure trans_hex();
procedure error();
function crc16(sbuf: array of byte ;len : integer) : word;
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
send_times,receive_times:integer;
status : integer;
FromF: file;
textf: textfile;
NumRead, NumWritten,i,crcdata: Integer;
Buf: array[1..63] of byte;
Bufout: variant;
write_buf : array[0..65535] of byte;
buf_size : integer;
const crc_ta : array[0..255] of integer =(
$0000, $1021, $2042, $3063, $4084, $50a5, $60c6, $70e7,
$8108, $9129, $a14a, $b16b, $c18c, $d1ad, $e1ce, $f1ef,
$1231, $0210, $3273, $2252, $52b5, $4294, $72f7, $62d6,
$9339, $8318, $b37b, $a35a, $d3bd, $c39c, $f3ff, $e3de,
$2462, $3443, $0420, $1401, $64e6, $74c7, $44a4, $5485,
$a56a, $b54b, $8528, $9509, $e5ee, $f5cf, $c5ac, $d58d,
$3653, $2672, $1611, $0630, $76d7, $66f6, $5695, $46b4,
$b75b, $a77a, $9719, $8738, $f7df, $e7fe, $d79d, $c7bc,
$48c4, $58e5, $6886, $78a7, $0840, $1861, $2802, $3823,
$c9cc, $d9ed, $e98e, $f9af, $8948, $9969, $a90a, $b92b,
$5af5, $4ad4, $7ab7, $6a96, $1a71, $0a50, $3a33, $2a12,
$dbfd, $cbdc, $fbbf, $eb9e, $9b79, $8b58, $bb3b, $ab1a,
$6ca6, $7c87, $4ce4, $5cc5, $2c22, $3c03, $0c60, $1c41,
$edae, $fd8f, $cdec, $ddcd, $ad2a, $bd0b, $8d68, $9d49,
$7e97, $6eb6, $5ed5, $4ef4, $3e13, $2e32, $1e51, $0e70,
$ff9f, $efbe, $dfdd, $cffc, $bf1b, $af3a, $9f59, $8f78,
$9188, $81a9, $b1ca, $a1eb, $d10c, $c12d, $f14e, $e16f,
$1080, $00a1, $30c2, $20e3, $5004, $4025, $7046, $6067,
$83b9, $9398, $a3fb, $b3da, $c33d, $d31c, $e37f, $f35e,
$02b1, $1290, $22f3, $32d2, $4235, $5214, $6277, $7256,
$b5ea, $a5cb, $95a8, $8589, $f56e, $e54f, $d52c, $c50d,
$34e2, $24c3, $14a0, $0481, $7466, $6447, $5424, $4405,
$a7db, $b7fa, $8799, $97b8, $e75f, $f77e, $c71d, $d73c,
$26d3, $36f2, $0691, $16b0, $6657, $7676, $4615, $5634,
$d94c, $c96d, $f90e, $e92f, $99c8, $89e9, $b98a, $a9ab,
$5844, $4865, $7806, $6827, $18c0, $08e1, $3882, $28a3,
$cb7d, $db5c, $eb3f, $fb1e, $8bf9, $9bd8, $abbb, $bb9a,
$4a75, $5a54, $6a37, $7a16, $0af1, $1ad0, $2ab3, $3a92,
$fd2e, $ed0f, $dd6c, $cd4d, $bdaa, $ad8b, $9de8, $8dc9,
$7c26, $6c07, $5c64, $4c45, $3ca2, $2c83, $1ce0, $0cc1,
$ef1f, $ff3e, $cf5d, $df7c, $af9b, $bfba, $8fd9, $9ff8,
$6e17, $7e36, $4e55, $5e74, $2e93, $3eb2, $0ed1, $1ef0
) ;
const testbuf : array[1..8] of byte =(0,0,0,0,6,$d,$d2,$e3);
const char_biao : array[0..15] of string = ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then { Display Open dialog box }
begin
receive_times := 0;
send_times := 0;
status :=0;
// button1.Enabled := false;
memo1.Lines.Add('文件读取中。');
trans_hex();
end;
end;
procedure TForm1.error();
var
i : integer;
begin
mscomm1.PortOpen := false;
i := 0;
end;
procedure TForm1.trans_hex();
var
i,j,k : integer;
s,ds : string;
aa : array[0..300] of byte;
tmpa,tmpb : integer;
maxadr : integer;
begin
if opendialog1.FilterIndex = 1 then
begin
buf_size := 0;
maxadr := 0;
for i:=0 to 65535 do
write_buf := $ff;
assignfile(textf,opendialog1.filename);
reset(textf);
try
while not eof(textf) do
begin
readln(textf,s);
i := -1;
repeat
i := i + 1;
until (i = (sizeof(s)-1)) or (s = ':');
j := 0;
ds := '';
while (i <= (length(s)-1)) do
begin
i := i + 1;
for k:=0 to 15 do
if char_biao[k] = s then
tmpa := k ;
i := i + 1;
for k:=0 to 15 do
if char_biao[k] = s then
tmpb := k ;
aa[j] := (tmpa shl 4 ) + tmpb;
ds := ds + inttohex(aa[j],2);
j := j+ 1;
end;
tmpa := (aa[1] shl 8) + aa[2];
if (tmpa > maxadr ) then
begin
maxadr := tmpa;
buf_size := maxadr + aa[0];
end;
for i:=0 to aa[0]-1 do
begin
write_buf[tmpa + i] := aa[i+4];
end;
// memo1.Lines.Add(ds);
end
finally
closefile(textf);
end;
// for i:=0 to buf_size-1 do
// memo1.Lines.Add(inttohex(write_buf,2) + ' ');
end
else
begin
AssignFile(FromF, OpenDialog1.FileName);
Reset(FromF, 1);
form1.Caption := opendialog1.FileName ;
BlockRead(FromF, write_buf, SizeOf(write_buf), buf_size);
end;
edit1.text := inttostr(buf_size);
progressbar1.Max := buf_size;
memo1.Lines.Add('连接中。');
end ;
function TForm1.crc16(sbuf: array of byte;len : integer) : word;
var
i,j,tmp:word;
begin
tmp := 0;
for i:= 0 to len-1 do
begin
j := $80;
repeat
if ((tmp and $8000)<> $0) then
begin
tmp := tmp shl 1;
tmp := tmp xor $1021;
end
else
tmp := tmp shl 1;
if (sbuf and j)<> 0 then
tmp := tmp xor $1021;
j := j shr 1;
until (j = 0)
end;
crc16 := tmp;
end;
procedure TForm1.MSComm1Comm(Sender: TObject);
var
receive : variant;
buf1 : array[0..0] of byte;
i : integer;
k ,j: integer;
begin
if mscomm1.CommEvent = comevreceive then
begin
j := mscomm1.InBufferCount;
receive := mscomm1.Input;
for k:= 0 to j-1 do
begin
case receive[k] of
$11: //同步数据
begin
buf1[0] := $11;
writecomm(buf1);
send_times := 0;
if (status <> 1) then
begin
memo1.Lines.Add('芯片擦除中。');
status := 1;
end
end;
$22:
begin
buf1[0] := $22;
writecomm(buf1);
if (status <> 2) then
begin
memo1.Lines.Add('擦除完成!');
memo1.Lines.Add('检查芯片是否为空。');
status := 2;
end
end;
$AA:
begin
buf1[0] := $aa;
writecomm(buf1);
if (status <> 3) then
begin
memo1.Lines.Add('空片检测成功!');
status := 3;
end
end;
$44:
begin
if (status <> 4) then
begin
memo1.Lines.Add('芯片检测失败!');
status := 4;
end
end;
$55: //数据写入正确,接着发送
begin
if (status <> 5) then
begin
memo1.Lines.Add('开始写入程序。');
status := 5;
end;
if (send_times < buf_size ) then
begin
if (send_times +60 < buf_size) then
NumRead := 60
else
NumRead := buf_size - send_times;
end
else
NumRead := 0;
if (NumRead <>0) then
begin
bufout:= VarArrayCreate([0,NumRead],varByte);
bufout[0] := NumRead;
for i:=1 to NumRead do
begin
bufout := write_buf[send_times];
send_times := send_times + 1 ;
end;
mscomm1.Output := bufout;
edit2.Text := inttostr(send_times);
progressbar1.Position := send_times;
end
else
begin
buf1[0] := $0;
writecomm(buf1);
end
end;
$66:
begin
if (status <> 6) then
begin
memo1.Lines.Add('芯片写入失败!');
status := 6;
end
end;
$77:
begin
if (status <> 7) then
begin
memo1.Lines.Add('芯片写入完成!');
status := 7;
end
end;
end;
end;
end;
end;
procedure TForm1.writecomm(buf :array of byte);
var
V:Variant;
i:Integer;
begin
V:= VarArrayCreate([Low(Buf), High(Buf)],varByte);
for i:=Low(Buf) to High(Buf) do V:=Buf;
mscomm1.output := V;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
// timer1.Enabled := false;
if mscomm1.PortOpen = true then
mscomm1.PortOpen := false;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
status := 0;
mscomm1.PortOpen := true;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
application.Terminate ;
end;
end.
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, OleCtrls, ExtCtrls, MSCommLib_TLB, ComCtrls;
type
TForm1 = class(TForm)
OpenDialog1: TOpenDialog;
Button1: TButton;
Label3: TLabel;
Label4: TLabel;
Label1: TLabel;
Edit1: TEdit;
Label2: TLabel;
Edit2: TEdit;
ProgressBar1: TProgressBar;
Button2: TButton;
Memo1: TMemo;
MSComm1: TMSComm;
procedure Button1Click(Sender: TObject);
procedure MSComm1Comm(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure writecomm(buf :array of byte);
procedure trans_hex();
procedure error();
function crc16(sbuf: array of byte ;len : integer) : word;
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
send_times,receive_times:integer;
status : integer;
FromF: file;
textf: textfile;
NumRead, NumWritten,i,crcdata: Integer;
Buf: array[1..63] of byte;
Bufout: variant;
write_buf : array[0..65535] of byte;
buf_size : integer;
const crc_ta : array[0..255] of integer =(
$0000, $1021, $2042, $3063, $4084, $50a5, $60c6, $70e7,
$8108, $9129, $a14a, $b16b, $c18c, $d1ad, $e1ce, $f1ef,
$1231, $0210, $3273, $2252, $52b5, $4294, $72f7, $62d6,
$9339, $8318, $b37b, $a35a, $d3bd, $c39c, $f3ff, $e3de,
$2462, $3443, $0420, $1401, $64e6, $74c7, $44a4, $5485,
$a56a, $b54b, $8528, $9509, $e5ee, $f5cf, $c5ac, $d58d,
$3653, $2672, $1611, $0630, $76d7, $66f6, $5695, $46b4,
$b75b, $a77a, $9719, $8738, $f7df, $e7fe, $d79d, $c7bc,
$48c4, $58e5, $6886, $78a7, $0840, $1861, $2802, $3823,
$c9cc, $d9ed, $e98e, $f9af, $8948, $9969, $a90a, $b92b,
$5af5, $4ad4, $7ab7, $6a96, $1a71, $0a50, $3a33, $2a12,
$dbfd, $cbdc, $fbbf, $eb9e, $9b79, $8b58, $bb3b, $ab1a,
$6ca6, $7c87, $4ce4, $5cc5, $2c22, $3c03, $0c60, $1c41,
$edae, $fd8f, $cdec, $ddcd, $ad2a, $bd0b, $8d68, $9d49,
$7e97, $6eb6, $5ed5, $4ef4, $3e13, $2e32, $1e51, $0e70,
$ff9f, $efbe, $dfdd, $cffc, $bf1b, $af3a, $9f59, $8f78,
$9188, $81a9, $b1ca, $a1eb, $d10c, $c12d, $f14e, $e16f,
$1080, $00a1, $30c2, $20e3, $5004, $4025, $7046, $6067,
$83b9, $9398, $a3fb, $b3da, $c33d, $d31c, $e37f, $f35e,
$02b1, $1290, $22f3, $32d2, $4235, $5214, $6277, $7256,
$b5ea, $a5cb, $95a8, $8589, $f56e, $e54f, $d52c, $c50d,
$34e2, $24c3, $14a0, $0481, $7466, $6447, $5424, $4405,
$a7db, $b7fa, $8799, $97b8, $e75f, $f77e, $c71d, $d73c,
$26d3, $36f2, $0691, $16b0, $6657, $7676, $4615, $5634,
$d94c, $c96d, $f90e, $e92f, $99c8, $89e9, $b98a, $a9ab,
$5844, $4865, $7806, $6827, $18c0, $08e1, $3882, $28a3,
$cb7d, $db5c, $eb3f, $fb1e, $8bf9, $9bd8, $abbb, $bb9a,
$4a75, $5a54, $6a37, $7a16, $0af1, $1ad0, $2ab3, $3a92,
$fd2e, $ed0f, $dd6c, $cd4d, $bdaa, $ad8b, $9de8, $8dc9,
$7c26, $6c07, $5c64, $4c45, $3ca2, $2c83, $1ce0, $0cc1,
$ef1f, $ff3e, $cf5d, $df7c, $af9b, $bfba, $8fd9, $9ff8,
$6e17, $7e36, $4e55, $5e74, $2e93, $3eb2, $0ed1, $1ef0
) ;
const testbuf : array[1..8] of byte =(0,0,0,0,6,$d,$d2,$e3);
const char_biao : array[0..15] of string = ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then { Display Open dialog box }
begin
receive_times := 0;
send_times := 0;
status :=0;
// button1.Enabled := false;
memo1.Lines.Add('文件读取中。');
trans_hex();
end;
end;
procedure TForm1.error();
var
i : integer;
begin
mscomm1.PortOpen := false;
i := 0;
end;
procedure TForm1.trans_hex();
var
i,j,k : integer;
s,ds : string;
aa : array[0..300] of byte;
tmpa,tmpb : integer;
maxadr : integer;
begin
if opendialog1.FilterIndex = 1 then
begin
buf_size := 0;
maxadr := 0;
for i:=0 to 65535 do
write_buf := $ff;
assignfile(textf,opendialog1.filename);
reset(textf);
try
while not eof(textf) do
begin
readln(textf,s);
i := -1;
repeat
i := i + 1;
until (i = (sizeof(s)-1)) or (s = ':');
j := 0;
ds := '';
while (i <= (length(s)-1)) do
begin
i := i + 1;
for k:=0 to 15 do
if char_biao[k] = s then
tmpa := k ;
i := i + 1;
for k:=0 to 15 do
if char_biao[k] = s then
tmpb := k ;
aa[j] := (tmpa shl 4 ) + tmpb;
ds := ds + inttohex(aa[j],2);
j := j+ 1;
end;
tmpa := (aa[1] shl 8) + aa[2];
if (tmpa > maxadr ) then
begin
maxadr := tmpa;
buf_size := maxadr + aa[0];
end;
for i:=0 to aa[0]-1 do
begin
write_buf[tmpa + i] := aa[i+4];
end;
// memo1.Lines.Add(ds);
end
finally
closefile(textf);
end;
// for i:=0 to buf_size-1 do
// memo1.Lines.Add(inttohex(write_buf,2) + ' ');
end
else
begin
AssignFile(FromF, OpenDialog1.FileName);
Reset(FromF, 1);
form1.Caption := opendialog1.FileName ;
BlockRead(FromF, write_buf, SizeOf(write_buf), buf_size);
end;
edit1.text := inttostr(buf_size);
progressbar1.Max := buf_size;
memo1.Lines.Add('连接中。');
end ;
function TForm1.crc16(sbuf: array of byte;len : integer) : word;
var
i,j,tmp:word;
begin
tmp := 0;
for i:= 0 to len-1 do
begin
j := $80;
repeat
if ((tmp and $8000)<> $0) then
begin
tmp := tmp shl 1;
tmp := tmp xor $1021;
end
else
tmp := tmp shl 1;
if (sbuf and j)<> 0 then
tmp := tmp xor $1021;
j := j shr 1;
until (j = 0)
end;
crc16 := tmp;
end;
procedure TForm1.MSComm1Comm(Sender: TObject);
var
receive : variant;
buf1 : array[0..0] of byte;
i : integer;
k ,j: integer;
begin
if mscomm1.CommEvent = comevreceive then
begin
j := mscomm1.InBufferCount;
receive := mscomm1.Input;
for k:= 0 to j-1 do
begin
case receive[k] of
$11: //同步数据
begin
buf1[0] := $11;
writecomm(buf1);
send_times := 0;
if (status <> 1) then
begin
memo1.Lines.Add('芯片擦除中。');
status := 1;
end
end;
$22:
begin
buf1[0] := $22;
writecomm(buf1);
if (status <> 2) then
begin
memo1.Lines.Add('擦除完成!');
memo1.Lines.Add('检查芯片是否为空。');
status := 2;
end
end;
$AA:
begin
buf1[0] := $aa;
writecomm(buf1);
if (status <> 3) then
begin
memo1.Lines.Add('空片检测成功!');
status := 3;
end
end;
$44:
begin
if (status <> 4) then
begin
memo1.Lines.Add('芯片检测失败!');
status := 4;
end
end;
$55: //数据写入正确,接着发送
begin
if (status <> 5) then
begin
memo1.Lines.Add('开始写入程序。');
status := 5;
end;
if (send_times < buf_size ) then
begin
if (send_times +60 < buf_size) then
NumRead := 60
else
NumRead := buf_size - send_times;
end
else
NumRead := 0;
if (NumRead <>0) then
begin
bufout:= VarArrayCreate([0,NumRead],varByte);
bufout[0] := NumRead;
for i:=1 to NumRead do
begin
bufout := write_buf[send_times];
send_times := send_times + 1 ;
end;
mscomm1.Output := bufout;
edit2.Text := inttostr(send_times);
progressbar1.Position := send_times;
end
else
begin
buf1[0] := $0;
writecomm(buf1);
end
end;
$66:
begin
if (status <> 6) then
begin
memo1.Lines.Add('芯片写入失败!');
status := 6;
end
end;
$77:
begin
if (status <> 7) then
begin
memo1.Lines.Add('芯片写入完成!');
status := 7;
end
end;
end;
end;
end;
end;
procedure TForm1.writecomm(buf :array of byte);
var
V:Variant;
i:Integer;
begin
V:= VarArrayCreate([Low(Buf), High(Buf)],varByte);
for i:=Low(Buf) to High(Buf) do V:=Buf;
mscomm1.output := V;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
// timer1.Enabled := false;
if mscomm1.PortOpen = true then
mscomm1.PortOpen := false;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
status := 0;
mscomm1.PortOpen := true;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
application.Terminate ;
end;
end.
已退回1积分