View previous topic :: View next topic |
Author |
Message |
kempol
Joined: 27 Sep 2010 Posts: 5
|
Posted: Sat Jul 15, 2017 8:39 am Post subject: bytematch change in running programm |
|
|
Hi
Is there a chance to change bytematch in running code?
By change I mean something like this:
Code: |
Select Case A
Case 0
Config Serialin1 = Buffered , Size = 202 , Bytematch = All
Case 1
Config Serialin1 = Buffered , Size = 202 , Bytematch = 10 '10 = Lf, 13 = Cr
End Select
|
Of course above example will not work, but I'm wondering if it is possible to do something similar.
I have a project with different type of data (binary and ascii) and I would like to receive them on the same port which I can configure by simple DIP switch.
Thanks.[/quote]
(BASCOM-AVR version : 2.0.7.8 , Latest : 2.0.7.8 ) |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Sat Jul 15, 2017 8:38 pm Post subject: |
|
|
it will not work because of this code in mcs.lib
* Cpi r25,_CHECHCHAR0_char ;check with char to compare
but you could change it and put [_GOTCHAR0] in a custom lib with code like:
*lds r16,{YOURBYTE}
* CP r25, R16 ;check with char to compare
then you need to dim YOURBYTE as a byte and assign it a value of your choice.
We could integrate it as a new option in the compiler but you need to discuss that at support. _________________ Mark |
|
Back to top |
|
|
AdrianJ
Joined: 16 Jan 2006 Posts: 2483 Location: Queensland
|
Posted: Mon Jul 17, 2017 8:20 am Post subject: |
|
|
Of course you can do this in the library function, as Mark suggests.
But you can just as easily do it in running code, rather than in the config. And this gives you a lot more flexibility to handle all sorts of characters in the incoming stream.
Code: |
while ischarwaiting() = 1 'character in buffer
bt = inkey() 'get the character as a byte
select case A 'your selection from external switch
case 0
'... do whatever you want with bt as binary
case 1
'... do the alternate as ascii
select case bt 'get the value
case 10 'pick out the LF
' ... do whatever you want with the LF
case ... anything else you want to handle specially, just create another case for it
case else 'all other characters
'... put them in string, or whatever
end select
end select
end while
|
Bascom is plenty fast enough to handle all sorts of constructions like this, and process characters one by one as they come in, especially when using buffered serial input.
If you do a search on the forum for "NMEA" you will find several examples using this to decode NMEA strings passed from GPS units. But the technique can be applied to almost anything. _________________ Adrian Jansen
Computer language is a framework for creativity |
|
Back to top |
|
|
laborratte
Joined: 27 Jul 2005 Posts: 299 Location: Berlin
|
Posted: Mon Jul 17, 2017 1:28 pm Post subject: |
|
|
Third option: use BYTEMATCH = ALL and check the received byte in your sub:
Code: |
Config Serialin1 = Buffered , Size = 202 , Bytematch = All
dim MyMatch as byte
(...)
MyMatch = 10 'or what you want
(...)
Serial1ByteReceived:
'r25 holds the received byte (Bascom Version 2.0.8.0, check mcs.lib if any doubt)
'r16 can be used as the content is no longer necessary
$asm
lds r16,{MyMatch} 'load variable MyMatch to Register R16
cp r25,r16 'Compare received Byte with MyMatch
breq Serial1ByteReceived_Matched 'If equal, jump to "Serial1ByteReceived_Matched"
ret 'Fast back to buffered serial in
$end asm
Serial1ByteReceived_Matched:
pushall
(...) 'your code
popall
return
|
So you have fast buffered serial in and you do not need to write your own lib. |
|
Back to top |
|
|
|