Home SerialPort.Write equivalent for Delphi CommPort library method
Reply: 0

SerialPort.Write equivalent for Delphi CommPort library method

ThorPowerx
1#
ThorPowerx Published in 2018-01-10 15:10:07Z

I'm translating some code from Delphi to C#, the problem is this code uses a Delphi library for communicating over serial port interface, and I've never used the VS SerialPort library so I don't know how to exactly translate some methods.

I think SerialPort.Write is what I'm looking for, but I'm not entirely sure, so I'd like to get some confirmations.

Here is the Delphi code from the SPComm library:

//
//  FUNCTION: WriteCommData(PChar, Word)
//
//  PURPOSE: Send a String to the Write Thread to be written to the Comm.
//
//  PARAMETERS:
//    pszStringToWrite     - String to Write to Comm port.
//    nSizeofStringToWrite - length of pszStringToWrite.
//
//  RETURN VALUE:
//    Returns TRUE if the PostMessage is successful.
//    Returns FALSE if PostMessage fails or Write thread doesn't exist.
//
//  COMMENTS:
//
//    This is a wrapper function so that other modules don't care that
//    Comm writing is done via PostMessage to a Write thread.  Note that
//    using PostMessage speeds up response to the UI (very little delay to
//    'write' a string) and provides a natural buffer if the comm is slow
//    (ie: the messages just pile up in the message queue).
//
//    Note that it is assumed that pszStringToWrite is allocated with
//    LocalAlloc, and that if WriteCommData succeeds, its the job of the
//    Write thread to LocalFree it.  If WriteCommData fails, then its
//    the job of the calling function to free the string.
//
//
function TComm.WriteCommData( pDataToWrite: PChar; dwSizeofDataToWrite: Word ): Boolean;
var
   Buffer: Pointer;
begin
     if (WriteThread <> nil) and (dwSizeofDataToWrite <> 0) then
     begin
          Buffer := Pointer(LocalAlloc( LPTR, dwSizeofDataToWrite+1 ));
          Move( pDataToWrite^, Buffer^, dwSizeofDataToWrite );
          FSendDataEmpty := False;
          if PostThreadMessage( WriteThread.ThreadID, PWM_COMMWRITE,
                                WPARAM(dwSizeofDataToWrite), LPARAM(Buffer) ) then
          begin
               Result := True;
               Exit
          end
     end;

     Result := False
end; {TComm.WriteCommData}

Delphi code:

    const
  baudrate = 1200;  // all controllers should operate properly at 1200
  rdIgnore = 0;     // marker - ignore further received data
  rdSingle = 1;     // marker - single block read
  wrSingle = 2;     // marker - single block write
  rdAll    = 3;     // marker - full flash read
  wrAll    = 4;     // marker - full flash write
  rdGEN    = 5;     // marker - General data block read
  GEN = $51;        // used in device read/write
  BAS = $52;        // used in device read/write
  PAS = $53;        // used in device read/write
  THR = $54;        // used in device read/write

procedure TMainForm.btnPortConnectClick(Sender: TObject);
begin
  // Connect to COM port
  CommPort.CommName := cbbPort.Text;
  CommPort.BaudRate := baudrate;
  CommPort.StartComm;
  // Enable disconnect button and search for controller
  TimerConnect.Enabled      := false;
  btnPortDisconnect.Enabled := true;
  btnPortConnect.Enabled    := false;
  next_op := rdGEN;
  ConnectDev;
end;

// Connect to controller
procedure ConnectDev;
var
  byteSendData: array[0..12] of byte;
begin
  byteSendData[0] := $11; // Read command
  byteSendData[1] := $51; // Read General location
  byteSendData[2] := $04; // Data bytes count ???
  byteSendData[3] := $B0; // Unknown
  byteSendData[4] := (byteSendData[1] + byteSendData[2] + byteSendData[3]) Mod 256; // Data verification byte

  // Send command
  sleep(80);
  if not MainForm.CommPort.WriteCommData(@byteSendData,5) then
    Application.MessageBox('Cannot connect to controller!', 'ERROR', MB_ICONERROR + mb_OK)
  else // If not working try reconnecting to controller
    begin
      sleep(80);
      MainForm.CommPort.BaudRate  := Main.baudrate;
      MainForm.TimerConnect.Enabled := true;
    end;
end;

My C# translation:

serialPort1.PortName = COMPort.SelectedItem.ToString();
serialPort1.BaudRate = baudrate;

serialPort1.Open(); // CommPort.StartComm;
nextOperation = rdGEN;

// CONNECT TO CONTROLLER
byte[] byteSendData = new byte[12];
byteSendData[0] = 0x11; // Read command
byteSendData[1] = 0x51; // Read General location
byteSendData[2] = 0x04; // Data bytes count ???
byteSendData[3] = 0xB0; // Unknown
byteSendData[4] = Convert.ToByte((byteSendData[1] + byteSendData[2] + byteSendData[3]) % 256); // Data verification byte
// Send command
System.Threading.Thread.Sleep(80);
serialPort1.Write(byteSendData, 0, 5); // ConnectDev
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.313027 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO