Log in

View Full Version : xport code, have example, confused though



kenpo
- 19th February 2009, 16:24
Hi everyone. so I'm excited to get the xport direct up and running and I see some of you here have used it.

I got the following code from : http://www.tigoe.net/pcomp/code/category/Picbasic%20Pro/35
but I'm unsure of how to use it. (can a 16f628 run the xport? it can right? it's just serial communication...)

my understanding thus far is

1. I need to use a terminal such as windows hyperterminal to set it up (can this instead be done via pbp?

2. hook up and run code.

I'm unsure of an exact hook up but I'm sure the manual will tell me, but I've tried to first compile the example code and get LOTS of errors...

not sure why, are there includes or other things I should be doing to get this code to compile? I'm using microcode studio and pbp 2.45

Thanks for any and all help!



The relevant Xport settings for this application are as follows:

*** Channel 1
Baudrate 9600, I/F Mode 4C, Flow 00
Port 10001
Remote IP Adr: --- none ---, Port 00000
Connect Mode : D4
Disconn Mode : 00
Flush Mode : 00

*** Expert
TCP Keepalive : 45s
ARP cache timeout: 600s
High CPU performance: disabled
Monitor Mode @ bootup : enabled
HTTP Port Number : 80
SMTP Port Number : 25

The code:

' Xport_to_SQL
' Microcontroller is connected to a Lantronix Xport
' serial-to-ethernet device. This program connects
' to a HTTP server through the Xport, makes a HTTP GET
' request for a PHP script, and sends the value
' of an analog sensor through as a part of the
' GET request.

' Xport communicates at 9600-8-n-1 non-inverted (true) serial.

' By Tom Igoe, 31 oct 2005
' updated 25 Oct. 2005

' Define ADCIN parameters
DEFINE ADC_BITS 10 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS

' pins to define serial RZX and TX
tx var portc.6
rx var portc.7
' pins with LEDs to indicate whether we're connecting
' or sending an HTTP request:
httpPin var portb.7
TCPPin var portb.6
output TCPPin
output httpPin

' constant to set the baud rate:
inv9600 con 16468
' for non-inverted connections (PIC to Xport direct):
true9600 con 84

' define variables:
adcVar var word
inByte var byte
connected var bit

TRISA = %11111111 ' Set PORTA to all input
ADCON1 = %10000010 ' Set PORTA analog and right justify result

connected = 0

' pause to let Xport boot up:
pause 2000
main:
' if you're connected to the server, then
' make a HTTP call. If not, then connect
' to the server:
if connected = 1 then
' read sensors, convert to bytes:
adcin 0, adcVar
' send HTTP GET request for php script:
gosub http_request
else
' attempt to connect to the server:
gosub xport_connect
endif
tcpPin = connected
' pause so we're not overwhelming the server:
pause 3000
goto main

xport_connect:
' turn off LED to indicate HTTP GET is in progress:
'low tcpPin
' wait for a "C" byte to come back:
while inByte <> 67
serout2 tx, true9600, ["C128.122.253.189/80", 10]
serin2 rx, true9600, [inByte]
wend
' turn on TCP pin to indicate that we connected:
'high TCPPin
connected = 1
return

http_request:
' light LED to indicate HTTP GET request in progress:
high httpPin
SEROUT2 TX, true9600, ["GET /~tqi6023/sql_datalog.php?action=insert&"]
serout2 tx, true9600, ["sensorValue=", DEC adcVar]
serout2 tx, true9600, [" HTTP/1.1", 10]
serout2 tx, true9600, ["HOST: itp.nyu.edu", 10, 10]
' wait for bytes from server:
' php script sends a 0 to end transmission:
while inByte <> 0
serin2 rx, true9600, [inByte]
wend
' now we're disconnected:
connected = 0
' turn off LED, since GET request is complete:
low httpPin
return

Archangel
- 19th February 2009, 17:44
Hi Kenpo,
I do not think 628 is going to do it, no ADC.

kenpo
- 19th February 2009, 18:21
sorry, please ignore the ADC code he has in there then.
I have no plans of using ADC.

for now I simply want to send an integer number to an online script.

just a test.
I didn't want to edit his code and risk removing soemthing I shouldn't so it's just a copy/paste

kenpo
- 19th February 2009, 19:23
ok, so here's my modified version:

untested, but compiles.

what I am unsure of:
1. how to set the xport with all the funky terminal settings and if it can be done via pbp

2. right now his code waits for a 0 return.
I'd like to receive more than that. ie, the number 55 and then do something like turn a pin HIGH. I'm unsure of how to do that though without breaking it. ie, it's waiting for a 0 return..

sorry for those of you that breath code, this must seem very silly



include "modedefs.bas"

@ DEVICE pic16F628A, INTRC_OSC_NOCLKOUT
' System Clock Options

@ DEVICE pic16F628A, MCLR_OFF
' Master Clear Options (Internal)
DEFINE OSC 4
CMCON = 7




' pins to define serial RZX and TX
tx var portA.6
rx var portA.7
' pins with LEDs to indicate whether we're connecting
' or sending an HTTP request:
httpPin var portb.7
TCPPin var portb.6
output TCPPin
output httpPin

' constant to set the baud rate:
inv9600 con 16468
' for non-inverted connections (PIC to Xport direct):
true9600 con 84

' define variables:
adcVar var word
inByte var byte
connected var bit

connected = 0

' pause to let Xport boot up:
pause 2000
main:
' if you're connected to the server, then
' make a HTTP call. If not, then connect
' to the server:
if connected = 1 then
' read sensors, convert to bytes:
adcVar = 55
' send HTTP GET request for php script:
gosub http_request
else
' attempt to connect to the server:
gosub xport_connect
endif
tcpPin = connected
' pause so we're not overwhelming the server:
pause 3000
goto main

xport_connect:
' turn off LED to indicate HTTP GET is in progress:
'low tcpPin
' wait for a "C" byte to come back:
while inByte <> 67
serout2 tx, true9600, ["C128.122.253.189/80", 10]
serin2 rx, true9600, [inByte]
wend
' turn on TCP pin to indicate that we connected:
'high TCPPin
connected = 1
return

http_request:
adcVar = 56
' light LED to indicate HTTP GET request in progress:
high httpPin
SEROUT2 TX, true9600, ["GET /xport_test.php?action=insert&"]
serout2 tx, true9600, ["sensorValue=", DEC adcVar]
serout2 tx, true9600, [" HTTP/1.1", 10]
serout2 tx, true9600, ["HOST: brandejs.ca", 10, 10]
' wait for bytes from server:
' php script sends a 0 to end transmission:
while inByte <> 0
serin2 rx, true9600, [inByte]
wend
' now we're disconnected:
connected = 0
' turn off LED, since GET request is complete:
low httpPin
return

kenpo
- 19th February 2009, 20:50
look at me, spamming my own post...

just wondering, could I do soemthing like change



while inByte <> 0
serin2 rx, true9600, [inByte]
wend


to



while inByte == z
serin2 rx, true9600, [inByte]
wend


that way I set inByte earlier on as 'z' and then it hangs down below until I get the number 55, or 3 or 0.

I'm imagining grabbing the weather value in degrees off the net for example

(and having my php script only output an integer value, not the extra html filler)

mister_e
- 19th February 2009, 21:34
Forget SERIN/SEROUT @ 9600 baud @4MHZ... even worst when dealing with the internal OSC. That PIC have a USART, use it + ceramic resonator + crystal for more reliable results.

You could try DEBUG/DEBUGIN... but there's no advantages over HSERIN/HSEROUT appart the possibility to use Inverted mode.

kenpo
- 19th February 2009, 21:49
that's just what I was thinking here as I was editing the code.
I looked up the manual for the xport so I can now see there's terminal options for setting it up, as well as their software and I can change 9600 to 2400

however I don't really see if it's possible to do all that from the pic. it seems it only needs to be done once? is that right?

I think the 16f628a internal OSC with 2400 should be reliable enough right?
it's a hobby project. I could take it even lower though there's not alot of data being sent.


the last function is now changed to


http_request:
adcVar = 56
' light LED to indicate HTTP GET request in progress:
high httpPin
inByte = "z"

SEROUT2 TX, T2400, ["GET /xport_test.php?action=insert&"]
serout2 tx, T2400, ["sensorValue=", DEC adcVar]
serout2 tx, T2400, [" HTTP/1.1", 10]
serout2 tx, T2400, ["HOST: brandejs.ca", 10, 10]
' wait for bytes from server:
' php script sends anything other than Z to end transmission:
while inByte == "z"
serin2 rx, T2400, [DEC inByte]
wend
' now we're disconnected:
connected = 0
' turn off LED, since GET request is complete:
low httpPin
return

mister_e
- 19th February 2009, 22:02
You're using True mode... why messing with bit banged SERIN/SERIN2/DEBUGIN while your PIC have built-in USART???? No advantage at all, and use more codespace as well...

HSERIN/HSEROUT and away you go.

I NEVER use internal OSC when I have to deal with async serial communication. OK OK I hear some "I did it, I still do it and it works all the time" .. well ... good for you ;)

kenpo
- 19th February 2009, 22:28
never used USART so I'm not really sure what you're talking about. I don't mind switching, but first I want to get this code working, as it should work as it's not my code. then I can start really changing it.

am I using "z" correctly?

it's been so long since I've dealt with picbasic. I've been coding in flash/php for the last 1.5 years.
trying to remember pbp code usage....

Charles Linquis
- 20th February 2009, 02:06
I'm with mister_e -

ALWAYS use the hardware USART if your PIC has one!
Use a crystal when you use RS-232.

The default baud rate of an XPORT is 9600 baud.
The XPORT is a 3.3V device. Are you running it off 3.3V?

You should use a level shifter (like a 74HC gate) between the XPORTs output and the PICs input to insure that the output level of the XPORT is high enough for the PIC. In a pinch, you can use a 1K pull up resistor (to 5V) on the line connecting the ouput of the XPORT to the PICs input. Since the XPORT is 5V compliant, you don't need to do anything with the line connecting the output of the PIC to the XPORT.

kenpo
- 20th February 2009, 14:31
haven't hooked it up yet, so currently running at 0v ;)

not sure how to run something at 3.3v is there something like a 7805?


and still no idea what USART is so I guess I'll do some reading.

xport defualt is 9600, but it can be brought down.

I'm excited to try this thing out, but I'm waiting for a breadboard compatible breakout board before I try it, rather than rush in and break something my first try.

mister_e
- 20th February 2009, 15:06
To run at 3.3 volt, yup you'll need a 3.3v voltage regulator. Now double check your specific datasheet to know if the PIC can run @3.3 volt without problem, case not, you may need to use a sort of level shifter between your PIC running @5volt and the Xport running @3.3v.

USART is just another built-in PIC feature, listed in the datasheet. You will need to use HSERIN/HSEROUT + some DEFINEs all listed in the PBP manual.

with 4MHz you can run the internal USART to have a really decent/accurate 9600 bauds.


DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
DEFINE HSER_SPBRG 25 ' 9600 Baud @ 4MHz, 0.16%
DEFINE HSER_CLROERR 1 ' Clear overflow automatically

Charles Linquis
- 20th February 2009, 15:11
You can get a 3.3V regulator. I use an LM3940
You obviously haven't read section 12 of the 16F628 datasheet - where it describes the USART!
There is no need to run the XPORT at a speed slower than 9600 baud.

kenpo
- 20th February 2009, 15:11
thanks everyone! I'm going to muck around with all of this now!

safa913
- 30th March 2009, 19:59
i have xport direct plus and i use 16f877a pic
can i use the same code ?