arduinoでmbps通信

#概要
arduinoでmbps通信やってみた。

#詳しく
arduino unoとノートPCのserial通信で1Mbpsをやってみた。
arduinoから、128x64の画像ピクセルをHEXにエンコードして、2048バイト、1Mbpsで送信する。
ノートPCのc#で受信する。

#結果

自分の環境では、ビット落ちしてる。

![image](https://qiita-image-store.s3.amazonaws.com/0/18104/4a2b5f8e-43d9-b80d-6a79-c54db925654f.png)

#サンプルコード
画像ピクセル

http://jsdo.it/ohisama1/UdiN

#サンプルコード
送信

```
#include <stdint.h>
#include <avr/io.h>
#include <util/delay.h>
#include <avr/pgmspace.h>

uint8_t pix[] = {
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 3, 128, 0, 0, 248, 0, 0, 7, 128, 0, 0, 0, 0, 0, 0, 112, 3, 128, 0, 0, 240, 0, 0, 7, 128, 0, 0, 0, 0, 0, 0, 112, 3, 128, 0, 0, 240, 0, 0, 7, 128, 0, 0, 0, 0, 0, 0, 113, 255, 255, 15, 248, 240, 0, 0, 7, 128, 0, 0, 0, 0, 0, 0, 113, 255, 255, 15, 249, 224, 0, 0, 7, 128, 0, 0, 0, 0, 0, 7, 255, 120, 120, 15, 121, 255, 252, 0, 7, 128, 0, 0, 0, 0, 0, 7, 255, 60, 120, 15, 123, 255, 252, 31, 255, 255, 224, 0, 0, 0, 0, 0, 112, 60, 112, 15, 123, 255, 252, 31, 255, 255, 224, 0, 0, 0, 0, 0, 112, 60, 240, 15, 127, 191, 252, 30, 7, 128, 224, 0, 0, 0, 0, 0, 112, 60, 240, 15, 127, 191, 252, 30, 7, 128, 224, 0, 0, 0, 0, 0, 115, 255, 255, 143, 127, 63, 252, 30, 7, 128, 224, 0, 0, 0, 0, 0, 115, 255, 255, 143, 126, 123, 252, 30, 7, 128, 224, 0, 0, 0, 0, 0, 112, 15, 128, 15, 124, 123, 252, 30, 7, 128, 224, 0, 0, 0, 0, 0, 126, 15, 0, 15, 120, 247, 188, 30, 7, 128, 224, 0, 0, 0, 0, 1, 254, 15, 0, 15, 120, 247, 188, 30, 7, 128, 224, 0, 0, 0, 0, 7, 249, 255, 255, 15, 121, 231, 188, 31, 255, 255, 224, 0, 0, 0, 0, 7, 241, 255, 255, 15, 251, 239, 60, 31, 255, 255, 224, 0, 0, 0, 0, 3, 112, 60, 120, 15, 255, 207, 60, 30, 7, 128, 224, 0, 0, 0, 0, 0, 112, 120, 120, 15, 31, 158, 60, 30, 7, 128, 0, 0, 0, 0, 0, 0, 112, 120, 240, 15, 15, 62, 60, 0, 7, 128, 0, 0, 0, 0, 0, 0, 112, 255, 240, 15, 6, 124, 60, 0, 7, 128, 0, 0, 0, 0, 0, 0, 112, 255, 224, 0, 0, 248, 60, 0, 7, 128, 0, 0, 0, 0, 0, 0, 112, 7, 248, 0, 1, 240, 60, 0, 7, 128, 0, 0, 0, 0, 0, 0, 112, 31, 254, 0, 7, 224, 120, 0, 7, 128, 0, 0, 0, 0, 0, 0, 240, 255, 63, 0, 15, 195, 248, 0, 7, 128, 0, 0, 0, 0, 0, 1, 243, 248, 15, 0, 15, 3, 240, 0, 7, 128, 0, 0, 0, 0, 0, 1, 225, 192, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
char enc[] = "0123456789abcdef";

void setup()
{
	UBRR0H = 0;
	UBRR0L = 1;
	UCSR0A |= 2;
	UCSR0B = (1 << RXEN0) | (1 << TXEN0);
	UCSR0C = 6;
	pinMode(13, OUTPUT);
	digitalWrite(13, LOW);
}
void loop()
{
	int i;
	for (i = 0; i < 1024; i++)
	{
		UDR0 = (uint8_t) enc[pix[i] >> 4];
		while (!(UCSR0A & (1 << UDRE0)));
		UDR0 = (uint8_t) enc[pix[i] & 0xf];
		while (!(UCSR0A & (1 << UDRE0)));
	}
	digitalWrite(13, HIGH);
	delay(5000);
	digitalWrite(13, LOW);
}




```

#サンプルコード
受信

```
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication13
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {            
            List<byte> received = new List<byte>();            
            serialPort1.Open();
            Console.Write("start\n");
            while (true)
            {
                if (serialPort1.BytesToRead > 0)
                {
                    received.Add((byte) serialPort1.ReadByte());
                    if (received.Count > 2047)
                    {
                        break;
                    }
                }
            }
            serialPort1.Close();
            byte[] data = received.ToArray();
            byte[] pix2 = new Byte[1024];;
            for (int i = 0; i < 1024; i++)
            {

                pix2[i] = hex2byte(data[i * 2], data[i * 2 + 1]);
            }
            Bitmap img = new Bitmap(128, 64);
            for (int i = 0; i < 16; i++)
            {
                for (int j = 0; j < 64; j++)
                {
                    int p = j * 16 + i;
                    int z = pix2[p];
                    for (int k = 0; k < 8; k++)
                    {
                        int m = ((z << k) & 0x80);
                        if (m == 0x80) m = 0;
                        else m = 255;
                        Color c = Color.FromArgb(m, m, m);
                        int x = i * 8 + k;
                        int y = j;
                        img.SetPixel(x, y, c);
                    }
                }
            }
            pictureBox1.Image = img;
            this.Refresh();
            Console.Write("ok\n");
        }
        byte hex2byte(byte c1, byte c2)
        {
	        if (c1 >= 'A' && c1 <= 'F') c1 -= 7;
	        else if (c1 >='a' && c1 <= 'f') c1 -= 39;
	        else if (c1 < '0' || c1 > '9') return 0;
	        if (c2 >= 'A' && c2 <= 'F') c2 -= 7;
	        else if (c2 >= 'a' && c2 <= 'f') c2 -= 39;
	        else if (c2 < '0' || c2 > '9') return 0;
	        return (byte) (c1 << 4 | (c2 & 0xf));
        }
    }
}

```

以上。