Sending a sequence of bytes through synaser to a peripheral lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- Relativity · 2015-07-08 12:00 I must drive a certain peripheral from the laptop using Lazarus. The peripheral has a serial interface and I use a serial-USB connection in order to connect the serial peripheral with the laptop usb port. The peripheral accepts commands with a certain protocol and sends back answers with the same protocol. I prepare the message to be sent to the peripheral byte-by-byte; I store the message in three arrays of bytes (the message to the peripheral is made of a head, a body and an end according to its protocol) and then I send it using synaser this way: for count :=1 to 12 do   communicator.SendByte(tk[count]); // tk is an array of 12 bytes that I have prepared according to the protocol for count :=1 to 16 do   communicator.SendByte(dk[count]); // dk is an array of 16 bytes that I have prepared according to the protocol for count :=1 to 4 do   communicator.SendByte(te[count]); // te is an array of 4 bytes that I have prepared according to the protocol where communicator is an instance of TBlockSerial. Then I receive the answer similarly through:   for count :=1 to 12 do     tk[count] := communicator.RecvByte(100);   for count :=1 to 16 do     dk[count] := communicator.RecvByte(100);   for count :=1 to 4 do     te[count] := communicator.RecvByte(100); but instead of a reasonable answer I get a sequence of bytes that, translated into ASCII reads: NAK 2 UART_ERROR_CHAR_TIMEOUT where "NAK" evidently means "Not acknowledged". What can I do ? Is this the peripheral's answer ? I have read that the UART is a piece of hardware in the computer: should I infer that my message never left the computer because there was something wrong ? Thank you to anybody who can give suggestions. ---------------------------------------------------------------------- mig-31 · 2015-07-08 12:14 Hi, it will good to show here what you send to peripheral and what you should receive from peripheral. Usually it is ASCII string communication. You send ASCII string to peripheral and wait for answer. ---------------------------------------------------------------------- Thaddy · 2015-07-08 12:20 What is the baud rate you use for communication? What baud rate is supported by the peripheral? It is probably a handshake issue. I suggest to use synaser (included in synapse) and set up your code to the baud rate your device is communicating with. ---------------------------------------------------------------------- jma_sp · 2015-07-08 12:41 Hello, As Thaddy says you must configure the correct baud rate. The response is indicating a timeout. What device type is it? Under Windows / Linux? Last time i created a simple program to see how serial works with synaser under windows first i tried with hyperterminal to observe the results, but before you have to set the values of connection (baud rate, data bits, parity, stop bits, and flow control). For this you can see the values stablished in hardware administrator => in device to communicate with,  and look for port number (by example if it is a modem) and baud rate set by the driver (is important to chek if the driver is correctly installed and how his properties are configured). If it works under hyperterminal it should works under your program. Also is important to know what set of at commands it support. UART https://en.wikipedia.org/wiki/Universal_asynchronous_receiver/tra nsmitter Regards. ---------------------------------------------------------------------- Thaddy · 2015-07-08 12:44 @jma_sp: excellent addition, tnx. That should really help the issue.  If not, plz report back. ---------------------------------------------------------------------- Relativity · 2015-07-08 12:54 I connect with: communicator.config(38400,8,'N',SB1,false,false); The bytes I send to the peripheral, in decimal are: tk = {36,0,1,0,0,32,0,0,0,0,0,0} dk = {2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0} te = {6,0,46,59} The peripheral can work at 9600, 38400 or 115200 baud. I am sure that I connect with the right parameters and I confirm in particular the 38400 baud. I am working with Windows Vista. With Hyperterminal it works correctly. Thank you anyway to everybody. I use TBlockSerial of synaser. ---------------------------------------------------------------------- Thaddy · 2015-07-08 12:58 The for..to should wait for ACK *per byte* as you have written the code like that. It maybe better to send it as a string or array of bytes. ---------------------------------------------------------------------- jma_sp · 2015-07-08 14:24 By example in a simple program i have created to read IMEI, S/N.... from some mobile devices I have used this for connection: ............................................ uses .............................,Synaser; ............................................ var respuesta:ansistring; telefono:TBlockSerial; ...............................................   telefono:=TBlockSerial.Create;   telefono.Connect ('COM39');   telefono.Config (921600,8,'N',1,false,false); ...............................................   telefono.ATCommand ('              '); { Where you can complete the available ATCommand and   parámeters for your device.............}   respuesta:=telefono.ATCommand ('AT'); ...............................................   telefono.Free; ............................................... By example you can send   respuesta:=telefono.ATCommand ('AT'); and read the returned value to know if ok. In this forum there is almost two other entries about serial communication, may be thereis some aditional info about it. http://forum.lazarus.freepascal.org/index.php?topic=19094.0 Here suggest to use RecvPacket(timeout) instead of RecvByte(timeout) http://forum.lazarus.freepascal.org/index.php/topic,6697.msg31318.html #msg31318 http://forum.lazarus.freepascal.org/index.php/topic,13749.msg160285.ht ml#msg160285 http://forum.lazarus.freepascal.org/index.php/topic,27531.msg170249.ht ml#msg170249 http://forum.lazarus.freepascal.org/index.php/topic,21045.msg122767.ht ml#msg122767 ---------------------------------------------------------------------- mig-31 · 2015-07-09 08:25 Quote from: Relativity on July 08, 2015, 02:54:04 pmI connect with: communicator.config(38400,8,'N',SB1,false,false); The bytes I send to the peripheral, in decimal are: tk = {36,0,1,0,0,32,0,0,0,0,0,0} dk = {2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0} te = {6,0,46,59} The peripheral can work at 9600, 38400 or 115200 baud. I am sure that I connect with the right parameters and I confirm in particular the 38400 baud. I am working with Windows Vista. With Hyperterminal it works correctly. Thank you anyway to everybody. I use TBlockSerial of synaser. Hi, you still do not describe communication. Hyperterminal send ASCII codes with terminator char. I think terminator Char are missing. ---------------------------------------------------------------------- Relativity · 2015-07-10 11:19 It is my fault. I sent the right bytes according to the peripheral's protocol, but not in the right order, because some of the bytes I have posted belong together forming a Word or Longword and these must be sent, according to the peripheral's protocol, with the little endian convention. My bytes were arranged, by mistake, with the big endian convention. The byte having value 32 = 0x20 in the tk array meant the number of bytes the whole sent message is made of (the sum of all the bytes of the three arrays is effectively 32). But this byte belonged together with the preceding byte (= 0), forming a Word (so it is foreseen by the protocol) and I guess that the peripheral understood that the message length = 0x3200, which is a huge number and probably waited for all these bytes, that actually didn't come, because it was just a 32-bytes message, and then there was a timeout. I guess this could be the right interpretation. Now I have fixed the order of the sent bytes and it works correctly. Sorry I have no words to thank everybody for support and, most of all, precious time.