Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Rx Tx with two arduino

Status
Not open for further replies.

qwertyqwq

Member
Hi.
I have a project , which it consist of comminication of two arduino with rx,tx. The goal is;
For example first arduino will send something like "P120" . After sended the message to second arduino , it will change (for example) pwm dutycycle to 120.
And also second arduino will send messages something like that. So it needs to read "P" letter at first and then compute "120" to something. Looks like gcode.
I searched on internet for a library or a code , cause i dont know how to write a code. If you guys have any example like i searched to , i can take it gladly. Or if you know any page , also i can read it gladly. Thanks.
 
Unless you're using Arduinos with two UARTs (Serial ports) then you're stuck as the UART is used to communicate with the PC.

You could use I²C (IIC) or SPI to do the same thing and there are lots of examples on the web. For Arduino look at the wire library.

Mike.
 
Unless you're using Arduinos with two UARTs (Serial ports) then you're stuck as the UART is used to communicate with the PC.

Not at all, there are masses of examples using software serial ports, and there's a well supported library to do so.

Although I didn't really see any requirement in his post requiring two serial ports anyway, or indeed any mention of how they might be communicating.
 
Unless you're using Arduinos with two UARTs (Serial ports) then you're stuck as the UART is used to communicate with the PC.

You could use I²C (IIC) or SPI to do the same thing and there are lots of examples on the web. For Arduino look at the wire library.

Mike.
There is no needed for pc comminication. Only two arduinos will comminicate eachothers. Like;
For example first arduino will send something like "P120" . After sended the message to second arduino , it will change (for example) pwm dutycycle to 120.
And also second arduino will send messages something like that. So it needs to read "P" letter at first and then compute "120" to something.
as i said here.
Now i started to look at ascii code for transfer word and number together. And after it sended to another arduino , it will decode ascii to string or number. Is that method good for my project ?
 
I've tried software serial and it's half duplex unless someone wrote a new library.

You may not need to talk to the PC but your Arduino does, that's how it gets programmed. Try googleing "Arduino master slave communication".

Mike.
 
I've tried software serial and it's half duplex unless someone wrote a new library.

You may not need to talk to the PC but your Arduino does, that's how it gets programmed. Try googleing "Arduino master slave communication".

Why would you consider half duplex a problem?, nothing in his description to suggest a requirement for full duplex, which is fairly rare to need anyway.

The serial port is only during programming, just as a PIC uses a couple of pins during programming, the serial port is perfectly free to use once it's programmed.

I don't see any relevance to "Arduino master slave communication" in this case?
 
Is this any use? It's an "extra" serial port routine I wrote for a PIC, that uses interrupts & can work full duplex with no software delays.
I'm not familiar with Arduino, but if they have suitable timers and interrupt on change, it may be adaptable?

For transmit, check cd_tx_btc is zero then load cd_tx_buf and set cd_tx_btc to 10.

The receive routine calls cd_rput() when a complete character is received; that was a function to append the new byte to a buffer in my program.

The TX routine also had a circular buffer in my program and that was checked by one of the interrupt routines to see if there was data waiting to be sent.

C:
int        cd_tx_btc; // Bit Count
int16    cd_tx_buf; // Serial data buffer

int        cd_rx_btc; // Bit count
int16    cd_rx_buf; // Serial data buffer


#INT_TIMER1
void timer1_isr() {
    // Timer 1; 16 Bit, clocked at 9,830,400 Hz for RS232 Receive timing.
    // 1024 clocks = 1 bit time @ 9600 Baud.
    // 0000 - 0x200 = 0xfc00
    l = get_timer1();
    set_timer1(0xfc30);
    
    if(cd_rx_btc)
        // Presently processing a character
        if(cd_rx_btc == 10 && input_state(PIN_C2))
            // Abort if centre of start bit |= 0
            cd_rx_btc=0;
        else if(cd_rx_btc) {
            cd_rx_buf >>= 1;
            if(input_state(PIN_C2))
                cd_rx_buf |= 0x100;
            cd_rx_btc--;
            if(!cd_rx_btc) {
                if(cd_rx_buf & 0x100)
                    // Stop bit present, Start bit shifted out.
                    cd_rput(cd_rx_buf & 0xff);
            }
        }
}


#INT_CCP1
void ccp1_isr() {
    // Triggered on C2 (cd_rxd) falling edge; possible start bit
    // Already in receive?
    if(cd_rx_btc)
        return;

    // New character, setup..
    cd_rx_btc=10;
    cd_rx_buf=0;
    set_timer1(0xfe30);
}

#INT_RTCC
void rtc_isr() {
    // Prescaler /4, 8 bit counter, Int every 1024 clocks
    // 9.8304MHz / 1024 = 9600Hz
    //INT Routine timing
//output_high(pin_d0);


    // Software RS232 Routines
    // Transmit
    if(cd_tx_btc) {
        if(cd_tx_buf & 1)
            output_high(PIN_A5);
        else
            output_low(PIN_A5);
        
        cd_tx_buf >>= 1;
        cd_tx_btc--;
    }
    else
        output_high(PIN_A5);

}

// In main() initialisation section:

    setup_timer_0(RTCC_INTERNAL | RTCC_DIV_4 | RTCC_8_BIT);
    setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);
    setup_ccp1(CCP_CAPTURE_FE);

    enable_interrupts(INT_RTCC);
    enable_interrupts(INT_TIMER1);
    enable_interrupts(INT_CCP1);
    enable_interrupts(GLOBAL);
 
For general command interpretation:

Use a character array[] and an integer or byte index to that.
Set the index to zero during the setup.

As each character is received,
If the character is a carriage return, put a zero in the array at the index location & copy the string the array contains to a separate array for parsing, then set the variable with the input array index to zero.

If it's not a return character, put it in the character array at the index location and increment the index to the array.
If the index (= character count) is greater than the maximum possible characters in a command, set it to zero.

The second array with the saved command can be processed separately from the serial routine.

eg. Use a switch() statement to select which command based on the first character.

You can do some basic validation first by checking the first character is a letter and the second a number (or - if you allow negative numbers, or whichever format you use.

For a similar thing, I used a loop that read each command section or number and stored each in arrays, working through the entire received line of text. At the end, the number of parameters was also saved.

The sections of the switch() only had to check the number of parameters was valid for each command & use the appropriate elements from the arrays.
 
Have a look at this little tutorial and search for his other links, he has some good code examples.

We based our simple comms on this code.

https://www.gammon.com.au/serial

If you are transmitting over more that a few meters, then consider using the little rs232 or similar modules, again works for us over 10mts +


000168.jpg
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top