Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

sending a string of HEX via serial port.
Goto page 1, 2  Next
 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive
View previous topic :: View next topic  
Author Message
freefuel

Bascom Member



Joined: 27 Aug 2007
Posts: 55
Location: New Jersey, USA

usa.gif
PostPosted: Fri Dec 21, 2007 4:08 pm    Post subject: sending a string of HEX via serial port. Reply with quote

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
View user's profile
pratik

Bascom Member



Joined: 13 Oct 2005
Posts: 55

india.gif
PostPosted: Fri Dec 21, 2007 7:46 pm    Post subject: Reply with quote

hi,
read the help for "String" command
Back to top
View user's profile
kimmi

Moderator



Joined: 24 Feb 2006
Posts: 1922
Location: Denmark

denmark.gif
PostPosted: Fri Dec 21, 2007 9:23 pm    Post subject: Reply with quote

Hi freefuel ,
What uart do you use software uart ?
Back to top
View user's profile Visit poster's website MSN Messenger
Frankeman

Bascom Member



Joined: 11 Aug 2004
Posts: 948
Location: the Netherlands

netherlands.gif
PostPosted: Fri Dec 21, 2007 9:37 pm    Post subject: Reply with quote

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
View user's profile
freefuel

Bascom Member



Joined: 27 Aug 2007
Posts: 55
Location: New Jersey, USA

usa.gif
PostPosted: Sat Dec 22, 2007 10:38 am    Post subject: Reply with quote

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
View user's profile
Frankeman

Bascom Member



Joined: 11 Aug 2004
Posts: 948
Location: the Netherlands

netherlands.gif
PostPosted: Sat Dec 22, 2007 10:52 am    Post subject: Reply with quote

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
View user's profile
freefuel

Bascom Member



Joined: 27 Aug 2007
Posts: 55
Location: New Jersey, USA

usa.gif
PostPosted: Sat Dec 22, 2007 11:20 am    Post subject: Reply with quote

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
View user's profile
Evert :-)

Bascom Expert



Joined: 18 Feb 2005
Posts: 2156

netherlands.gif
PostPosted: Sat Dec 22, 2007 12:41 pm    Post subject: Reply with quote

Syntax
var = STRING(m ,n)

Var
The string that is assigned.

N
The ASCII-code that is assigned to the string.

M
The number of characters to assign.

Code:

Dim S As String * 15
S = String(5 , 65)
Print S                                                     'AAAAA
 


This sample will print 5x the ascii karakter 65 = A, as you can see here in the sample it wil print AAAAA.

What you want to do with STRING is not possible, you need something like this.
S = Chr(&H02) + Chr(&H00) + Chr(&H00) + Chr(&H00) + Chr(&H07)

OR

S = "{002}{000}{000}{000}{007}"

And what the rest it trying to say to you that you must read the helpfile, there's very clear in the help file that STRING only allows 2 parameters.


MERRY CHRISTMAS TO YOU ALL Laughing

_________________
www.evertdekker.com Bascom code vault
Back to top
View user's profile Visit poster's website
freefuel

Bascom Member



Joined: 27 Aug 2007
Posts: 55
Location: New Jersey, USA

usa.gif
PostPosted: Sat Dec 22, 2007 12:58 pm    Post subject: Reply with quote

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
View user's profile
pratik

Bascom Member



Joined: 13 Oct 2005
Posts: 55

india.gif
PostPosted: Sat Dec 22, 2007 5:00 pm    Post subject: Reply with quote

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
View user's profile
Pixelfrank

Bascom Member



Joined: 10 Dec 2005
Posts: 15
Location: Oranienburg

germany.gif
PostPosted: Mon Mar 31, 2008 4:28 pm    Post subject: Reply with quote

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
View user's profile
DToolan

Bascom Member



Joined: 14 Aug 2004
Posts: 1384
Location: Dallas / Fort Worth, Texas (USA)

blank.gif
PostPosted: Mon Mar 31, 2008 4:46 pm    Post subject: Reply with quote

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
View user's profile Yahoo Messenger
Pixelfrank

Bascom Member



Joined: 10 Dec 2005
Posts: 15
Location: Oranienburg

germany.gif
PostPosted: Mon Mar 31, 2008 4:53 pm    Post subject: Reply with quote

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
View user's profile
Beke

Bascom Member



Joined: 14 Feb 2008
Posts: 45

hungary.gif
PostPosted: Mon Mar 31, 2008 6:53 pm    Post subject: Reply with quote

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
View user's profile
DToolan

Bascom Member



Joined: 14 Aug 2004
Posts: 1384
Location: Dallas / Fort Worth, Texas (USA)

blank.gif
PostPosted: Mon Mar 31, 2008 6:58 pm    Post subject: Reply with quote

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
View user's profile Yahoo Messenger
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive All times are GMT + 1 Hour
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum