• Y
  • All List
  • Feedback
    • This Project
    • All Projects
My profile Account settings Log out
  • Project
  • Groups
Loading...
  • Log in
  • Sign up
sbkim / 한국하우톤_ModbusServer star
  • Project homeH
  • CodeC
  • IssueI
  • Pull requestP
  • Review R
  • MilestoneM
  • BoardB
  • Files
  • Commit
  • Branches
한국하우톤_ModbusServerKHSCALE_TPSerialPort.cs
Download as .zip file
File name
Commit message
Commit date
KHSCALE_TP
..
2022-09-02
ModbusTest
..
2022-09-02
.gitattributes
.gitignore 및 .gitattributes를 추가하세요.
2022-06-23
.gitignore
.gitignore 및 .gitattributes를 추가하세요.
2022-06-23
KHModbus.sln
..
2022-09-02
File name
Commit message
Commit date
Properties
..
2022-09-02
App.config
..
2022-09-02
ComCasCi501.cs
..
2022-09-02
ComFs1020c.cs
..
2022-09-02
ComIcs9000.cs
..
2022-09-02
FormLogin.Designer.cs
..
2022-09-02
FormLogin.cs
..
2022-09-02
FormLogin.resx
..
2022-09-02
FormScale.Designer.cs
..
2022-09-02
FormScale.cs
..
2022-09-02
FormScale.resx
..
2022-09-02
KHSCALE_TP.csproj
..
2022-09-02
MainForm.Designer.cs
..
2022-09-02
MainForm.cs
..
2022-09-02
MainForm.resx
..
2022-09-02
Program.cs
..
2022-09-02
SerialBase.cs
..
2022-09-02
SerialPort.cs
..
2022-09-02
U3Config.cs
..
2022-09-02
U3Database.cs
..
2022-09-02
U3Util.cs
..
2022-09-02
UcInsertWork.Designer.cs
..
2022-09-02
UcInsertWork.cs
..
2022-09-02
UcInsertWork.resx
..
2022-09-02
UcOrderList.Designer.cs
..
2022-09-02
UcOrderList.cs
..
2022-09-02
UcOrderList.resx
..
2022-09-02
UcWork.Designer.cs
..
2022-09-02
UcWork.cs
..
2022-09-02
UcWork.resx
..
2022-09-02
content-window_icon-icons.com_58037.ico
..
2022-09-02
khkang 2022-09-02 a5eb3ed .. UNIX
Raw Open in browser Change history
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO.Ports; namespace KHSCALE_TP { public class SerialPort { public bool m_IsConn = false; public string m_strPortMsg = ""; public int m_recvCount = 0; private System.IO.Ports.SerialPort port = null; private List<byte> m_buffer = new List<byte>(); public void AddBuffer(byte[] data) { lock (this) { m_buffer.AddRange(data); } } public List<byte> GetBuffer(bool bClear) { List<byte> buffer = new List<byte>(); lock (this) { buffer.AddRange(m_buffer); if (bClear) m_buffer.Clear(); } return buffer; } public void ClearBuffer() { lock (this) { m_buffer.Clear(); } } public int GetBufferSize() { lock (this) { return m_buffer.Count; } } public void Close() { try { if (port != null) port.Close(); } catch { } port = null; m_IsConn = false; } public void Init(string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits) { try { port = new System.IO.Ports.SerialPort(portName, baudRate, parity, dataBits, stopBits); //port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived); port.ReadBufferSize = 1024 * 1024; port.Open(); m_IsConn = true; m_strPortMsg = portName + " 포트를 정상적으로 오픈하였습니다."; } catch (Exception ex) { m_strPortMsg = portName + " " + ex.Message; m_IsConn = false; } } public void Read() { m_strPortMsg = ""; try { port.ReadTimeout = 1; while (port.BytesToRead > 0) { int bytesToRead = port.BytesToRead; if (bytesToRead > 0) { byte[] bytesBuffer = new byte[bytesToRead]; int readBytes = port.Read(bytesBuffer, 0, bytesToRead); if (readBytes > 0) { m_recvCount++; byte[] bytesBuffer2 = new byte[readBytes]; Array.Copy(bytesBuffer, bytesBuffer2, readBytes); AddBuffer(bytesBuffer2); } } if (GetBufferSize() >= 256) { port.DiscardInBuffer(); return; } System.Threading.Thread.Sleep(1); } } catch (Exception ex) { m_strPortMsg = ex.Message; Close(); } } private void port_DataReceived(object sender, SerialDataReceivedEventArgs e) { //Console.WriteLine(port.ReadExisting()); } } }

          
        
    
    
Copyright Yona authors & © NAVER Corp. Supported by D2 Program

or
Sign in with github login with Google Sign in with Google
Reset password | Sign up