Hi,
I'm using Lazarus with LazSerial 0.5. I want to connect to an Arduino UNO board.
The Arduino board currently has a program that expects a text (command) from the serial port and responds with a text (which varies depending on the text received).
Using the serial monitor included with the Arduino IDE, everything works perfectly. When I try from Lazarus, the text is sent but I don't get a response. Thinking about the handshake configuration, speed, frame format, etc, I started trying all the variants and there was no case. I even tried receiving through an event (in the version I show here, I wait to receive the data with a timeout).
Then, I made the same program but using AutoIt3 scripting language with CommAPI library and it works perfect.
Using a serial port monitor, I captured the connection made by the Arduino IDE, AutoIt3 and Lazarus (knowing that the first 2 work fine). I don't understand much about the serial port parameters but I think the differences between the versions that worked and the one that didn't, was only in the IOCTL_SERIAL_SET_TIMEOUTS structure. How can I configure this structure from LazSerial? Or do I have to modify the source code?
The fragment of Lazarus code I'm using is:
function Look(sPort : String) : Boolean
var
sResponse : String;
bError : Boolean;
t0 : UInt64;
begin
Result := False;
sResponse := '';
bError := False;
//I configured LazSerial control to 9600,8N1,FlowControl None (and tested with Hardware and XonXoff also), RcvLineCRLF=false, rest to default
frmMain.Serial.Device := sPort;
try
frmMain.Serial.Open;
except
bError := True;
end;
if not bError then
begin
Sleep(500); //in a first version, I don't put this delay. I get the same result.
frmMain.Serial.WriteData('V' + #10);
t0 := GetTickCount64();
repeat
if frmMain.Serial.DataAvailable then sResponse := sResponse + frmMain.Serial.ReadData;
until (GetTickCount64() - t0) > 1000;
if sResponse = ('OK'+#10) then Result := True;
frmMain.Serial.Close;
end;
end;
Here is the captured data in each case:
AutoIt3 w/CommAPI library:
--------------------------
IOCTL_SERIAL_SET_BAUD_RATE: Retrieve Baud Rate
· Baud Rate = 9600
IOCTL_SERIAL_CLR_RTS: Clear RTS
IOCTL_SERIAL_CLR_DTR: Clear DTR
IOCTL_SERIAL_SET_LINE_CONTROL: Set line control
· Stop bits = 1
· Parity = No
· Data bits = 8
IOCTL_SERIAL_SET_CHARS: Set special characters
· EOF = 0
· Error = 0
· Break = 0
· Event = 0
· XON = 0
· XOFF = 0
IOCTL_SERIAL_SET_HANDFLOW: Set handshake information
· Control lines = 0
· Flow control = 0
· Xon Limit = 0
· Xoff Limit = 0
IOCTL_SERIAL_SET_TIMEOUTS: Set timeouts
· Read interval = 0
· Read total multiplier = 1
· Read total constant = 0
· Write total multiplier = 1
· Write total constant = 0
IOCTL_SERIAL_PURGE: Purge requests
· Mask = SERIAL_PURGE_TXABORT | SERIAL_PURGE_RXABORT | SERIAL_PURGE_TXCLEAR | SERIAL_PURGE_RXCLEAR (0xf)
Data 0x56 0x0A sent
Data response ok
The port has been closed
Arduino IDE Serial Monitor:
---------------------------
IOCTL_SERIAL_SET_BAUD_RATE: Retrieve Baud Rate
· Baud Rate = 9600
IOCTL_SERIAL_SET_RTS: Set RTS
IOCTL_SERIAL_SET_DTR: Set DTR
IOCTL_SERIAL_SET_LINE_CONTROL: Set line control
· Stop bits = 1
· Parity = No
· Data bits = 8
IOCTL_SERIAL_SET_CHARS: Set special characters
· EOF = 0xcc
· Error = 0x5c
· Break = 0x5c
· Event = 0x89
· XON = 0x11
· XOFF = 0x13
IOCTL_SERIAL_SET_HANDFLOW: Set handshake information
· Control lines = SERIAL_DTR_CONTROL (0x1)
· Flow control = SERIAL_RTS_CONTROL | SERIAL_XOFF_CONTINUE (0x80000040)
· Xon Limit = 2048
· Xoff Limit = 512
IOCTL_SERIAL_SET_TIMEOUTS: Set timeouts
· Read interval = 0
· Read total multiplier = 0
· Read total constant = 0
· Write total multiplier = 0
· Write total constant = 0
IOCTL_SERIAL_SET_WAIT_MASK: Set wait mask
· Wait Mask = SERIAL_EV_RXCHAR (0x1)
Data 0x56 0x0A sent
Data response ok
The port has been closed
LazSerial with FlowControl=fcNone:
--------------------------------------
IOCTL_SERIAL_SET_WAIT_MASK: Set wait mask
· Wait Mask = 0
IOCTL_SERIAL_SET_QUEUE_SIZE: Set queue size
· Input = 4096
· Output = 0
IOCTL_SERIAL_SET_TIMEOUTS: Set timeouts
· Read interval = 65535
· Read total multiplier = 0
· Read total constant = 0
· Write total multiplier = 0
· Write total constant = 0
IOCTL_SERIAL_SET_RTS: Set RTS
IOCTL_SERIAL_SET_DTR: Set DTR
IOCTL_SERIAL_PURGE: Purge requests
· Mask = SERIAL_PURGE_TXABORT | SERIAL_PURGE_RXABORT | SERIAL_PURGE_TXCLEAR | SERIAL_PURGE_RXCLEAR (0xf)
IOCTL_SERIAL_SET_BAUD_RATE: Retrieve Baud Rate
· Baud Rate = 9600
IOCTL_SERIAL_SET_RTS: Set RTS
IOCTL_SERIAL_SET_DTR: Set DTR
IOCTL_SERIAL_SET_LINE_CONTROL: Set line control
· Stop bits = 1
· Parity = No
· Data bits = 8
IOCTL_SERIAL_SET_CHARS: Set special characters
· EOF = 0xe5
· Error = 0xc5
· Break = 0xc5
· Event = 0
· XON = 0x11
· XOFF = 0x13
IOCTL_SERIAL_SET_HANDFLOW: Set handshake information
· Control lines = SERIAL_DTR_CONTROL (0x1)
· Flow control = SERIAL_RTS_CONTROL (0x40)
· Xon Limit = 1024
· Xoff Limit = 1024
A lot of IOCTL_SERIAL_GET_COMMSTATUS and IOCTL_SERIAL_SET_WAIT_MASK with the data 0x56 0x0A sent.
IOCTL_SERIAL_CLR_RTS: Clear RTS
IOCTL_SERIAL_CLR_DTR: Clear DTR
The port has been closed
LazSerial with FlowControl=fcXonXoff:
-------------------------------------
IOCTL_SERIAL_SET_HANDFLOW: Set handshake information
· Control lines = SERIAL_DTR_CONTROL (0x1)
· Flow control = SERIAL_AUTO_TRANSMIT | SERIAL_AUTO_RECEIVE | SERIAL_RTS_CONTROL (0x43)
· Xon Limit = 1024
· Xoff Limit = 1024
LazSerial with FlowControl=fcHardware:
--------------------------------------
IOCTL_SERIAL_SET_WAIT_MASK: Set wait mask
· Wait Mask = 0
IOCTL_SERIAL_SET_QUEUE_SIZE: Set queue size
· Input = 4096
· Output = 0
IOCTL_SERIAL_SET_TIMEOUTS: Set timeouts
· Read interval = 65535
· Read total multiplier = 0
· Read total constant = 0
· Write total multiplier = 0
· Write total constant = 0
IOCTL_SERIAL_SET_RTS: Set RTS
IOCTL_SERIAL_SET_DTR: Set DTR
IOCTL_SERIAL_PURGE: Purge requests
· Mask = SERIAL_PURGE_TXABORT | SERIAL_PURGE_RXABORT | SERIAL_PURGE_TXCLEAR | SERIAL_PURGE_RXCLEAR (0xf)
IOCTL_SERIAL_SET_BAUD_RATE: Retrieve Baud Rate
· Baud Rate = 9600
IOCTL_SERIAL_SET_DTR: Set DTR
IOCTL_SERIAL_SET_LINE_CONTROL: Set line control
· Stop bits = 1
· Parity = No
· Data bits = 8
IOCTL_SERIAL_SET_CHARS: Set special characters
· EOF = 0xe5
· Error = 0xc5
· Break = 0xc5
· Event = 0
· XON = 0x11
· XOFF = 0x13
IOCTL_SERIAL_SET_HANDFLOW: Set handshake information
· Control lines = SERIAL_DTR_CONTROL | SERIAL_CTS_HANDSHAKE (0x9)
· Flow control = SERIAL_RTS_HANDSHAKE (0x80)
· Xon Limit = 1024
· Xoff Limit = 1024
A lot of IOCTL_SERIAL_GET_COMMSTATUS and IOCTL_SERIAL_SET_WAIT_MASK with the dta 0x56 0x0A sent.
IOCTL_SERIAL_CLR_RTS: Clear RTS
IOCTL_SERIAL_CLR_DTR: Clear DTR
The port has been closed