Ejemplo n.º 1
0
 private void chkComm_CheckedChanged(object sender, EventArgs e)
 {
     if (chkComm.Checked)
     {
         bool success = SPort.OpenPorts(serialPort, Convert.ToInt32(txtPortNum.Text));
         if (!success)
         {
             MessageBox.Show("시리얼포트를 열지 못했습니다", "오류");
         }
     }
     else
     {
         SPort.ClosePorts(serialPort);
     }
 }
Ejemplo n.º 2
0
        //===========================================================
        //  통신에 성공하면 true 리턴
        //===========================================================
        public static bool RecieveText(SerialPort serialPort, ref string answer)
        {
            // 수신버퍼청소
            string dum = SPort.Read(serialPort);

            // 명령송신
            string st = SPort.sSTX() + "AS" + SPort.sETX();

            SPort.Send(serialPort, st);

            // 송신시간기록
            DateTime stime = DateTime.Now;

            // 수신대기
            int    idx1, idx2;
            bool   success = false;
            string rbuff   = "";

            while (true)
            {
                // timeout 검사
                double dtime = Util.TimeInSeconds(stime);
                if (dtime > 0.1)
                {
                    return(false);
                }

                // 수신버퍼검사
                rbuff += SPort.Read(serialPort);

                idx1 = rbuff.IndexOf(SPort.sACK());
                idx2 = rbuff.IndexOf(SPort.sETX());

                if (idx1 >= 0 && idx2 - idx1 >= 4)
                {
                    if (rbuff.Substring(idx1 + 1, 2) == "AS")
                    {
                        // 한 개의 block 찾음
                        answer  = rbuff.Substring(idx1 + 3, idx2 - idx1 - 3);
                        success = true;
                        break;
                    }
                }
            }
            return(success);
        }
Ejemplo n.º 3
0
        public static bool SendText(SerialPort serialPort, string talk)
        {
            // 수신버퍼청소
            string dum = SPort.Read(serialPort);

            // 명령송신
            string st = SPort.sSTX() + "TX" + talk + SPort.sETX();

            SPort.Send(serialPort, st);

            // 송신시간기록
            DateTime stime = DateTime.Now;

            // 수신대기
            int    idx1, idx2;
            bool   success = false;
            string rbuff   = "";

            while (true)
            {
                // timeout 검사
                double dtime = Util.TimeInSeconds(stime);
                if (dtime > 0.2)
                {
                    return(false);
                }

                // 수신버퍼검사
                rbuff += SPort.Read(serialPort);

                idx1 = rbuff.IndexOf(SPort.sACK());
                idx2 = rbuff.IndexOf(SPort.sETX());

                if (idx1 >= 0 && idx2 - idx1 == 3)
                {
                    if (rbuff.Substring(idx1 + 1, 2) == "TX")
                    {
                        success = true;
                        break;
                    }
                }
            }
            return(success);
        }