chipKIT® Development Platform

Inspired by Arduino™

PC RS232 to Fubarino SD UART problem

Created Tue, 16 Aug 2016 14:38:41 +0000 by buzby


buzby

Tue, 16 Aug 2016 14:38:41 +0000

Hi,

I am an embedded software developer and I'm currently using a Fubarino SD 1.5 to prototype from firmware that will be running on a board usingf a PIC32MX795. The board will take commands in over RS422 serial and copy them to another legacy board via DMA through the PMP port and vice versa. It's replacing an old board that ran PLM code on an 8085 with an 8257 DMA controller.

I converted the firmware to C and got it all working ok on the Fubarino (I'm currently doing a memory-to-memory DMA transfer to simulate the DMA side until our PCB is ready) and I've written a Visual C++ program to drive it from the serial port, simulating the messages from the other end olf the link (which is going to be another board with a PIC32MX on it eventually). All was going well until I noticed an odd problem when receiving messages from the Fubarino on the PC (I'm using Serial1 on pins 28 & 28 attached to a MAX232 running at 9600 8N1 with no flow control).

The protocol over the serial link is a Command-Response protocol. The Master end (the PC in this case) sends a Command, which is received by the Slave end (the PIC) . The Slave sends a Response to the Command by copying the received message into it's outgoing buffer and modifying the value of the second byte to show it is a response. The problem I am seeing is that on the PC I appear to be receiving the Response prefaced the Command that was sent (i.e if I send a 4 byte Command 0x04,0x01,0x04,0x00 I am receiving 8 bytes 0x04,0x01,0x04,0x00,0x04,0x11,0x04,0x00 when I'm only expecting the last 4).

I tried using the Br@y Terminal to replace my VC++ program and get the same response back, and if I run the MC PortMon tool on the PC's serial port it shows 8 bytes being received instead of 4. However, If I disconnect the Fubarino and loop back pins 2 & 3 in the PC's serial cable I only see 1 byte at a time/1 copy of the message being recieved on the PC.

I am using MPIDE 0023 (which I realise is unsupported, but the HardwareSerial library should be relatively safe from bugs) and I'm using the Serial1.readBytes(buf, len) and Serial1.Write(buf, len) methods to read and write to the serial port (using 2 separate buffers). The only thing I can think of is it's something to do with timing, as the code on the Fubarino sends the response almost immediately on receiving the command.

Any help or guidance would be appreciated, even if it's just form someone who has used one of the UARTs fro RS232 and can confirm they work ok.


majenko

Tue, 16 Aug 2016 15:16:08 +0000

The UART ports certainly work. I am sure someone would have noticed by now if they didn't.

There must be something wrong with either how you are reading and writing your data, or what you are doing with it once you have read it.

This code certainly works:

void setup() {
    Serial1.begin(9600);
}

void loop() {
    static char inbuf[4];
    static int ibPtr = 0;
    if (Serial1.available()) {
        char in = Serial1.read();
        inbuf[ibPtr++] = in;
        if (ibPtr == 4) {
            inbuf[1] &= 0x80;
            Serial1.write(inbuf, 4);
            ibPtr = 0;
        }
    }
}

It receives 4 bytes, sets the highest bit on the second byte, and echoes it back.


buzby

Thu, 18 Aug 2016 22:14:19 +0000

Thanks for the reply. I never really doubted the PIC32MX UART was functioning correctly, but I was pulling my hair out trying to work out what was going on. When I tried your simple sketch, I saw even stranger output on the PC, with the command still appearing when the PC read the serial port , but with start of the response was overwriting the end of the command too.

I was using my old development PC (and XP desktope with Visual C++ 6.0 Pro installed) as it has a built-in serial port. I got hold of a PCIe serial adapter for my Windows 7 laptop, and decided to rewrite my test program in C# as I have Visual C# Express installed on it from a previous project. To my surprise the C# version under Win7 works perfectly, with only the responses appearing when the serial port is read. I then tried it with Br@y terminal and was again only seeing the responses being received. I can only assume the problem on my XP machine is either with some form of output buffering in the serial port hardware or driver.

Thanks again for your help.