how to send string hex lazarus / 21-General ---------------------------------------------------------------------- microguy · 2022-02-07 02:25 I am trying to see string Hex. It is not working. It is for Modbus. Here is my code. I tried three different ways. None of them are working. TempStr: $o1$06$00... TempStr: $01+$06+$00 ..... TempStr: chr($01)+chr($06)+chr($00).... Let you know I have Modbus on Arduino. I use the Simply Modbus Master program to run it and it is working with Arduino. It is a demo program.  I need to send 01 06 00 00 00 01 48 0A to turn on the LED. Turn off the LED use this code 01 0600 00 00 00 89 CA. How can I send HEX to Arduino? See code what I have. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.FormCreate(Sender: TObject);begin  ser := TBlockSerial.Create;  Sleep(25);  ser.Connect('COM3');   Sleep(25);   ser.Config(9600, 8, 'N', SB1, False, False);  ser.RTS := false; // comment this if needed  ser.DTR := false; // comment this if neededend;   procedure TForm1.Button38Click(Sender: TObject);var tempstr: string;begin//  TempStr:= $01$06$00$00$00$00$89$CA;//  TempStr:= $01+$06+$00+$00+$00+$00+$89+$CA;  TempStr:= chr($01)+chr($06)+chr($00) +chr($00)+chr($00)+chr($00)+chr($89)+chr($CA);   Ser.SendString(TempStr);  Shape74.brush.Color:=clRed;end; procedure TForm1.Button39Click(Sender: TObject);var tempstr: string;begin//  TempStr:= $01$06$00$00$00$01$89$CA;//  TempStr:= $01+$06+$00+$00+$00+$01+$89+$CA;  TempStr:= chr($01)+chr($06)+chr($00) +chr($00)+chr($00)+chr($01)+chr($89)+chr($CA);  Ser.SendString(TempStr);  Shape74.brush.Color:=clGreen; end; ---------------------------------------------------------------------- dbannon · 2022-02-07 02:53 Pure speculation microguy.  Pascal strings are quite happy holding 'binary data' but maybe the serial unit you are using, TBlockSerial and its SendString may not like getting passed eg $00 and is treating it as a control character. Perhaps.  Might be worthwhile following that code back and see what it is actually sending. Does the same unit have a method to send an array of char ? Davo ---------------------------------------------------------------------- tetrastes · 2022-02-07 07:22 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var buf: array[1..N] of byte;...if NumBytesToSend <= N then     Ser.SendBuffer(@buf, NumBytesToSend); ---------------------------------------------------------------------- Josh · 2022-02-07 09:10 hi as the code to sendstring is Code: [Select]procedure TBlockSerial.SendString(data: AnsiString); begin   SendBuffer(Pointer(Data), Length(Data)); end; i suspect you hitting the lssues with control chars notabily null chars; check out https://wiki.freepascal.org/Character_and_string_types#AnsiString, an ansistring is terminated with $00 it would be adviced to use sendbuffer directly; as tetrastes advices ---------------------------------------------------------------------- Jurassic Pork · 2022-02-07 10:07 hello, have a look to the demo test program of my component TLazserial(using synaser). Example : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Str := Str + inttohex(checksum(Str),2)+ Char(13)+ Char(10);;  Serial.WriteData(Str);   WriteData  use sendString. Not sure that your values for RTS and DTR are OK --> try to comment those lines. Friendly, J.P ---------------------------------------------------------------------- mig-31 · 2022-02-07 11:01 Why you don't use a PascalScada, which already implemented MODBUS RTU/ASCII over RS-232/485/422 and MODBUS TCP/IP? ---------------------------------------------------------------------- Josh · 2022-02-07 11:05 Hi Just knocked this up; have not tested as have no serial devices to test; Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type   TByteBuff=Array of Byte; var   tosend:TByteBuff;  function HexStringToBuff(Const AString:AnsiString):TByteBuff;Var S:AnsiString='';  L,P,C:longint;  v:byte;begin  If AString='' then exit;  // remove any $ or spaces  for L:=1 to length(AString) do if AString[L] in ['$',' '] = false then S:=S+AString[L];  // hex string is in packs of 2 chars  L:=length(S);  setlength(Result,L div 2);  P:=1;  c:=0;  while l-p>1 do  begin    v:=StrToInt('$' + copy(s,p,2));    inc(p,2);    result[c]:=v;    inc(c);  end;End; begin  tosend:=HexStringToBuff('$00 01 $05 0399 ff fe 00');  // spaces and $ removed so the string is tidied  ser.SendBuffer(@tosend,length(tosend));end; ---------------------------------------------------------------------- tetrastes · 2022-02-07 12:41 Quote from: josh on February 07, 2022, 10:10:24 amhi as the code to sendstring is Code: [Select]procedure TBlockSerial.SendString(data: AnsiString); begin   SendBuffer(Pointer(Data), Length(Data)); end; Yes, thank you, I didn't check it myself. But this means that my advice is not needed. Quotei suspect you hitting the lssues with control chars notabily null chars; check out https://wiki.freepascal.org/Character_and_string_types#AnsiString, an ansistring is terminated with $00 AnsiStrings can contain null chars, as they contain the length, and length of OP's TempStr is 8. So his code should work. I think the problem is incorrect data. For example, he writes QuoteI need to send 01 06 00 00 00 01 48 0A to turn on the LED.but sends 01 06 00 00 00 01 89 CA instead. ---------------------------------------------------------------------- dbannon · 2022-02-08 08:30 Quote from: tetrastes on February 07, 2022, 01:41:55 pmAnsiStrings can contain null chars, Yes, as already stated above. Quote from: tetrastes on February 07, 2022, 01:41:55 pm.... For example, he writes QuoteI need to send 01 06 00 00 00 01 48 0A to turn on the LED.but sends 01 06 00 00 00 01 89 CA instead. Indeed, I missed that. Might be a copy and paste error .... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i OnStr := chr(1)+chr(6)+chr(0)+chr(0)+chr(0)+chr(1)+chr($48)+chr($0A);OffStr := chr(1)+chr(6)+chr(0)+chr(0)+chr(0)+chr(1)+chr($89)+chr($CA);Just to be clear, quite different to what OP was using. Davo ---------------------------------------------------------------------- microguy · 2022-02-23 23:04 Sorry for taking so long to respond. Josh, I tried your code for the function HexStringToBuff(Const AString:AnsiString):TByteBuff; and it did not work with the code. I use both codes 01 06 00 00 00 01 48 0A  and 01 06 00 00 00 89 CA they did not work. mig-31, I have PascalScada on my computer but I couldn't understand how to use it or could not get it working with Arduino. Do you know where I can find an instruction on how to use it with Arduino?