c# SerialPort

c# SerialPort

Tags
C#
Published
Author

RS232

  • ํ”„๋กœํ† ์ฝœ์€ ์ง๋ ฌํ†ต์‹ ์„ ์œ„ํ•œ ๊ทœ๊ฒฉ์œผ๋กœ์„œ ํ•œ๋ฒˆ์— ํ•˜๋‚˜์˜ ๋น„ํŠธ์”ฉ๋งŒ ์ „์†ก
  • ํ†ต์‹ ๊ฑฐ๋ฆฌ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ 15m์ด์ง€๋งŒ ํ†ต์‹  ์†๋„๊ฐ€ ๋†’์•„์งˆ์ˆ˜๋ก ๊ฑฐ๋ฆฌ๊ฐ€ ์งง์•„์ง€๊ณ  ๋‚ฎ์„์ˆ˜๋ก ์ข€ ๋” ๋ฉ€๋ฆฌ๊นŒ์ง€ ๊ฐ‘๋‹ˆ๋‹ค.
  • ์‹œ๋ฆฌ์–ผํ†ต์‹ ์€ ๋…ธ์ด์ฆˆ๊ฐ€ ๋งŽ์ด ํƒ€๊ธฐ๋•Œ๋ฌธ์— ์“ฐ๋ ˆ๊ธฐ ๊ฐ’์ด ๋งŽ์Œ
    • โ‡’ ์‹œ์ž‘๋ฌธ์ž/์ข…๋ฃŒ๋ฌธ์ž๋ฅผ ๋„ฃ๋Š”๊ฒŒ ์ข‹๋‹ค
  • ์‹œ์ž‘๋ฌธ์ž๋Š” hex์ฝ”๋“œ 0x02
  • ์ข…๋ฃŒ๋ฌธ์ž๋Š” hex์ฝ”๋“œ 0x0D, 0x0A

portName์€ ๋ฉ”๋ชจ์žฅ๊ฐ™์€๊ฑฐ์—์„œ ์ฝ์–ด๋“œ๋ฆฌ๊ฒŒํ•˜๊ณ  ๋ฉ”๋ชจ์žฅ์— ๋‚ด์šฉ์„ ๋ฐ”๊พธ๋Š” ํ˜•์‹

private void ReadyToSerialPort() { try { // ReadAllText()ํ•จ์ˆ˜ - ์ค„๋ฐ”๊ฟˆ์„ ํ†ตํ•ด์„œ ์ฒซ์ค„์€ portname ๋‹ค์Œ์ค„์€ BaudRate์„ ์ฝ๊ฒŒํ•˜๋Š” ์ฝ”๋“œ string str = File.ReadAllText(@"setting.txt").Replace("\n", string.Empty); string[] settings = str.Spilt(','); if(settings.Count() == 2) { serialPort1.PortName = settings[0]; // COM1, COM2 serailPort1.BaudRate = Conver.ToInt32(settings[1]); // ์ƒ๋Œ€๋ฐฉ ํฌํŠธ } } catch(Exception ex) { MessageBox.Show(ex.ToString()); } }
์ œ์–ดํŒ์—์„œ๋Š” ์žฅ์น˜๊ด€๋ฆฌ์ž์—์„œ ๊ผญ ํฌํŠธ๋ฅผ ๊ณ ์ •

๋…ธ์ด์ฆˆ๋ฅผ ํ†ตํ•œ ์“ฐ๋ ˆ๊ธฐ๊ฐ’ ์ฒ˜๋ฆฌ

private Object thisLock = new Object(); const char _02 = (char)0x02; const char _0D = (char)0x0D; const char _0A = (char))0x0A; string inStream = "";
  • ๋™์‹œ์— ๋“ค์–ด์˜ค๋Š” ๋ฐ์ดํ„ฐ๋“ค์ด ์ถฉ๋Œ์ด ์•ˆ๋‚˜๊ฒŒ Object๋กœ lock์„ ๊ฑธ์–ด์คŒ - thisLock
  • ์‹œ๋ฆฌ์–ผ ๋ฐ์ดํ„ฐ ๋‚ด์šฉ์ด ์‹œ์ž‘ - 0x02(STX) ๋ฌธ์ž
  • ์‹œ๋ฆฌ์–ผ ๋ฐ์ดํ„ฐ ๋‚ด์šฉ์ด ๋ - 0x0D, 0x0A
  • ๋‚ด์šฉ์ด ๋ง‰ ๋Š์–ด์ ธ์„œ ๋“ค์–ด์™€๋„ ์กฐํ•ฉํ•ด์„œ ์ œ๋Œ€๋กœ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  inStream
๐Ÿ’ก
PLC(์ž๋™์ œ์–ด๋ฐ๋ชจ๋‹ˆํ„ฐ๋ง์— ์‚ฌ์šฉํ•˜๋Š” ์ œ์–ด์žฅ์น˜) ์™€ ์‹œ๋ฆฌ์–ผ ํ†ต์‹ ์„ ํ•˜๊ฒŒ ๋˜๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ถ™์ž„, PC์ชฝ๋งŒ ์ฒ˜๋ฆฌํ•ด์•ผํ•จ

์—ฐ๊ฒฐํ•˜๊ธฐ

์‹œ๋ฆฌ์–ผํฌํŠธ๋Š” ์„œ๋กœ ์—ด๋ ค์žˆ๋Š” ํฌํŠธ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ด์ฃผ๋Š” ํ˜•์‹
์ฆ‰, ์—ฐ๊ฒฐํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ์—ด์–ด๋†“๋Š”๊ฒƒ
if(!serialPort1.IsOpen) // ์—ด๋ ค์žˆ๋Š”์ง€ ์ฒดํฌ { try { serialPort1.Open(); } catch(Exception ex) { MessageBox.Show(ex.ToString()); } }

๋ฐ์ดํ„ฐ ๋ฐ›๊ธฐ(Data Received)

C# ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ๋Š” ์•„๋ฌด๋ž˜๋„ event-driven ๋ฐฉ์‹์ด๊ธฐ ๋Œ€๋ฌธ์— ์šด์˜์ฒด์ œ ๋”ด์—์„œ ์ด๋ฒคํŠธ๋กœ ํฌํŠธ ์ด๋ฒคํŠธ๋ฅผ ๊ฐ์ง€ํ•ด์„œ ์ฝ”๋“œ๋กœ ๋„˜๊ฒจ์ค„์ˆ˜ ์žˆ๋‹ค.
  • ๋ฒˆ๊ฐœ๋ˆ„๋ฅด๊ณ  DataReceived ๋”๋ธ”ํด๋ฆญ
private void SerialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { try { lock(thisLock) { this.Invoke(new EventHandler(SerialPort_DataReceived_InMainThread)); } } catch{} }
  • ์ด๋ ‡๊ฒŒ ํ•˜์ง€ ์•Š์œผ๋ฉด GUI ์ชฝ์ผ์•„ ๋ฐ์ดํ„ฐ ์ฝ๋Š”์ชฝ์ด๋ž‘ ์ถฉ๋Œ์„ ๋ฐœ์ƒํ•ด์„œ ์ œ๋Œ€๋กœ ์ฒ˜๋ฆฌ ๋ชปํ•˜๊ฒŒ ๋จ
  • Invoke๋ฅผ ํ†ตํ•ด์„œ ๋‹ค์‹œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๋Š”๊ฑธ ์‹คํ–‰

๋ฐ์ดํ„ฐ ์ฝ๊ธฐ

private void SerailPort_DataReceived_InMainTread(object s, EventArgs e) { string indata = serialPort1.ReadExisting(); // ํฌํŠธ๋‹จ์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ์กฐ๋ฆฌ ๊ฐ€์ ธ์˜ด string _indata = indata; char[] arr = indata.ToCharArray(); if(arr.Length <= 0) return; // ์“ฐ๋ ˆ๊ธฐ๊ฐ’์—์„œ ๋ฐ์ดํ„ฐ๋งŒ ๊ฐ€์ ธ์˜ค๋Š”๋ฐฉ๋ฒ• if(arr[0] == _02) // ์‹œ์ž‘ inStream = indata; else inStream += indata; string[] splited = inStream.Split(_02); foreach(string sp in splited) // ๋ฐ์ดํ„ฐ์ €์žฅ { char[] arr2 = sp.ToCharArray(); if(arr2.Length <= 0) continue; int etxIndex = Array.IndexOf(arr2, _0A); if(etxIndex <= 0) continue; etxIndex = Array.IndexOf(arr2, _0D); if(etxIndex <= 0) continue; var arr3 = arr2.SubArray(0, etxIndex); string cmd = new string(arr3); SerialCommand(cmd); } // ๋ } public void SerialCommand(string cmd) { // ๋ฐ›๋Š”๊ตฌ๋ฌธ ์ฒ˜๋ฆฌ (๋ฐ›์€๋‚ด์šฉ์„ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ• ์ง€๋Š” ์—ฌ๊ธฐ์— ๊ตฌํ˜„) } // ๋ฐ์ดํ„ฐ๋ฅผ ์กฐ๋ฆฝํ•˜๋Š” ์ฝ”๋“œ public static T[] SubArray<T>(this T[] data, int index, int length) { T[] result = new T[length]; Array.Copy(data, index, result, 0, length); return result; }

๋ฐ์ดํ„ฐ๋ณด๋‚ด๊ธฐ

๋ฐ˜๋Œ€๋กœ ์•ž์— 0x02 ๋ถ™์ด๊ณ  ๋’ค์— 0x0D, 0x0A๋ถ™์—ฌ์„œ ๋ณด๋‚ด๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค.
public void SendSerialData(string data) { string strOutputData = _02 + data + 0D + _0A; if(serialPort1.IsOpen) { serialPort1.Write(strOutputData); } else { try { serialPort1.Open(); serialProt1.Write(strOutputData); } catch(Exception ex) { MessageBox.Show(ex.ToString()); } } }
ย