문제

I have to send file byte-by-byte to serially connected AT89s52 from computer (VB.NET).
Every sended byte have some job to do in microcontroller what require some time.
Here is relevant part of my C code to receiving bytes:

SCON = 0x50;
TMOD = 0x20; // timer 1, mode 2, 8-bit reload
TH1  = 0xFD; // reload value for 9600 baud
TR1  = 1;
TI   = 1;

again:

        while(RI!=0)
        {
            P1=SBUF;          // show data on led's
            RI=0;
            receivedBytes++;
        }

        if (key1==0)
        {
            goto exitreceive; // break receiving
        }

        show_lcd_received_bytes(receivedBytes); 
        // here is one more loop 
        // with different duration for every byte
        goto again;

And here is VB.NET code for sending bytes:

    For a As Integer = 1 To 10
        For t As Integer = 0 To 255
            SerialPort1.Write(Chr(t))
        Next t
    Next a

Problem is that mC have some job to do after every received byte and VB.NET don't know for that and send bytes too fast so in mC finishes just a part of all bytes (about 10%). I can incorporate "Sleep(20)" in VB loop ant then thing will work but I have many of wasted time because every byte need different time to process and that would be unacceptable slow communication.

Now, my question is if 8051 can set some busy status on UART which VB can read before sending to decide to send byte or not. Or how otherwise to setup such communication as described? I also try to receive bytes with serial interrupt on mC side with same results.

Hardware is surely OK because I can send data to computer well (as expected).

도움이 되었습니까?

해결책

Your problem is architectural. Don't try to do processing on the received data in the interrupt that handles byte Rx. Have your byte Rx interrupt only copy the received byte to a separate Rx data buffer, and have a background task that does the actual processing of the incoming data without blocking the Rx interrupt handler. If you can't keep up due to overall throughput issue, then RTS/CTS flow control is the appropriate mechanism. For example, when your Rx buffer gets 90% full, deassert the flow control signal to pause the transmit side.

다른 팁

As @TJD mentions hardware flow control can be used to stop the PC from sending characters while the microcomputer is processing received bytes. In the past I have implemented hardware flow by using an available port line as an output. The output needs to be connected to an TTL to RS-232 driver(if you are currently using a RS-232 you may have and extra driver available). If you are using a USB virtual serial port or RS-422/485 you will need to implement software flow control. Typically a control-S is sent to tell the PC to stop sending and a control-Q to continue. In order to take full advantage of flow control you most likely will need to also implement a fully interrupt driven FIFO to receive/send characters.

If you would like additional information concerning hardware flow control, check out http://electronics.stackexchange.com.

Blast from the past, I remember using break out boxes to serial line tracers debugging this kind of stuff.

With serial communication, if you have all the pins/wires utililzed then there is flow control via the RTS (Ready To Send) and DTR (Data Terminal Ready) that are used to signal when it is OK to send more data. Do you have control over that in the device you are coding via C? IN VB.NET, there are events used to receive these signals, or they can be queried using properties on the SerialPort object.

A lot of these answers are suggesting hardware flow control, but you also have the option of enhancing your transmission to be more robust by using software flow control. Currently, your communication is strong, but if you start running a higher baud rate or a longer distance or even just have a noisy connection, characters could be received that are incorrect, or characters could be dropped.

You could add a simple two-byte ACK sequence upon completion of whatever action is set to happen. It could look something like this:

Host sends command byte: <0x00> Device echoes command byte: <0x00> Device executes whatever action is needed Device sends ACK/NAK byte (based on result):

This would allow you to see on the host side if communication is breaking down. The echoed character may mismatch what was sent which would alert you to an issue. Additionally, if a character is not received by the host within some timeout, the host can try retransmitting. Finally, the ACK/NAK gives you the option of returning a status, but most importantly it will let the host know that you've completed the operation and that it can send another command.

This can be extended to include a checksum to give the device a way to verify that the command received was valid (A simple logical inverse sent alongside the command byte would be sufficient).

The advantage to this solution is that it does not require extra lines or UART support on either end for hardware flow control.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top