Please help with serial programming
John998
Posts: 7
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
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
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.
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.
Since you're sending a "1" or a "0", removing the first byte of what comes back to you from the BS2 seems simple.
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
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
<!--StartFragment -->
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.
SEROUT 16,84,[noparse][[/noparse]bin16 adc]
port 16? - think you need redifine this
i.e. 0 - 15
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 -->
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
·· 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
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.
·
Did you try someother website?