Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How do i read a certain (fixed) string to an uart and read the result??

Status
Not open for further replies.

Dasco

Junior Member level 2
Joined
Jul 21, 2008
Messages
22
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,435
How do i writea certain (fixed) string to an uart and read the result??

---------- Post added at 09:51 ---------- Previous post was at 09:50 ----------

i am using a pic24
 

there are example programs for driving the PIC24 UART on the Microchip page
**broken link removed**

also if you have a development board it should have come with example programs on a CD

in addition once you have initialised the UART baudrate etc you can use the standard C printf() function to write to the UART (you mayhave to change the default UART that printf() uses)

are you using a develoment board or have you built you own PCB?
 

I HAVE A PCB
AND i HAVE A CASE STRUCTURE WITH DIFFERENT (FIXED) STRINGS THAT I WANT TO SEND AND RECIEVE THE RESULT ( UNFIXED LENGTH WITH A END OF TEXT CHARACTER)
CAN YOU GIVE ME N EXAMPLE CODE?
 

if you download and install the Microchip application libraries
**broken link removed**

you will find lots of example code and in particular in directory C:\Microchip Solutions v2010-10-19\Microchip\Common you will find a file UART2.c
 

I INSTALLED THE LIB AND LOOKED INTO UART2
THAT IS NOT WHAT I AM LOOKING FOR
WHAT I WANNA DO IS SOMETHING LIKE THIS :



void BLA BLA (unsigned char YADA) {

SWITCH (YADA)
{
case 1: {
/*
SENDS A CERTAIN STRING with two characters via uart3 and should recieve its reply that ends with a certain character ( let's say 0xFF)
*/

break;
}
case 2: {
/*
SENDS A CERTAIN STRING with two characters via uart3 and should recieve its reply that ends with a certain character ( let's say 0xFF)
*/

break;
}
and some more cases
how can i implement this?
thanks in advance for your guidance
 

if you look in uart2.c there is a function to print a string
Code:
void UART2PrintString( char *str )
{
    unsigned char c;

    while( (c = *str++) )
        UART2PutChar(c);
}
to use UART3 copy uart2.c to uart3.c and change references to UART2 to UART3, U2 to U3, etc
which PIC24 are you using? if it has Perpheral Pin Select you will need to tell it which pins are the UART3 Tx and Rx
 

can you makea an example part for one of the cases??
I am a total beginner

---------- Post added at 11:26 ---------- Previous post was at 11:23 ----------

i have u3rx and u3tx and
 

something like
Code:
SWITCH (YADA)
{
case 1: UART3PrintString("01");
break;
case 2: UART3PrintString("03");
break;
 

and how to read the result??
it is not fixed length but wtih a end of message character
 

something along the lines of
Code:
// read a string, terminate on \n, return length of string
int UART3readString(char *data)
{
  int index=0;
  data[0]=0;
  while(1)
    {
    char ch=UART3getChar(); 
    data[index++]=ch; data[index]=0;
    if(ch=='\n') return index;
    }
}
note there is no error checking. if you read more characters than the length of the allocated array you will overwrite memory
 

so it will be soemthing like this??

SWITCH (YADA)
{
case 1: UART3PrintString("01");
int UART3readString(char *data)
{
int index=0;
data[0]=0;
while(1)
{
char ch=UART3getChar();
data[index++]=ch; data[index]=0;
if(ch=='\n') return index;
}
}
break;
case 2: UART3PrintString("03");
int UART3readString(char *data)
{
int index=0;
data[0]=0;
while(1)
{
char ch=UART3getChar();
data[index++]=ch; data[index]=0;
if(ch=='\n') return index;
}
}
break;.
 

more like
Code:
char data[100];
SWITCH (YADA)
{
case 1: UART3PrintString("01");
           UART3readString(data);
           break;
case 2: UART3PrintString("03");
           UART3readString(data);
           break;
 

so it will be soemthing like this :


void UART2PrintString( char *str )
{
unsigned char c;

while( (c = *str++) )
UART2PutChar(c);
}
int UART3readString(char *data)
{
int index=0;
data[0]=0;
while(1)
{
char ch=UART3getChar();
data[index++]=ch; data[index]=0;
if(ch=='\n') return index;
}
}
char data[100];
SWITCH (YADA)
{
case 1: UART3PrintString("01");
UART3readString(data);
break;
case 2: UART3PrintString("03");
UART3readString(data);
break;

right?

---------- Post added at 13:17 ---------- Previous post was at 12:49 ----------

i have four buffers in my peripheral :


ReadRxBuf
WriteRxBuf
ReadTxBuf
OWriteRxBuf

and 2 uart buffers:
U3Rx
U3Tx

which one reads which one? and which ones writes into which one?
 

looking better - probably time to use MPLAB to create a project and try to compile the code.
don't forget to put your switch() in a main() function and also call UART3init() to initialise the UART.
You will also need to include the other UART3 functions.
what are you connecting UART3 too?
does you PCB have a serial connection to PC for debugging?
 

i have an optical device i want to set some values and read the data
i can debug using MPLab
but I have 4 buffers of the optical device and 2 buffers from my uart
which one goes where?
 

I think to need to read the documentation of the optical device to find out what the two sets of Tx and Rx buffers do. Are theu both UARTs or is one SPI and the other a UART. If two UARTs one may be for contol and one for data, you may need to use two UARTs to talk to it.
 

it communicates via RS232 and it uses two buffers (WriteRxBuf ,WriteTxBuf)
to write in the circular buffer of the UART and two buffer (ReadRxBuf ,ReadTxBuf)
to reads from the circular buffer.

which one should i use to write into optical device and which one should i use to read from it?

---------- Post added at 16:09 ---------- Previous post was at 14:11 ----------

i tried this in MPlab and when ibuild i get

implicit declaration of function 'UART3getChar'
implicit declaration of function 'UART3putChar'

what i tried was :

void UART3PrintString( char *str )
{
unsigned char c;

while( (c = *str++) )
UART3PutChar(c);
}
int UART3readString(char *data)
{
int index=0;
data[0]=0;
while(1)
{
char ch=UART3getChar();
data[index++]=ch; data[index]=0;
if(ch=='\n') return index;
}
}
char data[100];
switch (YADA)
{

case 1: {
UART3PrintString("AB");
UART3readString(data);
 

you need to include uart3.c in the project (modify uart2.c as mentioned in previous posts)

I think we need more details of the optical device to understand the operation of its UARTs - what is the model number?
 

now i am lost where did we modify an uart3.c??
i can send you the codes and all the info via email if you want
 

if you download and install the Microchip application libraries
**broken link removed**

in directory C:\Microchip Solutions v2010-10-19\Microchip\Common you will find a file UART2.c

copy it to uart3.c and modify it to drive UART3, e.g. change U2 to U3 etc
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top