楽天市場
[無料でホームページを作成] [通報・削除依頼]

続8ピンPICでUSB -ターミナルソフトからの操作-

16FUSB 擬似CDC を実装したPICをターミナルソフトから使用する試み


<前準備>

壱. 16FUSBはCOMポートとして認識されないので、このままではターミナルソフトから使用することができない。仮想COMポート com0com をインストールして、このポートにターミナルソフトを接続できるようにする。

弐. USBとシリアル(Com0Com)の橋渡しをするドライバーを作成する。


<テスト回路>


img_20130228-005127.jpg


img_20130228-005222.jpg


<com0comの設定>

com0comをインストールし、COM8とCOM9をクロス接続する。

COM9はドライバ側でオープンし、TERATERM(ターミナルソフト)はCOM9にクロス接続されたCOM8をオープンする。この構成により、ドライバとターミナルソフトでのCOMポートオープンの多重化問題が解決される。 以下com0comの登録と設定を示す。


img_20130228-010403.jpgimg_20130228-010434.jpg


<USB-シリアルブリッジドライバ>

img_20130228-011127.jpg

前回同様 16FUSB_dio_hid_sample_application_win32-src-1.0.zip
を展開、Form1.hを書き換えて、再コンパイル。 プロジェクトファイルはそのまま使用。Form1.hは下部に記載。 

前回のプログラムから文字入力・表示部を無くし、漢字送受信(Shift-jis)に対応。シリアル送受信(9600ボー、TERATERM標準条件)を追加。


<ファームウエア:16FUSB側のソフト>

変更なし


<ループバックテスト1:文字転送>

前回のループバックテストは16FUSBのシリアル出力TXとシリアル入力RXをショートして行ったが、今回は16FUSBのシリアルとPIC10F322のソフトシリアルとをクロス接続して使用(上図参照)、このときシリアル信号は16FUSB(PIC12F1822)で反転されるため、PIC10F322側で再反転する必要がある。


まず、PIC10F322側から連続したデータを送るようにプログラムした、ノーウエイトでは処理が追いつかず、送信に1.4ms程度ウエイトを入れた。(PIC10F322は 8MHZで作動)

以下は受信データをそのまま送り返すPIC10F322側のプログラム

call GETC;1文字受信サブルーチン  
call PUTC
goto $-2   

PUTC
call rs1tx ;1文字送信サブルーチン
clrf rxd;50μsのウエイトを256回実施
call bwait2
decfsz rxd,F
goto $-2
return


<ループバックテスト2:  TERATERMからファイル転送>

転送元ファイル

img_20130228-014649.jpg


上記ファイルをTERATERMから転送したが、受信側(TERATERM)で改行されなかった。

原因は送信側でラインフィード0x0Aのコードを送信しないため。

TERATERMの通信条件を以下のように変更する。 Transmit:CR+LF

img_20130228-015301.jpg 

やはり処理が追いつかないので送信側でディレイ500msを入れた。

img_20130228-015523.jpg

ディレイが大きいのが気になるが、受信成功。漢字も正しく表示されている。

img_20130228-015736.jpg



<Form.h>



#pragma once

#include "stdio.h"
#include <wtypes.h>
#include <string>
#include "Usb.h"
#include <iostream>
#include <stdlib.h>

namespace My16FUSB_HID_DioSampleAPP {

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace std;
using namespace System::Drawing;
using namespace System::Text;
using namespace System::Threading;

/// <summary>
/// Summary for Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
delegate void SetCheckDelegate();
public:
Form1(void)
{
InitializeComponent();

usb = gcnew Usb();
usb->VendorID = 0x04D8;
usb->ProductID = 0x0628;

if(usb->findDevice()) {
this->toolStripStatuslabel1->Text = "16FUSB Connected com0com(COM9)";

usb->openReadHandle();
bgWorker = gcnew System::ComponentModel::BackgroundWorker();
bgWorker->DoWork += gcnew DoWorkEventHandler( this, &Form1::inputThread );
bgWorker->RunWorkerAsync();

usb->openWriteHandle();
} else {
System::Windows::Forms::MessageBox::Show( L"Device not found!", L"16FUSB",
System::Windows::Forms::MessageBoxButtons::OK);
}
}

protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}

private: System::IO::Ports::SerialPort^ serialPort;
private: System::ComponentModel::IContainer^ components;
protected:

private:
/// <summary>
/// Required designer variable.
/// </summary>



private: System::Windows::Forms::StatusStrip^ statusStrip1;
private: System::Windows::Forms::ToolStripStatusLabel^ toolStripStatuslabel1;

System::ComponentModel::BackgroundWorker^ bgWorker;

Usb^ usb;


#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->components = (gcnew System::ComponentModel::Container());
this->serialPort = (gcnew System::IO::Ports::SerialPort(this->components));
this->statusStrip1 = (gcnew System::Windows::Forms::StatusStrip());
this->toolStripStatuslabel1 = (gcnew System::Windows::Forms::ToolStripStatusLabel());
this->statusStrip1->SuspendLayout();
this->SuspendLayout();
//
// serialPort
//
this->serialPort->PortName = L"COM9";
this->serialPort->BaudRate = 9600;
this->serialPort->DataBits = 8;
this->serialPort->Parity = System::IO::Ports::Parity::None;
this->serialPort->StopBits = System::IO::Ports::StopBits::One;
this->serialPort->Open();
this->serialPort->Encoding = System::Text::Encoding::GetEncoding(932);
this->serialPort->DataReceived += gcnew System::IO::Ports::SerialDataReceivedEventHandler(this, &Form1::serialPort_DataReceived);
//
// statusStrip1
//
this->statusStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(1) {this->toolStripStatuslabel1});
this->statusStrip1->Location = System::Drawing::Point(0, 197);
this->statusStrip1->Name = L"statusStrip1";
this->statusStrip1->Size = System::Drawing::Size(319, 22);
this->statusStrip1->TabIndex = 4;
this->statusStrip1->Text = L"statusStrip1";
//
// toolStripStatuslabel1
//
this->toolStripStatuslabel1->Name = L"toolStripStatuslabel1";
this->toolStripStatuslabel1->Size = System::Drawing::Size(0, 17);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(319, 219);
this->Controls->Add(this->statusStrip1);
this->Name = L"Form1";
this->Text = L"16FUSB - com0com Bridge App";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->statusStrip1->ResumeLayout(false);
this->statusStrip1->PerformLayout();
this->ResumeLayout(false);
this->PerformLayout();

}
#pragma endregion

private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
if(this->toolStripStatuslabel1->Text == "") {
Application::Exit();
}
}

private: System::Void serialPort_DataReceived(System::Object^ sender, System::IO::Ports::SerialDataReceivedEventArgs^ e) {

unsigned char outputReport[2];

while(serialPort->BytesToRead){
outputReport[1] = serialPort->ReadByte();
}
outputReport[0] = 0;

usb->writeReport(outputReport);
}

private: System::Void inputThread( Object^ sender, DoWorkEventArgs^ e ) {

unsigned char *inputReport;
array<BYTE> ^pBuffer = gcnew array<BYTE>(1);

while(true) {

inputReport = usb->readReport();
pBuffer[0] = inputReport[1];

serialPort->Write(pBuffer, 0, 1);
}
}

};

}