Forum > Packages and Libraries

Synapse and Spdo

(1/1)

eldonfsr:
Hello friends i have a small application for comunicate pc to mill and lathes machines, i get connect the pc to machine and also pc to pc and send files but when i wanna connect and send file form pc to another pc i get a message can opne \\.\COM4 portas you imagin i using usb adapter here is my code, if some body can help thanks.


--- Code: ---unit formactions;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  FileCtrl,inifiles,synaser,LResources, SynEdit, SdpoSerial;
type

  { TFormAction }

  TFormAction = class(TForm)
    BSend: TButton;
    BClose: TButton;
    BReceive: TButton;
    Button1: TButton;
    BCancel: TButton;
    BSave: TButton;
    CBNameCon: TComboBox;
    FileListBox1: TFileListBox;
    Label1: TLabel;
    OD: TOpenDialog;
    SD: TSaveDialog;
    Serial: TSdpoSerial;
    SynEdit: TSynEdit;
    procedure BCancelClick(Sender: TObject);
    procedure BCloseClick(Sender: TObject);
    procedure BReceiveClick(Sender: TObject);
    procedure BSaveClick(Sender: TObject);
    procedure BSendClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure SerialRxData(Sender: TObject);
  private
    { private declarations }
      IniFile : TIniFile;
    AppIni : TIniFile;
  public
    { public declarations }
  end;

var
  FormAction: TFormAction;

implementation
 uses formconfig,Formasending;
{$R *.lfm}

{ TFormAction }

procedure TFormAction.FormShow(Sender: TObject);
Var cAppDir:String;
    cAppName:String;
    cappFile:String;
    nfiles:integer;
    k:Integer;
    cFile:string;
begin
  cAppDir :=GetCurrentDir+'\';
   cAppName:=ChangeFileExt(Application.ExeName,'.ini');
   cAppFile:= Copy(cAppname,length(cAppDir)+1,length(cAppName)-length(cAppDir)+1);
   FileListBox1.Directory:=GetCurrentDir;
   FileListBox1.Mask := '*.Ini';
   nFiles := FileListBox1.Items.Count-1;
   if nFiles > 0 then begin
     for k:=0 to nFiles do
       begin
          cFile :=FileListBox1.Items[k];
          if cFile <>cAppFile then
          CBNameCon.Items.Add(copy(cFile,1,length(cFile)-4));
       end;
   end;
   AppIni := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini')) ;
   try
     CbNameCon.Text := AppIni.ReadString('Comunication','Connection Name','') ;
   finally
    AppIni.Free;
   end;
   SynEdit.ClearAll;
end;

procedure TFormAction.SerialRxData(Sender: TObject);
var RecBuff: String;
    Ls : Integer;
    cChar : String;
    newStr : String;
    cLf : AnsiChar;
    cRet : AnsiChar;
    lStart : Boolean;

begin
 cLf := #10;
 cRet := #13;
 lStart := true;
 //sleep(500);
 while Serial.DataAvailable  do
  begin
    RecBuff := Serial.ReadData;
    SynEdit.Lines.Append(RecBuff);
//    NewStr:='';
//    for ls :=0 to length( RecBuff) -1 do
//      begin
//         cChar := copy( RecBuff, ls, 1);
//         if (cchar = cLf ) or (cchar = cRet) then
//          begin
//            if (newstr <> '' ) and (lStart = True) then
//               begin
//                SynEdit.Lines.Text:=NewStr;
//                lStart :=false;
//                NewStr:='';
//               end
//           else
//              begin
//                if NewStr <> '' then begin
//                  SynEdit.Lines.Append(NewStr);
//                  NewStr:='';
//                end;
 //           end;
//          end
//         else
//           NewStr := NewStr + cChar;
//      end;
  end;
  SynEdit.Refresh;
end;

procedure TFormAction.BCloseClick(Sender: TObject);
begin
   Serial.Close;
   FormAction.Close;
end;

procedure TFormAction.BCancelClick(Sender: TObject);
begin
  Serial.Active:=false;
  Serial.SynSer.GetCommState;
  Serial.Close;
  SynEdit.SetFocus;
end;

procedure TFormAction.BReceiveClick(Sender: TObject);
var Ciclo:Integer;
    rec:String;

begin
  iniFile := TIniFile.Create(CbNameCon.Text+'.INI') ;
    try
       FormSerConfig.CBBaudRate.Text:=IniFile.ReadString(CbNameCon.Text,'BaudRate','') ;
       FormSerConfig.CBParity.Text:=IniFile.ReadString(CbNameCon.Text,'Parity','') ;
       FormSerConfig.CBDataBits.Text:=IniFile.ReadString(CbNameCon.Text,'DataBits','') ;
       FormSerConfig.CBStopBits.Text:=IniFile.ReadString(CbNameCon.Text,'StopBits','') ;
       FormSerConfig.CBHandShake.Text:=IniFile.ReadString(CbNameCon.Text,'HandShake','') ;
       FormSerConfig.CBPort.Text:=IniFile.ReadString(CbNameCon.Text,'PortName','') ;

       serial.BaudRate:=TBaudrate(FormSerConfig.CBBaudRate.ItemIndex) ;
       Serial.Parity:=TParity(FormSerConfig.CBParity.ItemIndex) ;
       Serial.DataBits:=TDataBits(FormSerConfig.CBDataBits.ItemIndex) ;
       Serial.StopBits:=TStopBits(FormSerConfig.CBStopBits.ItemIndex) ;
       Serial.FlowControl:=TFlowControl(FormSerConfig.CBHandShake.ItemIndex) ;
       Serial.Device:=FormSerConfig.CBPort.Text ;
       if IniFile.ReadString(CbNameCon.Text,'DTRCHECK','') = 'TRUE' then
          Serial.SetDTR(true )
       else
         Serial.SetDTR(false);
//     Receiving stuff
       SynEdit.ClearAll;
       Serial.Active:=true;

       while not Serial.DataAvailable  do
       begin
         inc(ciclo);
       end;

     finally
       IniFile.Free;
     end;

end;

procedure TFormAction.BSaveClick(Sender: TObject);
Var SomeTxtFile : TextFile;
begin
   SD.Title := 'Save your file';
   SD.InitialDir := GetCurrentDir+'\cncfiles\';
   SD.Filter := 'Gibbs Code Files|*.ncf|GCode Files|*.CNC|Txt Files|*.TXT';
  SD.FileName:= 'Newtest';
  SD.DefaultExt := 'ncf';
 // AssignFile(SomeTxtFile, SD.Filename) ;
  if SD.Execute then  begin
      SynEdit.Lines.SaveToFile(SD.Filename) ;
     ShowMessage('File Saved as: '+SD.FileName);
  end
  else ShowMessage('Save file was cancelled');
end;

procedure TFormAction.BSendClick(Sender: TObject);
Var    FileName:String;
    cFileName:textFile;
    Str,cFile:String;
    i:integer;
 begin

  iniFile := TIniFile.Create(CbNameCon.Text+'.INI') ;
  try
       FormSerConfig.CBBaudRate.Text:=IniFile.ReadString(CbNameCon.Text,'BaudRate','') ;
       FormSerConfig.CBParity.Text:=IniFile.ReadString(CbNameCon.Text,'Parity','') ;
       FormSerConfig.CBDataBits.Text:=IniFile.ReadString(CbNameCon.Text,'DataBits','') ;
       FormSerConfig.CBStopBits.Text:=IniFile.ReadString(CbNameCon.Text,'StopBits','') ;
       FormSerConfig.CBHandShake.Text:=IniFile.ReadString(CbNameCon.Text,'HandShake','') ;
       FormSerConfig.CBPort.Text:=IniFile.ReadString(CbNameCon.Text,'PortName','') ;

       serial.BaudRate:=TBaudrate(FormSerConfig.CBBaudRate.ItemIndex) ;
       Serial.Parity:=TParity(FormSerConfig.CBParity.ItemIndex) ;
       Serial.DataBits:=TDataBits(FormSerConfig.CBDataBits.ItemIndex) ;
       Serial.StopBits:=TStopBits(FormSerConfig.CBStopBits.ItemIndex) ;
       Serial.FlowControl:=TFlowControl(FormSerConfig.CBHandShake.ItemIndex) ;
       Serial.Device:=FormSerConfig.CBPort.Text ;
       if IniFile.ReadString(CbNameCon.Text,'DTRCHECK','') = 'TRUE' then
          Serial.SetDTR(true )
       else
         Serial.SetDTR(false);

//    Serial.Open;
    Serial.Active:=true;
    if OD.FileName ='' then
    begin
      if OD.Execute then
        cFile := OD.FileName;
        if FileExists( cFile) then begin
          SynEdit.ClearAll;
          SynEdit.Lines.LoadFromFile(cFile);
          SynEdit.Refresh;
        end ;
    end;
    FormSend.Caption:=OD.FileName;
    FormSend.Show;
    i:=0;
    FormSend.ProgressBar1.Max:=Synedit.Lines.Count;
    FormSend.StatusBar1.Panels[1].Text:=FormSerConfig.CBPort.Text+','+FormSerConfig.CbBaudrate.text+','+FormSerConfig.CbParity.Text+','+FormSerConfig.CbDataBits.text+','+FormSerConfig.CbHandShake.Text;
    while i <= Synedit.Lines.Count do
    begin
//        Str:=SynEdit.Lines.Strings[i]  ;
//        showmessage(str);
        Serial.WriteData(SynEdit.Lines.Strings[i]+#10#13);
 //       sleep(100);
        inc(i);
        FormSend.ProgressBar1.Position:=FormSend.ProgressBar1.Position+1;
        Application.Processmessages;
    end;
    FormSend.Close;
    showmessage('Transmition done');
    Serial.Close;
  finally
    Serial.Close;
  end;

end;

procedure TFormAction.Button1Click(Sender: TObject);
var cFile:String;
begin
  try
   if OD.Execute then
    cFile := OD.FileName;
     if FileExists( cFile) then begin
       SynEdit.ClearAll;
       SynEdit.Lines.LoadFromFile(cFile);
       SynEdit.Refresh;
      end ;
  except
    showmessage('Can not Open File');
  end;
end;

end.
                                     

--- End code ---

thanks all.
 

Navigation

[0] Message Index

Go to full version