画像ファイル
画像ファイル RS-232C 送信モジュール
RS-232C関係
1RS-232Cの基礎
2RS-232Cの何が変わった..
3SerialPortクラス
4Unicode(ユニコード)の壁
5マルチスレッドの壁
6RS-232C サンプルコード
7RS-232CのHEXモニタ
8RS-232C 送信モジュール
9RS-232Cのループテスト
10RS-232Cのピンチェンジ..

VB.NET C#全般
1羊の皮を着た狼 VB.NET
2Form1、Form2の相互参照
3Form1、Form2の相互参照 2
4VB.NET C# データ型の基本
5VB.NET C# 文字列
6VB.NET タイマー精度
7BackgroundWorkerの魅力1..
8BackgroundWorkerの魅力2..
9VB6のタイマー
10コントロールの配列をインデクサ..
11コントロールの配列はジェネリク..
12インデクサ(C#、VB.NET)
13インデクサでBit操作
14Unicode 入門
15デリゲート入門
16マルチスレッド入門
17イベント入門
18デリゲートとイベント
18インターフェースの基本

RichTextBox関係
1RichTextBoxの不思議
2テキスト色付け高速化計画
3VB.NET RichTextBox1
4VB.NET RichTextBox 2

Socket通信
1C#、VB2005 でSocket通信
2サーバー 複数接続

プロセス間通信
1プロセス間通信(送信側)
2プロセス間通信(受信側)


質問、意見はこちらに
画像ファイル


RS-232C 送信モジュールの作成
RS-232Cの送信モジュールを作成します。
仕様はごく簡単ですが
1、文字コードとして Shift-jis、Unicode(utf-16)、utf-8が選べること。
2、Hexの文字が指定できること。
の2点です。
Hexの文字は<hex></hex>で囲むことにします。
すなわち、
<hex>61,62,63,2a,2b</hex>と指定すると、
0x61(&H61)、0x62(&H62)、0x63(&H63)、0x2A(&H2A)、0x2B(&H2B)が送られる事とします。
<hex></hex>を指定しないと文字列を送信します。
文字列の中に<crlf>、<cr>、<lf>を指定すると、(char)13 + (char)chr(10)(Chr(13)+cChr(10))
(char)13(Chr(13))、(char)10(Chr(10))に変換して送信します。
今回の場合はこれらの文字のエスケープは考えないことにします。

  下の画像はHexのデータを送信するところです。

画像ファイル


C#のコード
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace rs232cauto
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//セリアルポートをOpenする
serialPort1.Open();

}

//送信ボタン押下
private void button1_Click(object sender, EventArgs e)
{
//Hex送信文字を検出した場合
if (textBox1.Text.Trim().StartsWith("<hex>") &&
textBox1.Text.Trim().EndsWith("</hex>"))
{
//前後の空白を削除
string sData = textBox1.Text.Trim();

//制御文字を削除
sData = sData.Replace("<hex>", "");
sData = sData.Replace("</hex>", "");

//Hexデータ送信プロシジャーの呼び出し
sendHexData(sData);

}
else
//テキスト送信のプロシジャーの呼び出し
sendAscData(textBox1.Text);

}

//Hexデータ送信
private void sendHexData(string sData)
{
//データを分ける
string[] strData = sData.Split(',');
byte[] byteData= new byte[strData.Length ];

//データを変換してByte配列に入れる
for (int i = 0; i < strData.Length; i++)
byteData[i] = (byte)Convert.ToInt32(strData[i],16);

//送信
serialPort1.Write(byteData, 0, byteData.Length);
}

//テキスト送信モード
private void sendAscData(string sData)
{
//改行マークが有れば置き換える
sData = sData.Replace("<crlf>", "\r\n");
sData = sData.Replace("<cr>", "\r");
sData = sData.Replace("<lf>", "\n");

byte[] byteData;

//エンコードクラスの宣言
Encoding encStr;
if (radioButton1.Checked)
{
//shift-jis
encStr=Encoding.GetEncoding("shift-jis");
}
else if (radioButton2.Checked)
{
//utf-16
encStr = Encoding.GetEncoding("utf-16");
}
else
{
//utf-8"
encStr = Encoding.GetEncoding("utf-8");
}
//バイト配列に入れる
byteData = encStr.GetBytes(sData);

//送信
serialPort1.Write(byteData, 0, byteData.Length);
}
}
}


VB2005のコード
Imports System.Text
Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort1.Open()
End Sub
Private Sub Button1_Click(ByVal _
sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'//Hex送信文字を検出した場合
If TextBox1.Text.Trim().StartsWith("<hex>") And _
TextBox1.Text.Trim().EndsWith("</hex>") Then

'//前後の空白を削除
Dim sData As String = TextBox1.Text.Trim()

'//制御文字を削除
sData = sData.Replace("<hex>", "")
sData = sData.Replace("</hex>", "")

'//Hexデータ送信プロシジャーの呼び出し
sendHexData(sData)
Else
'//テキスト送信のプロシジャーの呼び出し
sendAscData(TextBox1.Text)
End If
End Sub

'//Hexデータ送信
Private Sub sendHexData(ByVal sData As String)
'//データを分ける
Dim strData() As String = sData.Split(",")
Dim byteData(strData.Length - 1) As Byte
'//データを変換してByte配列に入れる
For i As Integer = 0 To strData.Length - 1
byteData(i) = CByte(Convert.ToInt32(strData(i), 16))
Next
'//送信
SerialPort1.Write(byteData, 0, byteData.Length)
End Sub

'//テキスト送信モード
Private Sub sendAscData(ByVal sData As String)
'//改行マークが有れば置き換える
sData = sData.Replace("<crlf>", Chr(13) & Chr(10))
sData = sData.Replace("<cr>", Chr(13))
sData = sData.Replace("<lf>", Chr(10))

Dim byteData() As Byte

'//エンコードクラスの宣言
Dim encStr As Encoding
If RadioButton1.Checked Then
'//shift-jis
encStr = Encoding.GetEncoding("shift-jis")
ElseIf (RadioButton2.Checked) Then
'//utf-16
encStr = Encoding.GetEncoding("utf-16")
Else
'//utf-8"
encStr = Encoding.GetEncoding("utf-8")
End If
'//バイト配列に入れる
byteData = encStr.GetBytes(sData)

'//送信
SerialPort1.Write(byteData, 0, byteData.Length)
End Sub

End Class

画像ファイル