Q&A

  • CPORT 3.1 에서 MODBUS 통신시 에러 발생..
Delphi6.0 에서
CPORT 3.1  에서 MODBUS 통신을 하려고 하는데
자료를 송신하면 이런 에러가 걸려서 ....
010800000000E00B 자료를 송신하면

EComPort With Mesage 'WriteFile funcion failed(win err code :6) ....

송신하는 소스는 다음과 같아요..

unit Modbus_User;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, CPort;

type
  TForm_User = class(TForm)
    Edit_TX: TEdit;
    Btn_CRC: TButton;
    Btn_Send: TButton;
    Label_TX: TLabel;
    Label_RX: TLabel;
    Edit_RX: TEdit;
    ComPort1: TComPort;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    procedure FormShow(Sender: TObject);
    function Make_CRCvalue(Data: array of byte; nByte:word):Word;
    procedure Btn_SendClick(Sender: TObject);
    procedure Btn_CRCClick(Sender: TObject);
    procedure ComPort1RxChar(Sender: TObject; Count: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form_User: TForm_User;

Const
  CRC_Table:  ARRAY[0..255] OF WORD =
     ($0000,$C0C1,$C181,$0140,$C301,$03C0,$0280,$C241,$C601,$06C0,$0780,
      $C741,$0500,$C5C1,$C481,$0440,$CC01,$0CC0,$0D80,$CD41,$0F00,$CFC1,
      $CE81,$0E40,$0A00,$CAC1,$CB81,$0B40,$C901,$09C0,$0880,$C841,$D801,
      $18C0,$1980,$D941,$1B00,$DBC1,$DA81,$1A40,$1E00,$DEC1,$DF81,$1F40,
      $DD01,$1DC0,$1C80,$DC41,$1400,$D4C1,$D581,$1540,$D701,$17C0,$1680,
      $D641,$D201,$12C0,$1380,$D341,$1100,$D1C1,$D081,$1040,$F001,$30C0,
      $3180,$F141,$3300,$F3C1,$F281,$3240,$3600,$F6C1,$F781,$3740,$F501,
      $35C0,$3480,$F441,$3C00,$FCC1,$FD81,$3D40,$FF01,$3FC0,$3E80,$FE41,
      $FA01,$3AC0,$3B80,$FB41,$3900,$F9C1,$F881,$3840,$2800,$E8C1,$E981,
      $2940,$EB01,$2BC0,$2A80,$EA41,$EE01,$2EC0,$2F80,$EF41,$2D00,$EDC1,
      $EC81,$2C40,$E401,$24C0,$2580,$E541,$2700,$E7C1,$E681,$2640,$2200,
      $E2C1,$E381,$2340,$E101,$21C0,$2080,$E041,$A001,$60C0,$6180,$A141,
      $6300,$A3C1,$A281,$6240,$6600,$A6C1,$A781,$6740,$A501,$65C0,$6480,
      $A441,$6C00,$ACC1,$AD81,$6D40,$AF01,$6FC0,$6E80,$AE41,$AA01,$6AC0,
      $6B80,$AB41,$6900,$A9C1,$A881,$6840,$7800,$B8C1,$B981,$7940,$BB01,
      $7BC0,$7A80,$BA41,$BE01,$7EC0,$7F80,$BF41,$7D00,$BDC1,$BC81,$7C40,
      $B401,$74C0,$7580,$B541,$7700,$B7C1,$B681,$7640,$7200,$B2C1,$B381,
      $7340,$B101,$71C0,$7080,$B041,$5000,$90C1,$9181,$5140,$9301,$53C0,
      $5280,$9241,$9601,$56C0,$5780,$9741,$5500,$95C1,$9481,$5440,$9C01,
      $5CC0,$5D80,$9D41,$5F00,$9FC1,$9E81,$5E40,$5A00,$9AC1,$9B81,$5B40,
      $9901,$59C0,$5880,$9841,$8801,$48C0,$4980,$8941,$4B00,$8BC1,$8A81,
      $4A40,$4E00,$8EC1,$8F81,$4F40,$8D01,$4DC0,$4C80,$8C41,$4400,$84C1,
      $8581,$4540,$8701,$47C0,$4680,$8641,$8201,$42C0,$4380,$8341,$4100,
      $81C1,$8081,$4040);
implementation

{$R *.dfm}

//각 콤포넌트들의 설명 구성 및 APDComport의 초기 설정.
procedure TForm_User.FormShow(Sender: TObject);
begin
  Form_User.Caption:='WEST6100 - 서보 드라이브 Modbus 테스트용 샘플 프로그램';
  Label_TX.Font.Color:=clyellow;
  Edit_TX.Color:=clyellow;
  Edit_TX.Font.Color:=clblack;
  Label_RX.Font.Color:=clblue;
  Edit_RX.Color:=clblue;
  Edit_RX.Font.Color:=clwhite;
  comport1.Port := 'COM1';//사용하는 컴퓨터의 시리얼 포트 번호
  comport1.BaudRate := br4800;//통신 속도
end;

//Table에 의한 CRC값 생성 함수
function TForm_User.Make_CRCvalue(Data: array of byte; nByte: word): Word;
var
  CRCValue:Word;
  i:integer;
begin
  CRCvalue:=$FFFF;
  For i := 1 to nByte do
    CRCvalue := Hi(CRCvalue)  XOR CRC_Table[ Data[i-1] XOR Lo(CRCvalue) ];
  Result:=CRCvalue;
end;

//송신할 DATA의 CRC값을 계산하여 첨가.
procedure TForm_User.Btn_CRCClick(Sender: TObject);
var
  i,j:integer;
  CRC_Data_Len:integer;//CRC 계산에 사용될 데이타 길이
  CRC_Data_Array:array[1..30]of byte;//CRC 계산에 사용될 데이타
  Result_CRC:Word;//계산된 CRC값
begin
  edit_tx.text := edit1.text + edit2.text + edit3.text + edit4.text+ edit5.text + edit6.text;
  CRC_Data_Len:=round(length(Edit_TX.Text)/2);
  For i:=1 to CRC_Data_Len do
    begin
      j:=i*2-1;
      CRC_Data_Array[i]:=byte(strtoint('$'+Edit_TX.Text[j]+Edit_TX.Text[j+1]));
    end;
  Result_CRC:=Make_CRCValue(CRC_Data_Array,CRC_Data_Len);
  Edit_TX.Text:=Edit_TX.Text+inttohex(Lo(Result_CRC),2)+inttohex(Hi(Result_CRC),2);
end;

//데이타 송신
procedure TForm_User.Btn_SendClick(Sender: TObject);
var
  i,j:integer;
  TX_Data_Size:integer;//송신할 데이타 길이
  TX_Data_Array:array[1..32]of byte;//송신할 데이타
begin
  Edit_RX.Text:='';
  TX_Data_Size:=round(length(Edit_TX.Text)/2);
  For i:=1 to TX_Data_Size do
    begin
      j:=i*2-1;
      TX_Data_Array[i]:=byte(strtoint('$'+Edit_TX.Text[j]+Edit_TX.Text[j+1]));
    end;
    comport1.Write(TX_Data_Array,TX_Data_Size) ;


end;

//데이타 수신




procedure TForm_User.ComPort1RxChar(Sender: TObject; Count: Integer);
var
  i:integer;
  RX_Char:char;//수신된 값
  RX_Array:array[1..200]of string;//수신된 데이타
begin
  showmessage('aaaaaaaaaaaaaaaa');
  ComPort1.Read(RX_Array, Count);

  for i:=1 to count do
    begin
      RX_Array[i]:=inttohex(byte(RX_Char),2);
      Edit_RX.Text:=Edit_RX.Text+RX_Array[i];
    end;
end;

end.

무엇인지 고수님들 부탁해요....
1  COMMENTS
  • Profile
    박홍재 2007.05.09 00:35
    코드를 죽 보다가 보니까 Comport에 대해 Port 번호와 BaudRate까지 설정하시고 Open 하시지 않으셨네요.
    설마 Open 하시는 것을 잊어버리시지는 않으셨겠죠.(그런데 소스 어디에서도 Open에 대한 명령이 없다는...ㅠ.ㅠ)