C#ã§ã²ã¨ãã·ãªã¢ã«éä¿¡ãããæã®ã¡ã¢
1. com0comã®ã¤ã³ã¹ãã¼ã«
â2ã¤ã®ä»®æ³COMãã¼ããç¨æããä¸æ¹ããä»æ¹ã«éä¿¡ãã¦ããããã«è¦ããããäºãå¯è½ã
-
- http://sourceforge.net/projects/com0com/ ããzipããã¦ã³ãã¼ãããé©å½ãªãã£ã¬ã¯ããªã«è§£åã
- setup.exeãéãããnext -> I Agree -> installã
- ãã¼ãã¦ã¨ã¢æ¤åºã¦ã£ã¶ã¼ããæ¥ç¶ããªã -> èªåã¤ã³ã¹ãã¼ã« -> å®äºã(2ã»ãã)
- setup.exe: ãfinishã
- ããã¤ã¹ããã¼ã¸ã£ããcom0comãèªèããã¦ãããã¨ã確èªã
2. ãã¼ãåã®å¤æ´
âåæè¨å®ã§ã¯ãã¼ãåã CNCA0 㨠CNCB0 ã«ãªã£ã¦ããã®ã§ãããã COM8 㨠COM9 ã«å¤æ´ã
-
- ã¹ã¿ã¼ã -> ãã¹ã¦ã®ããã°ã©ã -> com0com -> SetupCommandPrompt
- ãchange CNCA0 PortName=COM8ã
- ãchange CNCB0 PortName=COM9ãã«ã¦ååå¤æ´ã
- ãlistãã«ã¦ãã¼ãåã確èª
以ä¸ã®æé ã¯Win7ãªããvistaã®å ´åãã¹ãã¢ã¼ãã§ã®èµ·åãå¿ è¦ã«ãªã£ããã¨è²ã é¢åãªããã§ãã
3. C#ã®ã½ã¼ã¹ã®ç¨æ
éä¿¡ç¨
using System; using System.Collections.Generic; using System.Linq; using System.IO.Ports; using System.Text; namespace ComWrite { class Program { static void Main(string[] args) { while (true) { Console.Write("sending message -> "); string req = Console.ReadLine(); SerialPort port = new SerialPort("COM8", 9600, Parity.None, 8, StopBits.One); try { port.Open(); // ããã¼å¶å¾¡ã¯ãã¾ããã port.DtrEnable = false; port.RtsEnable = false; port.WriteLine(req); Console.WriteLine("send ã :" + req); Console.WriteLine("received:" + port.ReadLine()); } catch (Exception e) { Console.WriteLine("Unexpected exception: ", e.ToString()); } port.Close(); port.Dispose(); } } } }
åä¿¡ç¨
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO.Ports; namespace ComRead { class Program { static void Main(string[] args) { SerialPort port = new SerialPort("COM9", 9600, Parity.None, 8, StopBits.One); port.DataReceived += new SerialDataReceivedEventHandler(SerialPort_DataReceived); try { port.Open(); port.DtrEnable = false; port.RtsEnable = false; } catch (Exception e) { Console.WriteLine("Unexpected exception: ", e.ToString()); } Console.ReadLine(); port.Close(); port.Dispose(); } private static void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { SerialPort port = (SerialPort)sender; byte[] buf = new byte[1024]; int len = port.Read(buf, 0, 1024); string s = Encoding.GetEncoding("Shift_JIS").GetString(buf, 0, len); port.WriteLine(s); Console.Write("received: " + s); } } }
æå¤ã¨ã·ã³ãã«ã
åãã¦ã®C#ã ã£ããã©ããªãã¨ããªããã®ã§ãã
åä½ç°å¢
Windows XP pro sp3
Microsoft Visual Studio 2008 Professional
åè
http://homepage2.nifty.com/nonnon/Link/Null-Modem-Win7.html
http://msdn.microsoft.com/ja-jp/library/cc825644.aspx
浅草ギ研 Visual C# 2005 のシリアル通信機能を使ってみる