Shop OBEX P1 Docs P2 Docs Learn Events
Please help with serial programming — Parallax Forums

Please help with serial programming

John998John998 Posts: 7
edited 2006-10-23 07:31 in Learn with BlocklyProp
Hi,
i am trying to program to stamp to do the following:
-get input from serial port
-get·data from a/d converter
-send output to serial port

What i have achieved:
I can get input and send output· to serial port independantly but when i do both at the same program it give me problems.· The problem is that the output has datas that belongs to the input data i recieved earlier.· For example: input data from serial port is 1, and i want to send a 000011011101,·the data i·recieve at the serial port is 0001000011011101.· I used·pin 16·to communicate with the serial port both·send and recieve.· i tried to search for another·pin i can use·so i have 1 pin for·send and 1 pin for recieve, but i don't know how.· Can·someone please help me?· Following is the code i used.


SCLK···· CON· 0· 'serial clock pin
CS··· CON· 1· 'chip select pin
serData_in· CON· 2· 'serial data into the Stamp
'
Varibles
adc··· VAR Word· '12 bit result from the a/d converter
datain VAR Bit· 'start test signal from serial port
'
Program
main:······························· ' main program loop
··· 'DEBUG "enter datain: ",CR
··· adc = 0
··· SERIN 16,84, [noparse][[/noparse]datain]
··· IF datain = 1 THEN start_input
··· IF datain = 0 THEN sendvb
····PAUSE 1000
GOTO main

start_input:···· ' get input from adc and send·data to computer
··· GOSUB getinput
··· GOSUB sendvb
RETURN

sendvb:··· ' send the data to the computer
··· SEROUT 16,84,[noparse][[/noparse]bin16 adc]
RETURN

getinput:
··· LOW CS··············· 'start conversion
··· SHIFTIN Serdata_in,SCLK,MSBPOST,[noparse][[/noparse]adc\12]····· 'read in converted signal
··· HIGH CS·············· 'stop conversion
RETURN

Comments

  • Kevin WoodKevin Wood Posts: 1,266
    edited 2006-07-26 19:18
    Your code is a little confusing. What is the datain test signal used for?
  • John998John998 Posts: 7
    edited 2006-07-26 19:28
    datain is the signal to tell tell the stamp to start taking datas from the a/d converter.
    if datain is 1, take reading from the a/d converter then send the data to serial out.
    if datain is 0, don't take reading from the a/d converter but send 0 to serial out.
  • Kevin WoodKevin Wood Posts: 1,266
    edited 2006-07-27 01:29
    How are you sending the 1 or 0 to datain? I see the SERIN, but what's supplying the 1 or 0? Why aren't you using DEBUGIN?
  • John998John998 Posts: 7
    edited 2006-07-27 14:24
    I am sending datain from Visual Basic through serial port. i am not using DEBUGIN because i don't want to open the stamp editor everytime i run my application. Thanks
  • Kevin WoodKevin Wood Posts: 1,266
    edited 2006-07-28 09:08
    Try sending the datain from the debug terminal.

    Just launch the terminal, choose your port, and type 1 or 0 in the input window.

    Does your code work this way?

    If it does, then the problem is likely in your VB serial port settings.
  • John998John998 Posts: 7
    edited 2006-07-28 14:47
    No, using the debug terminal produce the same result. a 1 in front of the output data. Is it possible that the serial port buffer still have the input data even after i received it. is there a way to flush that buffer before i do a serout?
  • allanlane5allanlane5 Posts: 3,815
    edited 2006-07-28 15:39
    Yes, when you send to the BS2 serial port from your PC, whatever you send WILL be echo'ed back to you by the BS2 hardware. So, when you send a '1' from the PC to the BS2, you must make sure on the PC to remove the echo'ed "1".

    Since you're sending a "1" or a "0", removing the first byte of what comes back to you from the BS2 seems simple.
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-07-28 15:58
    Gents -

    Although it may be obvious to some, let me just clarify Allen's response, just a tiny bit. His response was (in part):

    "Yes, when you send to the BS2 serial port from your PC ..."

    I would only add the following:

    Yes, when you send to the native BS2 serial port (pin port 16) from your PC ...

    Sorry for the interruption. And now, back to your regularly scheduled program smile.gif

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->
  • taboadataboada Posts: 4
    edited 2006-08-10 17:31
    In a previous post, someone has recommended to try the debug terminal to see if it would work.· If it had, then what else could have been the problem?· I have a situation where the bs2p40 chip with its loaded program·will work through the debug terminal, but not so when I run it through another serial communication program.· Thanks for your help!
  • allanlane5allanlane5 Posts: 3,815
    edited 2006-08-10 18:10
    1. The 'other serial communication program' wasn't opening the right port, perhaps, at the right baud rate.

    2. The 'other serial communication program' was holding DTR high, which will hold the BS2 in reset. The 'debug' terminal knows not to do this.
  • taboadataboada Posts: 4
    edited 2006-08-10 22:15
    Thanks for your help.· The problem is that the other serial communication program is holding DTR high.· One of the programs I am using is hyperterminal.· How do you set the DTR to low in the program?· Any help would be greatly appreciated.· Thanks!
  • mike_zmike_z Posts: 4
    edited 2006-08-28 09:24
    sendvb: ' send the data to the computer
    SEROUT 16,84,[noparse][[/noparse]bin16 adc]

    port 16? - think you need redifine this
    i.e. 0 - 15
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-08-28 09:38
    Mike -

    Pin port 16 is a legitimate specification although it has some restrictions. Pin port 16 is Sin/Sout or the DEBUG port. It will only operate at 9600, 8, N, 1, as far as I'm aware.

    regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->
  • allanlane5allanlane5 Posts: 3,815
    edited 2006-08-28 15:08
    Setting DTR low:

    1. Using the MSCOMM control, you do: Comm1.DTREnable = FALSE.

    2. Using the 'raw' Windows CommPort handle. This is trickier.
    I've attached some sample 'REALBasic' code -- but really all you
    need from this is the Windows calls -- CreateFile, and
    EscapeCommFunction
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-08-28 17:44
    Bruce Bates said...(trimmed)
    Mike -
    Pin port 16 is a legitimate specification although it has some restrictions. Pin port 16 is Sin/Sout or the DEBUG port. It will only operate at 9600, 8, N, 1, as far as I'm aware.
    Bruce,

    ·· Although DEBUG works at a fixed 9600 bps (on all BASIC Stamps except the BS2px, which runs at 19200 bps) the SIN/SOUT (P16) can be set to any baud rate that P0 through P15 can be set to.· The only difference is that regardless of the baud rate, inverted mode is assumed.· Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2006-08-28 18:01
    I toyed around with this for a while and found I could make it work with a couple of minor modifications using the Stamp's DB 9 connector and a straight through two wire connection.

    I made the datain variable a byte and and formatted the SERIN to read a HEX number

    datain VAR Byte· 'start test signal from serial port

    '
    Program
    main:······························· ' main program loop
    ··· 'DEBUG "enter datain: ",CR
    ··· adc = 0
    ··· SERIN 16,84, [noparse][[/noparse]HEX datain]
    ··· IF datain = 1 THEN start_input
    ··· IF datain = 0 THEN sendvb
    ··· PAUSE 1000
    GOTO main

    This is the VB code I used, it was assigned to a Button_Click event for a test (you may be using COMM1 instead of SerialPort1)

    'Make sure you have an open port
    ·· If Not Me.SerialPort1.IsOpen Then
    ·· MsgBox("Com port is closed", MsgBoxStyle.OkOnly, "Port")
    ·· Else
    'The "01" is a string that represents user input as a HEX number and Chr(13) is the end of transmission
    ·· Me.SerialPort1.Write("01" & Chr(13))
    'Initialize two strings to manipulate your data
    ·· Dim mydata As String = ""
    ·· Dim filtered_data As String = ""
    'Read the data plus the echo
    ·· mydata = SerialPort1.ReadLine
    'remove the echo
    ·· filtered_data = Microsoft.VisualBasic.Mid(mydata, 4)
    End If

    Jeff T.

    P.S
    You said you wanted to send·12 bits to the PC yet your SEROUT is formatted to send 16 bits.


    ·
  • Terry^_^Terry^_^ Posts: 1
    edited 2006-10-23 07:31
    Hi,
    Did you try someother website?
Sign In or Register to comment.