0
votes

I am very much new to Delphi Prism. In fact, you can even say I am a newbie, but I have programmed in Delphi for the last good 10 years. I was able to port our legacy software that was written in previous version of Delphi to Delphi 2010. Now, I am ready to write the same software for Delphi.Net for the WEB (ASP.NET) using Delphi Prism. For the life of me, I can't seem to understand the overall layout or idea of how Delphi Prism works. I have gone through tutorials and examples and even searched on Stackoverflow for samples and codes. Still, I can't seem to understand Delphi Prism, even though I have already spent a whole week just playing around with Delphi Prism IDE.

Someone online said that one can take a window standalone program and write it entirely for the web by just using .NET framework.

Right now I am simply trying to create a webform with SerialPort Component to communicate on the comport. I was able to design the webpage controls(buttons, labels)... The program compiles and loads with a localhost started. I see the buttons on the browser, but it won't communicate on the serialport.

I am confused. It looks like I am not doing it right. Can someone give me an example of Delphi Prism SerialPort component usage?

I am not sure if I installed it or it came with the Delphi Prism IDE. However, if you look in the toolbox under Components section after starting Delphi Prism, you will see a component called SerialPort. Apparently, it is part of Microsoft .NET Framework.

I meant to ask how one would use the serial port component that came with the Delphi Prism IDE.

Thank you much,

1
You mean communicate with a serial port on your machine, or communicate with a serial port on the server where the web application will be running? For the moment they are the same machine, but presumably when you deploy it, they won't be.Roman
@RRUZ Good Point. So, you mean to tell me any tools and components you use in your program for Delphi.NET or ASP.net is ultimately to be used by the server machine not the client machine. See, of all the readings I've done nowhere did they mention such an information. Thanks.ThN
..I meant to ask how one would use the serial port component that came with the Delphi Prism IDE. which components ? can you mention the nane of one of these components?RRUZ
I am not aware of any Serial Port components for Delphi Prism.Warren P
As far as I know, I have not installed any components to Delphi Prism IDE other than just installing Delphi Prism on my computer.ThN

1 Answers

3
votes

Instead of looking for a Delphi prism component for Serial Port , you must look for an .Net Serial Port component. You can use this article SerialPort (RS-232 Serial COM Port) in C# .NET as guide about this topic.

Now check this delphi prism class which handle the Serial Port communication (only receive data)

namespace SerialComm;

interface

uses
  System.IO.Ports, //this namespace contains classes for controlling serial ports.      
  System.Collections.Generic,
  System.Linq,
  System.Text;

type
  DataMode = (Text,Hex);

  TSerialComm = public class
  private
    CurrentDataMode     : DataMode;
    method port_DataReceived( sender : object;  e : SerialDataReceivedEventArgs);
  public
    ComPort             : SerialPort := new SerialPort();
    method OpenPort();
    method ClosePort();
    method Init;
  end;

implementation

//here you receive the data
method TSerialComm.port_DataReceived( sender : object;  e : SerialDataReceivedEventArgs);

    method  ByteArrayToHexString(data : Array of Byte) : string;
    Begin
      var sb : StringBuilder := new StringBuilder(data.Length * 3);
      for each b in data do 
      sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));
      result:=sb.ToString().ToUpper();
    end;

Var
  data :string ;
begin

    if not ComPort.IsOpen then  exit;

    try
              // text mode
              if (CurrentDataMode = DataMode.Text) then
              begin                                    
                   data  := comport.ReadExisting();    
             //do your stuff here 
              end
              else
              ///binary (hex) mode
              begin
                var bytes : Integer  := ComPort.BytesToRead;
                var buffer : Array of byte  := new byte[bytes];
                ComPort.Read(buffer, 0, bytes);
                Data:= ByteArrayToHexString(buffer);
                //do your stuff here 
              end;

    except
      on ex: exception do begin         
          OutLog('port_DataReceived: ' + ex.Message + ' ** Trace: ' + ex.StackTrace);
         exit;
      end;
    end;
end;

method TSerialComm.OpenPort();
begin
      CurrentDataMode := DataMode.Text;
      OutLog('Open Port COM');
      if ComPort.IsOpen then ClosePort();       
       ComPort.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived); 

        //ComPort.ReadTimeout:=100;
        ComPort.BaudRate := Convert.ToInt32(_Settings.BaudRate);
        ComPort.DataBits := Convert.ToInt32(_Settings.DataBits);
        var  aStopBits : StopBits  := StopBits(Enum.Parse(typeof(StopBits), _Settings.StopBits, true));
        ComPort.StopBits           := aStopBits;
        var  aParity : Parity      := Parity(Enum.Parse(typeof(Parity), _Settings.Parity, true));
        ComPort.Parity             := aParity;
        ComPort.PortName := _Settings.PortName;
        ComPort.Open();
        if ComPort.IsOpen then
         OutLog('Port '+ComPort.PortName+' Open')
        else
         OutLog('was not possible open the port '+ComPort.PortName);
end;

method TSerialComm.ClosePort();
begin
    if ComPort.IsOpen then
    begin
      ComPort.DataReceived +=nil;
      ComPort.ReadExisting;
      ComPort.Close();
    end;    
end;

method TSerialComm.Init;
begin 
 ComPort.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
end;

Note 1 : the OutLog is just a function to log , you can comment the calls to this function.

Note 2 : This code must run from the server side.