PDA

View Full Version : Hserin / out Woes..



andybarrett1
- 21st April 2015, 21:16
Hi All..

Am using this code as a simple password check on a serial link :-



hserout [":- "]
hserin 65535,bed, [pass (0)]: hserout [pass (0)]
hserin 65535,bed, [pass (1)]: hserout [pass (1)]
hserin 65535,bed, [pass (2)]: hserout [pass (2)]
hserin 65535,bed, [pass (3)]: hserout [pass (3)]
hserin 65535,bed, [pass (4)]: hserout [pass (4)]
hserin 65535,bed, [pass (5)]: hserout [pass (5)]
hserin 65535,bed, [pass (6)]: hserout [pass (6)]
hserin 65535,bed, [pass (7)]: hserout [pass (7)]
hserin 65535,bed, [pass (8)]: hserout [pass (8),10,13]

IF ("2" = pass(0))and ("9" = pass(1))and ("9" = pass(2))and ("7" = pass(3))and ("9" = pass(4))and ("2" = pass(5))and ("4" = pass(6))and ("5" = pass(7))and ("8" = pass(8)) THEN correct



There are nine decimal digits which have to be correct.... but some times it fails on the last digit, but not always ?? If I cut the pass word to 4 digits it fails also on the last digit ????

My question is... is there a more elegant way of doing the above? I still want to see each char as typed...wich rules out a string of words.

But more I need it to be reliable..I am using a 4 Meg Xtal my defines are :-




DEFINE OSC 4 'Set oscillator in MHz
'OSCCON = $60 '4 Megs

DEFINE HSER_RCSTA 90H 'Set receive status and control
DEFINE HSER_TXSTA 24H 'Set transmit status and control
DEFINE HSER_BAUD 9615 'Baud Rate


ADCON1 = 7 ' Turns Analogs off
CMCON0 = 7 'Disable both comparators
ANSEL = 0 ' Set all analog pins to digital



thank you for looking....

HenrikOlsson
- 22nd April 2015, 06:39
Hi,
I'm not sure if the DEFINE HSER_BAUD accepts non standard baudrates, I suspect it does since it kind of works. What's with the 9615 anyway?
I don't know what would make it fail on the last character - sometimes. Are you typing in, and sending, the characters one by one, ie there's 100ms or whatever between each character?

Anyway, here's an alternative aproach I just wrote. I have not tested it, not even compiled it so take it for what it is.



Key VAR BYTE
CorrectKey VAR BYTE[9]
KeyIndex VAR BYTE
PassFail VAR BIT

Pass CON 1
Fail CON 0

CorrectKey[0] = "2"
CorrectKey[1] = "9"
CorrectKey[2] = "9"
CorrectKey[3] = "7"
CorrectKey[4] = "9"
CorrectKey[5] = "2"
CorrectKey[6] = "4"
CorrectKey[7] = "5"
CorrectKey[8] = "8"


Main:
hserout [":- "]

PassFail = Pass
For KeyIndex = 0 to 8
HSERIN 65535, bed, Key
HSEROUT Key
IF Key <> CorrectKey[KeyIndex] THEN
PassFail = Fail
ENDIF
NEXT

IF PassFail = Pass THEN
HSEROUT[10, 13, "Correct key entered",10, 13]
ELSE
HSEROUT[10, 13, "Wrong key entered", 10, 13]
ENDIF


/Henrik.

Tabsoft
- 22nd April 2015, 15:54
Here is another approach that allows you to wait for all of the characters to be entered before testing the password.
It also makes use of arraywrite to enter the Master Password.



DEFINE OSC 4 'Set oscillator in MHz
'OSCCON = $60 '4 Megs

DEFINE HSER_RCSTA 90H 'Set receive status and control
DEFINE HSER_TXSTA 24H 'Set transmit status and control
DEFINE HSER_BAUD 9615 'Baud Rate

MasterPass var byte[9]
pass var byte[9]
PassTest var bit

i var byte

' ADCON1 = 7 ' Turns Analogs off
' CMCON0 = 7 'Disable both comparators
' ANSEL = 0 ' Set all analog pins to digital

arraywrite MasterPass,["299792458"]

main:
'Test for 299792458
PassTest = 1
for i = 0 to 8
pass[i] = 0
next i

hserout [":- "]
for i = 0 to 8
hserin 65535, bed, [pass(i)]
hserout [pass(i)]
next i

'Check Password with Master
for i = 0 to 8
if pass(i) <> MasterPass(i) then PassTest = 0
next i

if PassTest = 1 then correct

hserout [13,10,"NO WAY!!",13,10]


goto main



correct:

hserout [13,10,"Correct!",13,10]

goto main


bed:
hserout ["Timeout",13,10]


bye:
goto main

andybarrett1
- 24th April 2015, 12:14
Thanks for the replies.....

Will try then out later.... have been working away!

BR
Andy

andybarrett1
- 25th April 2015, 23:02
hi All...

Have played with my code a little more....

Have come to conclusion that the serial port is seeing 0xFf before I type any password... I found by putting a 232 sniffer on the ports !!!


Ie the password is 9 chars but the first on occasionally is 0xFF ????

any Ideas ? anyone ?

Tomorrow O will go back to Serin / Serout see how that gets on !

Thank you for help !

Dave
- 26th April 2015, 13:11
For reliable communications, I always clear the receive buffer before transmitting a request to any perif. That way if you have some noise on the comm lines, it won't be interpreted as bad data.

andybarrett1
- 26th April 2015, 19:50
For reliable communications, I always clear the receive buffer before transmitting a request to any perif. That way if you have some noise on the comm lines, it won't be interpreted as bad data.

Hi Dave

Sounds good.... But how do I do that ?

I see no pointers in the manual V2.60

THank you for help

andybarrett1
- 26th April 2015, 20:04
MY Defines are now :-

DEFINE HSER_RCSTA 90H 'Set receive status and control
DEFINE HSER_TXSTA 24H 'Set transmit status and control
DEFINE HSER_BAUD 9615 'Baud Rate
DEFINE HSER_CLROERR 1 ' Clear Buffer

Tabsoft
- 26th April 2015, 20:19
Andy,

You can always just use a "HSERIN" bogus statement to throw away anything in the buffer before you execute your regular "HSERIN" statements.
Since the EUSART has a 2 byte buffer you may need to do two of the bogus "HSERIN" statements with timeouts just to make sure you don't get stuck waiting.

andybarrett1
- 26th April 2015, 21:16
Hi

Thanks for reply ...

Will try tomorrow and post what happens ...

Thank you for advice

Andy

HenrikOlsson
- 27th April 2015, 06:12
Hi,

You can always just use a "HSERIN" bogus statement to throw away anything in the buffer before you execute your regular "HSERIN" statements.
Just keep in mind that if there is no garbage data in the RX buffer then the bogus statement will throw away the real data when it comes so if you go down that route make sure you specify a very low timeout value for the initial bogus statement.

Here's another way to flush the buffer:


RCIF VAR PIR1.3 ' Alias to the RX Interrupt flag for USART 1 on an 18F25k22
Dummy VAR BYTE
WHILE RCIF
Dummy = RCREG1
WEND

/Henrik.

andybarrett1
- 27th April 2015, 22:57
Andy,

You can always just use a "HSERIN" bogus statement to throw away anything in the buffer before you execute your regular "HSERIN" statements.
Since the EUSART has a 2 byte buffer you may need to do two of the bogus "HSERIN" statements with timeouts just to make sure you don't get stuck waiting.

Hi

I just put your suggestion in place and it seems to be working for me .... I just used 50 as a time out figure... thank you!

I will try the other suggestions also thank you as always Henrik

For now I need sleep !!!!!

Br
Andy