Lazarus Hardware Interruptions lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- hac3ru · 2013-07-09 08:44 Hello. Sorry if I'm not posting in the right section but I didn't find one to fit... My question: Does Lazarus know about hardware interrupts? And if the answer is positive, can anyone give me some directions? Thanks in advance. ---------------------------------------------------------------------- BigChimp · 2013-07-09 08:48 I suppose it "knows" as much as the underlying operating system will allow it. For more info, see: http://wiki.lazarus.freepascal.org/Hardware_Access Hint: the wiki has *lots* of info ---------------------------------------------------------------------- hac3ru · 2013-07-09 08:50 That didn't help... I'll be back! With some more details ---------------------------------------------------------------------- Leledumbo · 2013-07-09 09:06 You need to provide more details since "knows" could mean anything. Kernels in Pascal will of course deal with interrupts, but still we don't know what you need from that. ---------------------------------------------------------------------- hac3ru · 2013-07-09 09:49 Before that.... This code dies... I want to send something, anything since it's the first time I'm doing this, to the RS232 interface Code: [Select]procedure TForm1.Button1Click(Sender: TObject);     var      x:integer;   begin        Timer1.Enabled:=True;        try           ser:=TBlockSerial.Create;           ser.Connect('COM7'); //ComPort           ser.config(1200, 7, 'N', SB1, False, False);           Label2.Caption:=('Device: ' + ser.Device + '   Status: ' + ser.LastErrorDesc +' '+Inttostr(ser.LastError));           except ShowMessage('Could not connect');           end;   end; procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction); begin      try    ser.free;      except showmessage('NotFree');      end; end; procedure TForm1.FormCreate(Sender: TObject); begin   RS232_connect() //unit1.pas(69,16) Error: Identifier not found "RS232_connect" ?????? ???? ???? end; procedure TForm1.Timer1Timer(Sender: TObject); begin   Write(IntToHex(ser.RecvByte(10000), 2), ' '); end; end.I have created a form with a button and two labels... One of the labels will indicate the status.... I click the button, i get Status: OK but after that it dies. It just freezes.... No data sent to RS232 Ideas? Actually, if I leave it be for 20 - 30 seconds, it'll return an Error "File not open." ---------------------------------------------------------------------- irfanbagus · 2013-07-09 10:32 Quote from: hac3ru on July 09, 2013, 11:49:54 amBefore that.... This code dies... I want to send something, anything since it's the first time I'm doing this, to the RS232 interface Code: [Select]procedure TForm1.Button1Click(Sender: TObject);     var      x:integer;   begin        Timer1.Enabled:=True;        try           ser:=TBlockSerial.Create;           ser.Connect('COM7'); //ComPort           ser.config(1200, 7, 'N', SB1, False, False);           Label2.Caption:=('Device: ' + ser.Device + '   Status: ' + ser.LastErrorDesc +' '+Inttostr(ser.LastError));           except ShowMessage('Could not connect');           end;   end; procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction); begin      try    ser.free;      except showmessage('NotFree');      end; end; procedure TForm1.FormCreate(Sender: TObject); begin   RS232_connect() //unit1.pas(69,16) Error: Identifier not found "RS232_connect" ?????? ???? ???? end; procedure TForm1.Timer1Timer(Sender: TObject); begin   Write(IntToHex(ser.RecvByte(10000), 2), ' '); end; end.I have created a form with a button and two labels... One of the labels will indicate the status.... I click the button, i get Status: OK but after that it dies. It just freezes.... No data sent to RS232 Ideas? Actually, if I leave it be for 20 - 30 seconds, it'll return an Error "File not open." 'File not open' error because you try to call write ( Write(IntToHex... ) from gui application. uncheck 'win32 gui application' in project options. ---------------------------------------------------------------------- hac3ru · 2013-07-09 10:56 Well... I kinda need it to be a WIN32 App.... And it's not giving me an error anymore but still not sending anything... I got a USB to RS232 adapter with 2 leds on it.... RXD and TXD which should light if data is transfered..... Any ideas? Thanks for wasting time with me  //Working. I did ser.SendByte(x) // X being incremented by a counter. My question is: Does it convert that X to Byte? I first did ser.SendByte(0000 0001)... After that I tried with X and it worked.... So... If i do ser.SendByte(x) //x being 1 at first// it'll send 0000 0001? Ser.SendByte(x)// x=2// send 0000 0010? And so on? And also... Can I send a word or something? Thank you ---------------------------------------------------------------------- Jurassic Pork · 2013-07-09 11:57 hello hac3ru, why dont' you use a serial port lazarus component to do this (TLazSerial or TSdpoSerial) ? ---------------------------------------------------------------------- hac3ru · 2013-07-09 12:10 I'll give it a shot. Is it easier to work with? Also... I was trying to convert from char to ascii value using ord(edit1.text); i going from 1 to length(edit1.text).... It's giving me an error: Code: [Select]unit1.pas(85,55) Error: Incompatible type for arg no. 1: Got "Byte", expected "TTranslateString" Ermm... How do you send data to the RS232 using LazSerial? I have added an LazSerial entity, or whatever the name would be, I configured it's device as COM7 but when I try to run the application, I get "Error reading LS1.Active: Could not open device \\COM1".... Why COM1? Later Edit: Never Mind... I fixed the Communication problem. The ord(char) still remains. Anybody any ideas? ---------------------------------------------------------------------- Jurassic Pork · 2013-07-09 15:03 hac3ru , put your code in a reply to see    what is wrong. ---------------------------------------------------------------------- hac3ru · 2013-07-09 15:32 Communication's alright now but the ord(char) returns the error written above.... There's not much of a code. A textbox, a label and a button... Code: [Select]Error: Incompatible type for arg no. 1: Got "Byte", expected "TTranslateString" ---------------------------------------------------------------------- Jurassic Pork · 2013-07-09 16:11 what do you want to send ?  a string ? a buffer of bytes ? a char ? a byte ? ord  only available for a char not for a string. ---------------------------------------------------------------------- hac3ru · 2013-07-09 16:47 I wanted to transform a string into an array of chars... So... I did: Code: [Select]for i:=1 to Length(Edit1.text)    begin       arr[i]:=ord(Edit1.text[i]);    end; And that returned an error.... ---------------------------------------------------------------------- Jurassic Pork · 2013-07-10 00:05 you must cast  byte to char to avoid this error : Code: [Select] arr[i]:=Char(ord(Edit1.text[i]));but it is easyer to work with array of bytes or string Here is three ways to send 'ABCDEFGHIJKLMNOP' + CRLF  to a serial port : Code: [Select]procedure TForm1.Button7Click(Sender: TObject); var buffer : array of Byte;     arr : array of Byte;     List1: TStringList;     MyString: String;     x: integer; begin     List1 := TStringList.Create;     List1.Delimiter := ',';     // ABCDEFGHIJKLMNOP   hexa  value comma delimited     List1.DelimitedText := '41,42,43,44,45,46,47,48,49,4A,4B,4C,4D,4E,4F,50,0D,0A';     SetLength(buffer,List1.Count);     for x:= 0 to List1.Count -1 do     begin     buffer[x] := StrtoInt('$' + List1[x]);     end;     //Edit1.text contains ABCDEFGHIJKLMNOP     SetLength(arr,Length(Edit1.Text) + 2);     for x:=1 to Length(Edit1.text) do      begin       arr[x-1]:= ord(Edit1.text[x]);      end;      arr[x] := 13;      arr[x+1] := 10;    myString := Edit1.Text + #13 + #10;   LazSerial1.Open;     LazSerial1.WriteBuffer(buffer[0],List1.count);     LazSerial1.WriteBuffer(arr[0],Length(arr));     LazSerial1.WriteData(myString);   List1.Free; end;  Friendly, J.P ---------------------------------------------------------------------- hac3ru · 2013-07-10 06:32 Thank you! Got a question though: Why do you use LazSerial1.WriteBuffer? Why can't you just go to Laz1.WriteData? Also, I get a warning that Arr is not initialized and the application crashes.... Nvm got it //Later edit: This code works: Code: [Select]           SetLength(arr,Length(edit1.text)+1);            Memo1.Clear;            Memo1.Lines.Add('Chars as ASCII Code');            for i:=1 to Length(Edit1.Text) do                begin                     arr[i]:=ord(Edit1.text[i]);                     Memo1.Lines.Add(Edit1.text[i]+'='+IntToStr(arr[i]));                end;This doesn'tCode: [Select]           SetLength(arr,Length(edit1.text));            Memo1.Clear;            Memo1.Lines.Add('Chars as ASCII Code');            for i:=1 to Length(Edit1.Text) do                begin                     arr[i]:=ord(Edit1.text[i]);                     Memo1.Lines.Add(Edit1.text[i]+'='+IntToStr(arr[i]));                end;Why does it need to be length+1? What am I missing here? The funny part is that it crashes only if I insert into the textbox "aaa" and after that I add another "d" so it will be "aaad". If i insert "aaa" and after that "aaax" it works... Anyone got a clue? ---------------------------------------------------------------------- Jurassic Pork · 2013-07-10 06:38 because you wrote : QuoteI wanted to transform a string into an array of chars... ---------------------------------------------------------------------- hac3ru · 2013-07-10 07:44 And... What's the buffer used for? :"> Why WriteBuffer and not go with WriteData? What's the difference? Sorry if it's a newbie question... Kinda of a newbie here I want to send a command to a microprocessor.. I need to send just strings because I will process those strings using the microprocessor... So? Can i go LazSerial1.WriteData"command for microprocessor"; ? The Cpu will get the string and I can program that cpu to do something acording to what is being written in there, right? ---------------------------------------------------------------------- Jurassic Pork · 2013-07-10 08:32 use WriteData if it is string command    ex : 'StartCpu' use WriteBuffer if it is binary command  ex: (in hexa  01 00 2B 12 4C) ---------------------------------------------------------------------- hac3ru · 2013-07-10 09:03 Oww Thank you. Have a nice day mate ---------------------------------------------------------------------- hac3ru · 2013-07-10 17:35 I have one more question: Is there any way to show an error message if LazSerial1.Open or LazSerial1.Active:=True can't complete? ---------------------------------------------------------------------- Jurassic Pork · 2013-07-10 21:45 Add exception management. Example : Code: [Select] try     LazSerial1.Open;     LazSerial1.WriteBuffer(buffer[0],List1.count);     LazSerial1.WriteBuffer(arr[0],Length(arr));     LazSerial1.WriteData(myString);   except   on E: Exception do        ShowMessage('Error on serial port ' +        LazSerial1.Device + ' : ' + E.message);  //Show Exception   end;    LazSerial1.Close;    List1.Free;            Friendly, J.P ---------------------------------------------------------------------- hac3ru · 2013-07-11 07:08 On button1Click Code: [Select] try      LS1.WriteData('RAW');   except on E:exception do        begin          showmessage ('Error on serial port '+LS1.Device+':'+E.message);        end;   end;        It's not doing anything. I opened the connection, sent ('RAW' ) and it worked. I unplugged the rs232 and tried to send it again. No error message I got it to work. Thanks again ---------------------------------------------------------------------- hac3ru · 2013-07-15 14:47 Got another question: I got a string of hex... I mean, i got a string like Code: [Select]var hex:string; begin hex:='001f2d4c'; //used for example endI want to send that hex code using LazSerial1.WriteBuffer... How can I do that? Been struggling for 2 hours.... Code: [Select]       list1:=TStringList.Create;        List1.Delimiter := ',';        i:=1;        begin           List1.DelimitedText:=concat(List1.DelimitedText,msg[i],msg[i+1]);           if i it depends of the hardware ( target, handshake  etc...) ---------------------------------------------------------------------- hac3ru · 2013-07-17 08:52 Ohh okay... Now... I have a TrackBar that has min:=1 and max:=2. The problem is that when I change the position of the trackbar, it sends the data two times. I am using that Trackbar as an on/off switch. If 1 then ON else Off, but it is sending data twice => message gets sent twice. Any ideas? Got IT ---------------------------------------------------------------------- hac3ru · 2013-08-06 12:26 I've got a question: How is LazSerial.ReadData working? If I do: Code: [Select]on RXdata var Receive:string; Receive:=LazSerial1.ReadData; Showmessage (Receive); It shows ???<>:{@$? Characters... The target is sending a hexa string I have also tried Code: [Select] On RXData recv:=''; recv:=LazSerial1.ReadData; Receive:=concat(receive,recv); ShowMessage(receive);When I tried this, I don't even see wierd characters.... It's just empty... ---------------------------------------------------------------------- Jurassic Pork · 2013-08-06 14:46 hello hac3ru, have you read this message ---------------------------------------------------------------------- hac3ru · 2013-08-06 20:17 Yup... But still... Shouldn't I be able to show SOMETHING? Like... The integer numbers? I will try it in the morning and see if it works. Thank you for your time mate. And .... Am I getting the entire message using ReadData? I mean, the target is sending me ff02dcf235618facb8148b for example. ReadData will get all that in one go? Code: [Select] procedure TForm1.LazSerial1RxData(Sender: TObject); var   rcv,received: string; var   i: integer; begin   rcv:='';   Form2.Label1.Caption := 'Something was received. Check memo1 to see what.';   rcv :=LazSerial1.ReadData;   for i:=1 to Length(rcv) do     received:=received + IntToHex(Ord(rcv[i]),2) + ' ';   Form2.Memo1.Lines.Add('String received is:' + rcv); end;     Still not working I am getting those chars ..... If the target machine is sending only 1 character at a time, it is working. If it is sending a hexa string, I can't "decode" it... Okay. Taking my words back. It is working but .... It's too slow. The target is sending data very fast and I can't get it fast enough .... If the target is sending 1 char at a time, I can see it correctly. If it sends multiple, it won't work Any ideas? ---------------------------------------------------------------------- hac3ru · 2013-08-07 08:21 Jurassic Pork thank you for your time. If you want, you can add the following function in your LazSerial com library: Code: [Select]function TLazSerial.ReadByte: Byte; begin   if FSynSer.Handle=INVALID_HANDLE_VALUE then     ComException('can not read from a closed port.');   result:=FSynSer.RecvByte(0) end; Using this function, you get one byte at a time and this seems to have fixed my problem. It can be used on event OnRXData. I am using it like Code: [Select]procedure TForm1.LazSerial1RxData(Sender: TObject); var   rcv: string;   rcvbyte:byte; begin   rcvbyte:=LazSerial1.ReadByte;   rcv:=rcv + IntToStr(rcvbyte);   Form2.Memo1.Lines.Add('Received: '+rcv); end;   I get in Memo1 the correct message. Seems to be all good. As I said, if you want, you can include it into your library. Maybe it will help someone. ---------------------------------------------------------------------- Jurassic Pork · 2013-08-07 08:40 have you tried with property RcvLineCRLF of the component tlazserial to false. In this case onrxdata use recvpacket not recvString. For binary datas it is what you need. ---------------------------------------------------------------------- hac3ru · 2013-08-07 13:22 Yup. It was always set to false. Don't know why it wasn't working. I'm just glad I got it to work Thanks for help mate