It might take a bit of explanation, but -
The code below is sent after the XPort is initialized for UDP.
Hoffset is number of hours before or after GMT.
SIGN is earlier ("1") or later ("2") than GMT.
The code works only with 18F chips because it uses PBPL.
Code:GETNETTIME: NETRETRYCOUNT = 0 GOSUB INTDISABLE GETNETTIME2: HSEROUT [13,10,"TIMESERVER ",#TSIP1,".",#TSIP2,".",#TSIP3,".",#TSIP4,13,10] PAUSE 100 THEADER[0] = $2 ' STX THEADER[1] = TSIP1 ' DESTINATION IP THEADER[2] = TSIP2 THEADER[3] = TSIP3 THEADER[4] = TSIP4 THEADER[5] = 0 ; DESTINATION PORT HIGHBYTE THEADER[6] = 123 ; DESTINATION PORT LOWBYTE LRC1 = 0 LRC2 = 0 For X = 1 to 6 LRC1 = LRC1 ^ THeader[x] next X THEADER[7] = LRC1 THEADER[8] = 0 THEADER[9] = 48 'Data Length lowbyte ;Send the header HSEROUT [13,10] For X = 0 to 9 HSEROUT2 [THeader[X]] Next X LRC2 = LRC2 ^ THeader[8] LRC2 = LRC2 ^ THeader[9] ;Send the data HSEROUT2 [$1B] LRC2 = LRC2 ^ $1B For X = 0 To 46 HSEROUT2 [0] LRC2 = LRC2 ^ 0 NEXT X ;Send the Checksum (LRC) HSEROUT2 [LRC2] getresponse: HSERIN2 1000,NoResponse,[Wait($2)] hserin2 200,NoResponse,[SKIP 41,TimeNow.Byte3,TimeNow.Byte2,TimeNow.Byte1,TimeNow.Byte0] ' TIME1900_2009 = 3439756800 READ 851,HOFFSET IF sign = "1" then if DST = 1 and HOffset = 0 THEN Sign = "2" GOTO OtherSide ENDIF TimeNow = Timenow - ((HOffset - DST) * 3600) ENDIF OtherSide: IF Sign = "2" THEN TimeNow = TimeNow + ((Hoffset + DST) * 3600) ENDIF SINCEJAN2009 = TIMENOW - 3439756800 ; 3439756800 is the #of sec from Jan1 1900 to Jan1 2009 RUNNINGSECONDS = SINCEjAN2009 YR = 2009 GETYEAR: LEAP = (YR//4) ; = 0 if leap year if LEAP = 0 then SecondsInYear = 31622400 ;366*60*60**24 ELSE SecondsInYear = 31536000 ;365*60*60*24 endif If RunningSeconds > SecondsInYear Then RunningSeconds = RunningSeconds - SecondsInyear YR = YR + 1 GOTO GetYear ENDIF Mo = 1 ; Start with Month = 1 GETMONTH: IF LEAP > 0 THEN lookup2 MO,[0,2678400,2419200,2678400,2592000,2678400,2592000,2678400,2678400,2592000,2678400,2592000,2678400],SecondsInMonth ELSE LOOKUP2 MO,[0,2678400,2505600,2678400,2592000,2678400,2592000,2678400,2678400,2592000,2678400,2592000,2678400],SecondsInMonth ENDIF If RunningSeconds >= SecondsInMonth THEN RunningSeconds = RunningSeconds - SecondsInMonth MO = MO + 1 GOTO GETMONTH ENDIF FINDDAYS: DA = RUNNINGSECONDS/86400 RUNNINGSECONDS = RUNNINGSECONDS//86400 HR = RUNNINGSECONDS/3600 RUNNINGSECONDS = RUNNINGSECONDS//3600 MN = RUNNINGSECONDS/60 RUNNINGSECONDS = RUNNINGSECONDS//60 SC = RUNNINGSECONDS Da = Da + 1 ; Because there is no day zero HSEROUT [13,10,13,10,DEC2 HR,":",DEC2 MN,":",DEC2 SC," ",DEC2 MO,"/",DEC2 DA,"/",#YR,13,10] YR8 = YR - 2000 ; clock only takes two digits! ConverttoBCD: DT = (DA/10) << 4 + (DA//10) YR8 = (YR8/10) << 4 + (YR8//10) MN = (MN/10) << 4 + (MN//10) SC = (SC/10) << 4 + (SC//10) HR = (HR/10) << 4 + (HR//10) MO = (MO/10) << 4 + (MO//10) gosub CLOCKWRITE NetFail = 0 HSEROUT ["CLK updated",13,10] pause 2000 goto DoneClock NoResponse: hserout ["RETRYING GetNetTime",13,10] pause 1000 NETRETRYCOUNT = NETRETRYCOUNT + 1 if NETRETRYCOUNT < 5 THEN goto getnettime2 ENDIF IF NETRETRYCOUNT < 10 THEN GOSUB GETALTERNATESERVER goto getnettime2 ENDIF READ 800,Dummy TSIP1 = Dummy THEADER[1] = TSIP1 'Set it back to primary READ 801,Dummy TSIP2 = Dummy THEADER[2] = TSIP2 READ 802,Dummy TSIP3 = Dummy THEADER[3] = TSIP3 READ 803,Dummy TSIP4 = Dummy THEADER[4] = TSIP4 NetFail = 1 HSEROUT [13,10,"FAILED NET TIME",13,10] DoneClock: GOSUB INTREENABLE RETURN
Charles Linquist
Thanks, Charles. I've been planning to write a similar procedure although I 'm using a different ethernet adapter (WIZ110SR), different RTC (ST M41T81) and different processor (ZBasic ZX40p). Still, adapting your code should save me a lot of time and error.
BTW, I've also used the ConnectOne nano LanReach mentioned by the OP. It has a very attractive price, easy communication protocol (patterned after modem AT commands) and a nice set of Internet protocols (e.g. SMTP/POP3 for email which are much easier to use than Lantronix). Unfortunately, Mouser will not ship it outside the USA due to Homeland Security issues.
Also, Tibbo (http://tibbo.com/products/) has several ethernet-serial adapters some of which can be programmed in their Basic dialect. They have source examples for email, SNTP, etc. on their website. Unfortunately, their XPort like versions are not sold in the USA (I suspect because of Lantronix patents).
Last edited by dhouston; - 30th October 2010 at 12:21.
I started using Lantronix years ago, because I bought their development kit first and also because of the small size of the XPort. I built up this complex scheme of writing the setup records over the serial port (since the customer dictated that setup NOT be allowed over Ethernet). The first board (18F8720 + XPort) started out as telnet only, but I just kept adding capability. First UDP then SNMP. The SNMP part was the hardest, because - while the most devices claim to support SNMP, they mean that the device itself terminates the SNMP request. That doesn't work for me, because I needed to support SNMP reading *MY* sensors. So the XPort has to become a UDP tunnel and I have to process the SNMP packets and OIDs in my code - not trivial! I also must send SNMP traps upon errors.
Later on, I needed SNMP and SSH/SSL simultaneously, so I used the Lantronix MatchPort AR. It handles the encryption. But setting up that device is not easy either - it uses XML records!
The biggest challenge was finding devices that allowed relatively easy configuration and setup over the SERIAL port, and yet blocked all attempts to configure through Ethernet (and would't even respond to pings). Military protocols demand that. All the serial<->Lan people want to impress me with the easy setup over Ethernet using a GUI. I tell them that is the exact opposite of what I need!
Like so many other cases - I'm so far down the road with Lantronix, it would be hard to switch now.
Charles Linquist
Understood. I wasn't trying to convert you, anyway. But, for people just starting with this, looking at WizNET (http://www.wiznet.co.kr/en/), ConnectOne (http://www.connectone.com/default.asp) and Tibbo (http://tibbo.com/) is a good idea if only to get an idea of the various choices available.
PS: I also used the XPort but it was my second route. My first was with BasCom AVR.
Thank you Charles!
Thanks and Regards;
Gadelhas
Charles,
Thanks for your code post. I was considering a Netburner 'PINK' module, but you've got me looking at the Lantronix Pro again. I have to admit that I'm really in the dark when doing an ethernet project!! If I'm primarily interested in sending emails and (in the future) acquiring the time, and serving up a simple web page to give input to a PIC project, what Lantronix evaluation board should I buy? I'm equally unexperienced in Linux and 'Evolution' OS's, but need to buy an eval board just to have a ready-built platform in which to experiment sending and receiving serially from my PIC to the Lantronix. With really NO experience, except for PIC's and PBP, which eval board from Lantronix would you buy?
Thanks for your opinion, and best regards,
Len G.![]()
I've decided to give Lantronix Matchport module a try. Onward.![]()
IIRC, Roving Network's RN-XV http://www.rovingnetworks.com/products/RN_XV WiFly module automatically contacts an NTP server and updates its onboard RTC every 4 hours. It's also quite inexpensive ($35) and connects via serial.
Last edited by dhouston; - 21st July 2012 at 19:49.
Bookmarks