function message_rec.write_msg2(receivedbyte: byte;
var Count, ID1, OP1, DL1, CHK1: byte;
var Addr1, Msg1: ArrayOfByte): string;
begin
Result := 'OK';
try
case Count of
0:
begin
ID1 := receivedbyte;
end;
1:
begin
OP1 := receivedbyte;
end;
2..5:
begin
Addr1[Count - 2] := receivedbyte;
end;
6:
begin
DL1 := receivedbyte;
end;
end;
try
SetLength(msg1, DL1);
except on E:Exception do
begin
result:='SetLength';
end;
end;
if ((DL1 = 0) and (Count >= 7)) then
begin
end
else
begin
if ((Count >= 7) and (Count < 7 + DL1)) then
begin
Msg1[Count - 7] := receivedbyte;
end;
end;
if (Count = 7 + dl1) then
begin
chk1 := receivedbyte;
end
except
on E: Exception do
begin
Result := 'Class:' + E.ClassName + sLineBreak +'Message:' + E.Message;
end;
end;
end;This is the function I use to form the message I get from an RS232 interface. I take Byte by Byte, increase a counter and based on that counter, I know what the byte is. The problem is that sometimes, random, I get Access Violation. Can anyone tell why?
DL = DataLength. I set the MSG to length of DataLength so everything will get in there.
Sometimes it works fine, sometimes I get access violation.