problem to send data from computer to pic via serial commun

Discuss PIC and electronic related things

Moderators: David Barker, Jerry Messina

Post Reply
kerelous58
Posts: 8
Joined: Fri Aug 17, 2012 2:30 pm
Location: egypt

problem to send data from computer to pic via serial commun

Post by kerelous58 » Tue May 13, 2014 12:56 pm

hello i' m new in swordfish and try to develope program send and transmit from pc

i open swordfish example and test the cobe it's work but when the pic send data to the pc as the flowing code

----------------------------------------------------------------------------------------------------------------------
Device = 18F452
Clock = 20

Include "usart.bas"
Include "convert.bas"

SetBaudrate(br9600)

loop:

USART.Write("Hello ",13, 10)
USART.Write(13, 10)

GoTo loop

----------------------------------------------------------------------------------------------------------------------

but when try to make a program which send data from pc to pic it's doesn't work and all the code belw USART.READ like the LCD it's doesn't work

i think the code is freezed

----------------------------------------------------------------------------------------------------------------------

Device = 18F452
Clock = 20

Include "usart.bas"
Include "convert.bas"
Include "LCD.bas"


Dim Value As word

SetBaudrate(br9600)

loop:

USART.Read(Value)
DelayMS(500)
LCD.WriteAt(1,1,"serial value = ",dectostr(Value)," ")

GoTo loop

----------------------------------------------------------------------------------------------------------------------

thanks

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: problem to send data from computer to pic via serial com

Post by Jerry Messina » Wed May 14, 2014 11:18 am

That's likely a difference between what the PC is sending vs what you expect to receive.

Since you have Value declared as a word, the call to USART.Read() expects to see 2 bytes... no more, no less.
Also, the USART.bas module does not buffer received data or always handle errors, and that can get you into trouble.

For a simple test, try the Echo.bas program in the Samples directory. That will prove that your hardware is working properly.

SHughes_Fusion
Posts: 219
Joined: Wed Sep 11, 2013 1:27 pm
Location: Chesterfield

Re: problem to send data from computer to pic via serial com

Post by SHughes_Fusion » Wed May 14, 2014 12:53 pm

Like Jerry I'd recommend you receive data a byte at a time. A PC will output in ASCII - assuming you are using a terminal. This means if you type '0' in to your PC, the PIC will receive the (byte) value 48.

Most comms with a PC will be in ASCII format - you can't easily use non-ASCII as the Windows drivers interpret control codes rather than just passing them through. Therefore, in this sort of application it only makes sense to read bytes (or chars depending on what you are doing with the data).

I'd also drop the delay - I have had issues with DelayMS freezing my code, but USART.Read waits until it receives a byte anyway.

Bear in mind also that the PIC only has a 2.7 byte FIFO on the serial input - in other words, it can't fully read three bytes, you need to read the first byte before the third completes so you need to process serial receives promptly.

Personally, I'd love to see a full interrupt-based serial stack built in to Swordfish rather than using blocking calls but this does make it tricky to implement other interrupts.

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: problem to send data from computer to pic via serial com

Post by Jerry Messina » Wed May 14, 2014 1:04 pm

I'd love to see a full interrupt-based serial stack built in to Swordfish
The ISRRX.bas module does a good job of providing interrupt-based serial input.
For output you're on your own, but most times I find interrupt-driven output to be more hassle than it's worth.

There's an example in samples\interrupts\interrupts.bas

SHughes_Fusion
Posts: 219
Joined: Wed Sep 11, 2013 1:27 pm
Location: Chesterfield

Re: problem to send data from computer to pic via serial com

Post by SHughes_Fusion » Wed May 14, 2014 1:19 pm

I was more meaning native support rather than via a library, it's easy enough to set up interrupt-based when you are experienced but for beginners such as the OP it would be very handy.

Interrupt-based transmission is easy enough, 6 lines of code in the ISR and about a dozen more to set it up and in the PutChar command. It has the benefit that you aren't waiting for the transmit to complete, your code can carry on and do other things.

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: problem to send data from computer to pic via serial com

Post by Jerry Messina » Wed May 14, 2014 1:50 pm

I was more meaning native support rather than via a library
Not to get too OT, but I prefer the current approach where almost everything is in external modules where I can modify the source.

To me, that's one of the biggest selling points of SF.
It has the benefit that you aren't waiting for the transmit to complete, your code can carry on and do other things
True, but you have to provide enough buffering to handle the entire output. Otherwise you end up waiting anyway.

SHughes_Fusion
Posts: 219
Joined: Wed Sep 11, 2013 1:27 pm
Location: Chesterfield

Re: problem to send data from computer to pic via serial com

Post by SHughes_Fusion » Wed May 14, 2014 1:59 pm

I think a mixed approach would be best myself. Have some functions which have a basic internal implementation which you can disable and write your own if you prefer. For what I'm doing at the moment I do some tests on the received data before buffering it so I do need to write my own handler, but there are other projects where it would be nice to just test a flag to see if data is ready and read it in if so.

This goes back to something I asked a while back about a way to merge interrupt handlers automatically. Again not a trivial thing to do but it would help in implementing things like this if the Swordfish interrupt routines could extend to being able to hook code in to a particular interrupt flag.

As for transmit buffering, a lot depends on the context. Unless you regularly send messages considerably bigger than the buffer you will still get some saving as you'd only end up waiting for n-buffer size character times, rather than n character times, which can be quite significant at slower baud rates.

kerelous58
Posts: 8
Joined: Fri Aug 17, 2012 2:30 pm
Location: egypt

Re: problem to send data from computer to pic via serial com

Post by kerelous58 » Wed May 14, 2014 7:17 pm

Jerry Messina wrote:That's likely a difference between what the PC is sending vs what you expect to receive.

Since you have Value declared as a word, the call to USART.Read() expects to see 2 bytes... no more, no less.
Also, the USART.bas module does not buffer received data or always handle errors, and that can get you into trouble.

For a simple test, try the Echo.bas program in the Samples directory. That will prove that your hardware is working properly.
thanks for replay jerry
i try echo.bas and it's work and i try to recieve a hex adress consist of 27 number
----------------------------------------------------------------------------------------------------------------------
Device = 18F452
Clock = 20

Include "usart.bas"
Include "convert.bas"
Include "LCD.bas"

Public Dim
VarArray(27) As Byte

SetBaudrate(br19200)

serial:
USART.WaitForCount( VarArray(),27)
LCD.WriteAt(1,1,(y),Hextostr(VarArray(26)),Hextostr(VarArray(27)))

GoTo serial
----------------------------------------------------------------------------------------------------------------------
to full the adress in array and chose after that what i want from this adress

but i found you are right about USART.BAS the micro response is too slow it once recieve some of the adress and once no

so my experience in the serial communcation is weak so could you help me to improve this code or some idea or some info
to solve this problem

thanks

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: problem to send data from computer to pic via serial com

Post by Jerry Messina » Wed May 14, 2014 8:37 pm

If you do not use interrupts and buffers, then serial input can be surprisingly difficult to get working reliably.
At 19200 baud you have almost 500 usec per byte, so a 20MHz clock should be fine used with the USART.WaitForCount() routine.
If anything unexpected happens, you will probably need to restart both ends.

There's a mistake in the array data you're printing out. VarArray(27) has elements VarArray(0)....VarArray(26), not 27
Try this:

Code: Select all

Device = 18F452
Clock = 20

Include "usart.bas"
Include "convert.bas"
Include "LCD.bas"

Public Dim
VarArray(27) As Byte	// array elements are 0..26

SetBaudrate(br19200)

serial:
clear(VarArray)			// set array to all zeros
USART.ClearOverrun()           // clear any current error

USART.WaitForCount( VarArray(),27)
LCD.WriteAt(1,1,(y),Hextostr(VarArray(25)),Hextostr(VarArray(26)))

GoTo serial
The PC program must send EXACTLY 27 bytes of data, and the PIC must be running before the PC program runs.

kerelous58
Posts: 8
Joined: Fri Aug 17, 2012 2:30 pm
Location: egypt

Re: problem to send data from computer to pic via serial com

Post by kerelous58 » Wed May 14, 2014 9:55 pm

Jerry Messina wrote:If you do not use interrupts and buffers, then serial input can be surprisingly difficult to get working reliably.
At 19200 baud you have almost 500 usec per byte, so a 20MHz clock should be fine used with the USART.WaitForCount() routine.
If anything unexpected happens, you will probably need to restart both ends.

There's a mistake in the array data you're printing out. VarArray(27) has elements VarArray(0)....VarArray(26), not 27
Try this:

Code: Select all

Device = 18F452
Clock = 20

Include "usart.bas"
Include "convert.bas"
Include "LCD.bas"

Public Dim
VarArray(27) As Byte	// array elements are 0..26

SetBaudrate(br19200)

serial:
clear(VarArray)			// set array to all zeros
USART.ClearOverrun()           // clear any current error

USART.WaitForCount( VarArray(),27)
LCD.WriteAt(1,1,(y),Hextostr(VarArray(25)),Hextostr(VarArray(26)))

GoTo serial
The PC program must send EXACTLY 27 bytes of data, and the PIC must be running before the PC program runs.

thanks for your interesting and i'll test the code and tell you the result

kerelous58
Posts: 8
Joined: Fri Aug 17, 2012 2:30 pm
Location: egypt

Re: problem to send data from computer to pic via serial com

Post by kerelous58 » Thu May 29, 2014 10:52 pm

kerelous58 wrote:
Jerry Messina wrote:If you do not use interrupts and buffers, then serial input can be surprisingly difficult to get working reliably.
At 19200 baud you have almost 500 usec per byte, so a 20MHz clock should be fine used with the USART.WaitForCount() routine.
If anything unexpected happens, you will probably need to restart both ends.

There's a mistake in the array data you're printing out. VarArray(27) has elements VarArray(0)....VarArray(26), not 27
Try this:

Code: Select all

Device = 18F452
Clock = 20

Include "usart.bas"
Include "convert.bas"
Include "LCD.bas"

Public Dim
VarArray(27) As Byte	// array elements are 0..26

SetBaudrate(br19200)

serial:
clear(VarArray)			// set array to all zeros
USART.ClearOverrun()           // clear any current error

USART.WaitForCount( VarArray(),27)
LCD.WriteAt(1,1,(y),Hextostr(VarArray(25)),Hextostr(VarArray(26)))

GoTo serial
The PC program must send EXACTLY 27 bytes of data, and the PIC must be running before the PC program runs.

thanks for your interesting and i'll test the code and tell you the result


sorry for retarding i have a lot of travail the last two week

i test your code and finally it's work perfectly

thanks for your efforts

kerelous58
Posts: 8
Joined: Fri Aug 17, 2012 2:30 pm
Location: egypt

Re: problem to send data from computer to pic via serial com

Post by kerelous58 » Wed Jun 04, 2014 9:00 pm

sorry again

but i have anther problem
i try to write program which send a count dawn time

and when the micro receive code to stop count dawn and return to the main program

all thing is freezing and no response from the micro

and this is the code

timer:

For minuit=time-1 To 0 Step-1
For second= $3B To 0 Step-1

USART.Write($5A,$A5,$05,$82,$00,$07,$00,minuit)
USART.Write($5A,$A5,$05,$82,$00,$09,$00,second)



LCD.WriteAt(2,4,DecToStr(minuit)," : ",DecToStr(second))



If USART.DataAvailable Then

Clear(VarArray)
USART.ClearOverrun()
USART.WaitForCount( VarArray(),9)

x=VarArray(5)
If x=8 Then // if x=8 that mean stop count dawn and goto main program

DelayMS(1000)
USART.Write($5A,$A5,$04,$80,$03,$00,$00) // this adress sent from micro to inform that the count dawn is stopped
Cls
GoTo serial
EndIf

EndIf



DelayMS(1000)

Next
Next // after the count dawn finished
USART.Write($5A,$A5,$04,$80,$03,$00,$00)// this adress sent from micro to inform that the count dawn is stopped
Cls
GoTo serial

Post Reply