Post by Tasp on Feb 14, 2021 13:22:27 GMT -5
Hi all,
I am finding txing UDP data rather sporadic. I know its a fire and forget protocol but so far I haven't had any issues if I rx data first, then return to the same IP and port. However this is opening then sending, doesn't require a response or an ACK.
The code below sits as a standalone EXE and waits for LB to run it and pass a string$ using CommandLine$ (this bit works).
It then parses that CommandLine and decides whether it's Serial, UDP or TCP (TCP not added yet....).
For testing this currently tx's the data to another Serial/UDP/TCP test program Hercules, it's running on the same computer, I thought originally the issue could be that it's because it's on the same machine, however, I'm able to consistently send UDP data from Packet Sender using the same details. IE. I can constantly hit the send button on Packet Sender and it's rx'd on Hercules everytime, doesn't miss a beat.
This code however only seems to be rx'd 6-8 times out of 10, sometimes less.
Code requires LBNet DLL by Chris Inverson API reference
I am finding txing UDP data rather sporadic. I know its a fire and forget protocol but so far I haven't had any issues if I rx data first, then return to the same IP and port. However this is opening then sending, doesn't require a response or an ACK.
The code below sits as a standalone EXE and waits for LB to run it and pass a string$ using CommandLine$ (this bit works).
It then parses that CommandLine and decides whether it's Serial, UDP or TCP (TCP not added yet....).
For testing this currently tx's the data to another Serial/UDP/TCP test program Hercules, it's running on the same computer, I thought originally the issue could be that it's because it's on the same machine, however, I'm able to consistently send UDP data from Packet Sender using the same details. IE. I can constantly hit the send button on Packet Sender and it's rx'd on Hercules everytime, doesn't miss a beat.
This code however only seems to be rx'd 6-8 times out of 10, sometimes less.
'NOMAINWIN
GLOBAL LBNETpath$
'Uncomment this line out when testing
CommandLine$ = "IPU%192.168.0.39%8000%THIS IS A TEST MESSAGE"
'LBNETpath$ = "\Lib\CoreL\LBNet.dll"
LBNETpath$ = "LBNet.dll"
WindowTitle$ = "Integration - "
PRINT CommandLine$
Type$ = LEFT$(CommandLine$, 3)
'If window is lauched without any data display an error
IF Type$ = "" THEN
PRINT "Invalid argument!"
NOTICE WindowTitle$ + CHR$(13) + "Invalid Argument!"
END IF
'***********************************************************COM PORT************************************************
'RS232
'Structure = COM4%baud%parity%data%stop%messsagetext
IF Type$ = "COM" THEN
'Comm device
ONCOMERROR [COMERR]
token$ = "{"
idx = 0
WHILE token$<>""
idx = idx + 1
token$ = word$(CommandLine$, idx, "%")
IF token$ <> "" THEN
IF idx = 1 THEN Comport$ = token$
IF idx = 2 THEN Baud$ = token$
IF idx = 3 THEN Parity$ = token$
IF idx = 4 THEN Databits$ = token$
IF idx = 5 THEN Stopbits$ = token$
IF idx = 6 THEN Message$ = afterlast$(CommandLine$, "%")
print token$
END IF
WEND
CommInfo$ = Comport$ + ":" + Baud$ + "," + Parity$ + "," + Databits$ + "," + Stopbits$ '+ "," + "ds0,cs0,rs"
PRINT CommInfo$, Message$
'Open RS232 port and send info
OPEN CommInfo$ FOR RANDOM AS #com
#com, Message$;
CLOSE #com
PRINT "DONE!"
END
END IF
'********************************************************COM PORT FINISHED***************************************
'**********************************************************IP UDP START******************************************
'IP UDP
IF Type$ = "IPU" THEN
'IP UDP
'Collect information and parse into useable strings
token$ = "{"
idx = 0
WHILE token$ <> ""
idx = idx + 1
token$ = word$(CommandLine$, idx, "%")
IF token$ <> "" THEN
IF idx = 2 THEN IP$ = token$
IF idx = 3 THEN Port$ = token$
'IF idx = 3 THEN
print token$
END IF
WEND
Message$ = afterlast$(CommandLine$, "%")
'Open LBnet for UDP traffic
CALL OpenLBNetDLL
'open UDP socket
[doOpen]
hSock = UDPConnect(IP$, Port$, 0)
if IsSocketInvalid(hSock) then
print "Connect() failed. - ";GetError()
goto [doEnd]
end if
'Collect length of data
bufLen = LEN(Message$)
'Send the info to the port and IP specified
[doSend]
PRINT "doOpen "; IP$, Port$, Message$
sendNum = UDPSend(hSock, Message$, bufLen)
IF sendNum = -1 THEN
theError = GetError()
PRINT "UDPSendTo() failed. - ";theError
GOTO [doClose]
END IF
'Close socket
[doClose]
result = UDPClose(hSock)
PRINT "Close result: "; result
'Close LBNet
[doEnd]
CALL CloseLBNetDLL
'Close Program
END
END IF
'***********************************************************IP UDP END*******************************************
'***********************************************************IP TCP START*******************************************
'IP TCP
IF Type$ = "IPT" THEN
'IP TCP
END IF
'***********************************************************IP TCP END*******************************************
'All conditions tested for so END program
PRINT "Operation Completed!"
END
'********************
'Error opening the RS232 port? Then just close the program.
[COMERR]
PRINT "Communications Error"
END
'********************
'====================
'==Helper Functions==
'====================
Sub OpenLBNetDLL
open DefaultDir$ + LBNETpath$ for DLL as #LBNet
a = InitLBNet()
End Sub
Sub CloseLBNetDLL
a = EndLBNet()
close #LBNet
End Sub
Function InitLBNet()
CallDLL #LBNet, "InitLBNet",_
InitLBNet as long
End Function
Function EndLBNet()
CallDLL #LBNet, "EndLBNet",_
EndLBNet as long
End Function
Function IsSocketInvalid(sock)
CallDLL #LBNet, "IsSocketInvalid",_
sock as ulong,_
IsSocketInvalid as long
End Function
Function GetError()
CallDLL #LBNet, "GetError",_
GetError as long
if GetError < 0 then
GetError = (abs(GetError) XOR hexdec("FFFFFFFF")) + 1
end if
End Function
Function UDPConnect(host$, srv$, msTimeout)
CallDLL #LBNet, "UDPConnect",_
host$ as ptr,_
srv$ as ptr,_
msTimeout as long,_
UDPConnect as long
End Function
Function UDPConnectFrom(host$, srv$, msTimeout, localSrv$)
CallDLL #LBNet, "UDPConnectFrom",_
host$ as ptr,_
srv$ as ptr,_
msTimeout as long,_
localSrv$ as ptr,_
UDPConnectFrom as long
End Function
Function UDPSend(udpSock, buf$, bufLen)
CallDLL #LBNet, "UDPSend",_
udpSock as long,_
buf$ as ptr,_
bufLen as long,_
0 as long,_
UDPSend as long
End Function
Function UDPSendTo(udpSock, buf$, bufLen, udpInfo$)
CallDLL #LBNet, "UDPSend",_
udpSock as long,_
buf$ as ptr,_
bufLen as long,_
udpInfo$ as ptr,_
UDPSendTo as long
End Function
Function UDPClose(udpSock)
CallDLL #LBNet, "UDPClose",_
udpSock as long,_
UDPClose as long
End Function
Function UDPGetInfoSize()
CallDLL #LBNet, "UDPGetInfoSize",_
UDPGetInfoSize as long
End Function
Function UDPReceive(udpSock, byref buf$, bufLen)
CallDLL #LBNet, "UDPReceive",_
udpSock as long,_
buf$ as ptr,_
bufLen as long,_
0 as long,_
UDPReceive as long
End Function
Function UDPReceiveFrom(udpSock, byref buf$, bufLen, byref udpFrom$)
udpFrom$ = space$(UDPGetInfoSize())
CallDLL #LBNet, "UDPReceive",_
udpSock as long,_
buf$ as ptr,_
bufLen as long,_
udpFrom$ as ptr,_
UDPReceiveFrom as long
End Function
Function UDPIsReadAvailable(udpSock, msTimeout)
CallDLL #LBNet, "UDPIsReadAvailable",_
udpSock as long,_
msTimeout as long,_
UDPIsReadAvailable as long
End Function
Function UDPCreateListenSocket(pService$)
CallDLL #LBNet, "UDPCreateListenSocket",_
pService$ as ptr,_
UDPCreateListenSocket as long
End Function
'include lb45func.bas
Code requires LBNet DLL by Chris Inverson API reference