Main Site Documentation

ChipworkX to Fez Serial Connection


#1

Hello,

Following your advise to use serial connection for communication between ChipworkX and Panda, I have encountered several problems. Here is what I do:

I have two projects - one for the ChipworkX and another for the Panda. They are both referencing a third project, holding the serial port configuration (so it is the same for both projects), here it is:



private SerialPort serial;
private static byte[] inBuffer;

public SerialPortControl(string portName)
        {
            serial = new SerialPort(portName, 9600, Parity.None, 8, StopBits.One);
            serial.WriteTimeout = -1;
            serial.ReadTimeout = -1;
            serial.Handshake = Handshake.XOnXOff;
            serial.Open();
            serial.DataReceived += new SerialDataReceivedEventHandler(serial_DataReceived);
            serial.ErrorReceived += new SerialErrorReceivedEventHandler(serial_ErrorReceived);
        }

        public void SendData(byte[] data)
        {
            serial.Write(data, 0, data.Length);
        }

        public byte[] ReceiveData()
        {
            inBuffer = new byte[256];
            serial.Read(inBuffer, 0, 256);
            return inBuffer;
        }

What it seems, is that the serial port buffer is of size 256 bytes, that is why I have set the inBuffer at that size and also reading that many bytes from the port. Now, the problem I have is on several levels:

  1. When sending data from the ChipworkX to the Panda and just printing it out using Debug.Print (I know the panda doesn’t have much memory), I don’t get all the data pritned out. I have made several experiments:

            Bytes
    

    Sent | Received
    1024 | 768
    1536 | 1024
    4864 | 3328
    So the problem here is that if the data sent is bigger than 256 bytes, it seems that some of it is lost. If it is less than 256, however, all of it is transmitted

  2. I would also like the Panda to respond from time to time. So what I do is subscribe ChipworkX to the data received event (in exactly the same way like the Panda). I set a loop on the Panda to send a message every 3 seconds. Sometimes I will get the event fired every time, other times (almost always), I get the event fired once every 13 minutes (exactly!).

  3. When I am using the ChipworkX COM1 or COM3 port, the touchscreen doesn’t work. When using COM2 port the touchscreen sometimes works, sometimes not. The screen itself is refreshed, only the touch component doesn’t work.

I have read quite alot on serial ports the last days and what I don’t understand is the Handshaking part. I believe actually some of my problems are caused by that. As you can see, I have set the handshaking, but how to stop and resume data transfer? Any help would be much appreciated. Thanks :slight_smile:


#2

What about using handshaking? There is xon/xoff implemented in NETMF the believe but I never used it.

You need a way to handle handshaking, through software or hardware.

You could have just used the hardware handshaking on COM2 but release notes says it is not functional.


#3

Yes, I also believe that’s what I’m missing. What I don’t understand is how to use the handshaking. Besides setting the property, nothing else is clear. There isn’t any method for stopping the data flow, neither one for requesting a resume of the data flow.


#4

You can packetize your data. Packet goes out, no more is sent till ok is returned. You can do that using some software you define or through IOs for handshaking.


#5

You are assuming that when you get a DataReady event there are 256 bytes ready to read. This is not the case.

You should use the BytesToRead property to determine how many bytes are in the internal buffer, and then read that data.

Seems funny things happen when you try to read more bytes than are in the internal buffer in the event.


#6

Thanks for the reply :slight_smile:
Can you be more specific.
I don’t understand how the handshaking takes place - is it something that needs to be invoked manually? Because if it is, I cannot find any method for that in th SerialPort class.
I’m using .NET Micro 4.1 and the SerialPort class is in the Microsoft.SPOT.Hardware.SerialPort.dll, v4.0.30319 assembly.
If the handshaking is something that doesn’t need to be invoked manually is it supposed to automatically stop and resume the data flow? And do I need to manually empty the SerialPort InBuffer?
If you have an example code using handshaking that would be most helpful. Thanks again


#7

Let’s look at this differently, what is it that you are trying to transfer between ChipworkX and FEZ Panda. Maybe if I understand what you are trying to do then I can give you better answer.


#8

Thanks for the response Mike.
I followed your advise and it seems it works better now. The problem with missing data is still there, however. I just tested with an array of size 889 and the whole was transferred with no problems (for the first time! :slight_smile: ). In my next test - an array 1787, only 1434 were transferred and a lot of ErrorReceived events were fired. Any suggestions?


#9

@ Gus
I’m trying to transfer an array of bites. On the Panda a loop will run going through each byte, getting each bit and signaling pins according to the bit value. The problem is that the byte array can get pretty big and I want it waiting in the ChipworkX in the sending queue until Panda has finished processing the last batch.


#10

Ok good, then ChipworkX can have a queue of 100-byte-arrays. Then you can send 100 bytes to Panda and wait for panda to signal “I am ready fro more”. The signal can be a byte coming back or a pin.

Panda will have a thread looping waiting for incoming data and processing them. Whenever it is done, it signals ready to ChipworkX


#11

Thanks, that makes sense :slight_smile:
But if I am to signal back to ChipworkX with a byte, then where the handshaking part comes into action?


#12

that is the handshaking, the byte you send back or a pin that you set high…etc.

You are not using the handshaking built into UART but you are designing your own handshaking into your system


#13

Thanks, Gus
I will try out your suggestions and will update you tomorrow :slight_smile:


#14

Hi all,
thanks for the help, it’s working now :slight_smile: