//Thread Read Port RS232. private static void Read() { MITProtocol mp = new MITProtocol(); TRINP.thread = true; do { byte[] RecBuf = new byte[bcom.getBufSize()]; int contBytes = 0; _continue = true; while (_continue) { try { //tiempo de espera para lectura del puerto Thread.Sleep(1000); //Bytes leidos int lenbuff = _serialPort.BytesToRead; if (lenbuff > 0) { byte[] buffer = new byte[lenbuff]; _serialPort.Read(buffer, 0, lenbuff); System.Buffer.BlockCopy(buffer, 0, RecBuf, contBytes, lenbuff); contBytes = contBytes + lenbuff; } else { _continue = false; } } catch (TimeoutException) { _continue = false; } } TRINP.BRecBuf = RecBuf; bcom.setRecBuf(System.Text.Encoding.UTF8.GetString(RecBuf)); bcom.setLenRecBuf(contBytes); if (mp.ProcesaRsp() && (bcom.getLenRecBuf() > 0)) { bcom.setRecBuf(""); bcom.setLenRecBuf(0); bcom.setstatusRead(true); } bcom.setTimeOut(bcom.getTimeOut() - 1); if (TRINP.cancelop) { bcom.setTimeOut(0); TRINP.cancelop = false; } } while (bcom.getTimeOut() > 0); TRINP.thread = false; }
public bool _SendMsg(byte[] sendbuf, int lensendbuf) { MITProtocol mprot = new MITProtocol(); bcom.setSendBuf(sendbuf); bcom.setLenSendBuf(lensendbuf); if (bcom.getTypeCom().Equals("1")) //RS232 { //COM / USB / RS232 if (SendRS232()) { status = true; } //ETH / WIFI / LAN else if (SendIP()) { } } return(status); }
//Read Port RS232. public static void ReadPort() { MITProtocol mp = new MITProtocol(); Thread readThread; try { bcom.setstatusRead(false); readThread = new Thread(new ThreadStart(Read)); readThread.IsBackground = true; readThread.Start(); while (readThread.IsAlive) { Application.DoEvents(); } readThread.Abort(); readThread.Join(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }