View previous topic :: View next topic |
Author |
Message |
freefuel
Joined: 27 Aug 2007 Posts: 55 Location: New Jersey, USA
|
Posted: Fri Dec 21, 2007 4:08 pm Post subject: sending a string of HEX via serial port. |
|
|
I'm trying to send the HEX values of 02 00 00 00 07 out the serial port but I keep getting an error stating
Error : 242 Line : 18 Source variable does not match target variable [TEMP1 = 1 , 1 , 1 , 7]
why am I getting this error?
$crystal = 8000000
$baud = 38400
'tell the compiler which chip we use
$regfile = "m16def.dat"
'some variables we will use
Dim S As String * 5
S = String(2 , 1 , 1 , 1 , 7)
Dim Mybaud As Long
'when you pass the baud rate with a variable, make sure you dimesion it as a LONG
'Ucsr0b = 0 ' DISABLE HW UART
Mybaud = 38400
Do
Print "Hello World"
'first get some data
Serin S , 0 , D , 0 , Mybaud , 0 , 8 , 2
'now send it
Serout S , 0 , D , 1 , Mybaud , 0 , 8 , 2
' ^ 1 stop bit
' ^---- 8 data bits
' ^------ even parity (0=N, 1 = E, 2=O)
' ^-------------- baud rate
' ^-------------------- pin number
' ^----------------------- port so PORTA.0 and PORTA.1 are used
' ^--------------------------- for strings pass 0
' ^-------------------------------- variable
Wait 1
Loop
End _________________ The only difference between science fiction and reality is about 60 to 100 years. |
|
Back to top |
|
|
pratik
Joined: 13 Oct 2005 Posts: 55
|
Posted: Fri Dec 21, 2007 7:46 pm Post subject: |
|
|
hi,
read the help for "String" command |
|
Back to top |
|
|
kimmi
Joined: 24 Feb 2006 Posts: 1922 Location: Denmark
|
Posted: Fri Dec 21, 2007 9:23 pm Post subject: |
|
|
Hi freefuel ,
What uart do you use software uart ? |
|
Back to top |
|
|
Frankeman
Joined: 11 Aug 2004 Posts: 948 Location: the Netherlands
|
Posted: Fri Dec 21, 2007 9:37 pm Post subject: |
|
|
Hi,
Code: | S = String(2 , 1 , 1 , 1 , 7)
|
This is no valid sourcecode, fill an array with the values 2,1,1,1,7 and send that away.
Frank. |
|
Back to top |
|
|
freefuel
Joined: 27 Aug 2007 Posts: 55 Location: New Jersey, USA
|
Posted: Sat Dec 22, 2007 10:38 am Post subject: |
|
|
this is a sample directly from the help file.
when I compile that it passes.
Dim S As String * 15
S = String(5 , 65)
Print S 'AAAAA
End _________________ The only difference between science fiction and reality is about 60 to 100 years. |
|
Back to top |
|
|
Frankeman
Joined: 11 Aug 2004 Posts: 948 Location: the Netherlands
|
Posted: Sat Dec 22, 2007 10:52 am Post subject: |
|
|
Try to compile with the line:
S = String(2 , 1 , 1 , 1 , 7)
And then read the help about the string command. |
|
Back to top |
|
|
freefuel
Joined: 27 Aug 2007 Posts: 55 Location: New Jersey, USA
|
Posted: Sat Dec 22, 2007 11:20 am Post subject: |
|
|
I did and I get the error listed in my first post. _________________ The only difference between science fiction and reality is about 60 to 100 years. |
|
Back to top |
|
|
Evert :-)
Joined: 18 Feb 2005 Posts: 2156
|
|
Back to top |
|
|
freefuel
Joined: 27 Aug 2007 Posts: 55 Location: New Jersey, USA
|
Posted: Sat Dec 22, 2007 12:58 pm Post subject: |
|
|
Thank you Evert
after seeing your example I now see that string command can only send the same charter.
I tried looking for an array command but could not find one.
Is there a way to store are retrieve a series of different values? _________________ The only difference between science fiction and reality is about 60 to 100 years. |
|
Back to top |
|
|
pratik
Joined: 13 Oct 2005 Posts: 55
|
Posted: Sat Dec 22, 2007 5:00 pm Post subject: |
|
|
Hi,
try this
$crystal = 8000000
$baud = 38400
$regfile = "m16def.dat"
Const Cmaxchar = 160
Dim Tx(cmaxchar) As Byte
Dim I As Byte
Dim B As Byte
For I = 1 To 5
Read B
Tx(i) = B
Next I
Do
Print "Hello World"
For I = 1 To 5
Print Hex(tx(i))
Next I
Wait 1
Loop
Dta:
Data 02 00 00 00 07
by & MERRY CHRISTMAS TO YOU ALL |
|
Back to top |
|
|
Pixelfrank
Joined: 10 Dec 2005 Posts: 15 Location: Oranienburg
|
Posted: Mon Mar 31, 2008 4:28 pm Post subject: |
|
|
Hello,
i have an problem too, when i try to get five Nulls (&H00) into a String.
1. Disp1 = "{000}{000}{000}{000}{000}{001}FF00{002}
2. Disp1 = String(5 , &H00) + Chr(&H01) + "FF00" + Chr(&H02)
3. Disp1 = Chr(&H00) + Chr(&H00) + Chr(&H00) + Chr(&H00) + Chr(&H00) + Chr(&H01) + "FF00" + Chr(&H02)
in example 1 there is nothing in the string (???)
both example 2 and 3 contains the SOH (&H01) + "FF00" + STX(&H02), but the Nulls are ignored.
i want use the string to initilize an external RS232 device.
when i use:
4. Print Chr(&H00) ; Chr(&H00) ; Chr(&H00) ; Chr(&H00) ; Chr(&H00) ; Chr(&H01) ; "FF00" ; Chr(&H02) ;
this works fine, but it is not so "readable"
Frank |
|
Back to top |
|
|
DToolan
Joined: 14 Aug 2004 Posts: 1384 Location: Dallas / Fort Worth, Texas (USA)
|
Posted: Mon Mar 31, 2008 4:46 pm Post subject: |
|
|
Frank... a null character designates the end of a string so a normal print command will stop at the null character.
People are so hung up on using strings... *eyeroll* |
|
Back to top |
|
|
Pixelfrank
Joined: 10 Dec 2005 Posts: 15 Location: Oranienburg
|
Posted: Mon Mar 31, 2008 4:53 pm Post subject: |
|
|
Pixelfrank wrote: | Print Chr(&H00) ; Chr(&H00) ; Chr(&H00) ; Chr(&H00) ; Chr(&H00) ; Chr(&H01) ; "FF00" ; Chr(&H02) ; |
Hello DToolan,
thanks for answering.
But why works this above?
For the external RS232 device it is inportant to get the 5 Nulls before.
Frank |
|
Back to top |
|
|
Beke
Joined: 14 Feb 2008 Posts: 45
|
Posted: Mon Mar 31, 2008 6:53 pm Post subject: |
|
|
Hi,
AFAIK, Chr(0) does not exist, it is the "null" character, i.e. nothing. To see the print commands behaviour with it, try the following code:
Code: | Dim A As String * 5
A = Chr(65) + Chr(66) + Chr(67) + Chr(0) + Chr(68)
Print A
Print Len(a)
End |
Strings are terminated by the 0 "value".
According to the Help, the binary content of a variable is printed by the Printbin command.(its pair is the Inputbin).
Why Pixelfrank's command works, I cannot explain. An oscilloscope could clarify the situation. |
|
Back to top |
|
|
DToolan
Joined: 14 Aug 2004 Posts: 1384 Location: Dallas / Fort Worth, Texas (USA)
|
Posted: Mon Mar 31, 2008 6:58 pm Post subject: |
|
|
Quote: | But why works this above? |
From the Bascom Help...
CHR
Action
"Convert a numeric variable or a constant to a string with a length of 1 character."
"When you use PRINT Chr(numvar), the ASCII character itself will be printed."
This implies that when using CHR(&H00) that the print command will print it whether it is a null or not because it knows to stop after just one character.
When printing a normal string... ie, "HELLO WORLD" there is a null character at the end of "HELLO WORLD" and that is how it knows when to stop.
Since you are using CHR(&H00) it knows to print just one character and then stop.
You could just use Printbin and an array? Oh... thats not a string. People and strings... gotta' love them.
PixelFrank... you could also do the following
Code: | DIM wTemp As Word
wTemp = LOADLABEL(Init)
PRINTBIN wTemp
Do : Loop
End
Init:
DATA &H00 , &H00 , &H00 , &H00 , &H00 , &H01 , &H46 , &H46, &H30 , &H30 , &H00 , &H02 |
Last edited by DToolan on Mon Mar 31, 2008 9:08 pm; edited 2 times in total |
|
Back to top |
|
|
|