예제 #1
0
 /// <summary>
 ///     Create a new SerialTextFile and attach the instance to the specfied serial port.
 /// </summary>
 /// <param name="port">Serial port name.</param>
 /// <param name="baudRate">Baud rate.</param>
 /// <param name="parity">Parity.</param>
 /// <param name="dataBits">Data bits.</param>
 /// <param name="stopBits">Stop bits.</param>
 /// <param name="endOfLine">Text indicating the end of a line of text.</param>
 public SerialTextFile(IIODevice device, SerialPortName port, int baudRate, Parity parity, int dataBits, StopBits stopBits,
                       string endOfLine)
 {
     serialPort = device.CreateSerialPort(port, baudRate, dataBits, parity, stopBits);
     LINE_END   = endOfLine;
     serialPort.DataReceived += SerialPortDataReceived;
 }
예제 #2
0
        public Vc0706(IIODevice device, SerialPortName portName, int baud)
        {
            serialPort = device.CreateSerialPort(portName, baud);

            /*serialPort = device.CreateSerialMessagePort(
             *          portName: portName,
             *          suffixDelimiter: Encoding.ASCII.GetBytes("\r\n"),
             *          baudRate: baud,
             *          preserveDelimiter: true,
             *          readBufferSize: 512);*/
            serialPort.Open();

            switch (baud)
            {
            case 9600:
            default:
                SetBaud9600();
                break;

            case 19200:
                SetBaud19200();
                break;

            case 38400:
                SetBaud38400();
                break;

            case 57600:
                SetBaud57600();
                break;
            }
        }
예제 #3
0
        public bool OpenSerialPort(SerialPortName portName, SerialBaudrate baudrate, bool byteMode)
        {
            string     portNameAfterConvert = String.Format("COM{0:D}", portName);
            SerialPort tempSerialPort       = new SerialPort(portNameAfterConvert, (int)baudrate);

            CurrentSerialPort = tempSerialPort;
            ByteMode          = byteMode;
            if (!isSerialPortOpen)
            {
                try
                {
                    //尝试打开串口
                    CurrentSerialPort.ReadTimeout     = 8000; //串口读超过8秒
                    CurrentSerialPort.WriteTimeout    = 8000; //串口写超时8秒
                    CurrentSerialPort.ReadBufferSize  = 1024; //数据读缓存
                    CurrentSerialPort.WriteBufferSize = 1024; //数据写缓存
                    CurrentSerialPort.Parity          = Parity.None;
                    CurrentSerialPort.DataBits        = 8;
                    CurrentSerialPort.StopBits        = StopBits.One;
                    CurrentSerialPort.DataReceived   += CurrentSerialPortDataReceived;
                    CurrentSerialPort.Open();
                    isBeginReceiving         = true;
                    isSerialPortOpen         = true;
                    SerialPortOpenCompeleted = true;
                }
                catch (Exception e)
                {
                    SetAfterSerialClosedOrLost();
                    throw new Exception("无法打开串口\r\n" + e.Message);
                }
            }

            return(isSerialPortOpen);
        }
예제 #4
0
    /// <summary>
    /// 使用するシリアルポートを設定する
    /// <para>親子関係になっている前提</para>
    /// </summary>
    void SetUpSerialPort()
    {
        PortList = new List <serial_unit>();

        int         no = 0;
        serial_unit _unit;

        //子供を探してリストを追加する
        foreach (Transform child in transform)
        {
            //アクティブな子供だけを追加する
            if (!child.gameObject.activeSelf)
            {
                continue;
            }

            SerialPortName _sp = child.GetComponent <SerialPortName>();
            if (_sp != null)
            {
                _unit = new serial_unit();
                _unit.portName_def      = _sp.portName_def;
                _unit.UserName          = _sp.UserName;
                _unit.isAutoSetPortName = _sp.isAutoSetPortName;
                _unit.portName          = _sp.portName;
                _unit.baudRate          = _sp.baudRate;
                PortList.Add(_unit);

                _sp.SerialListNo = no++;
            }
        }
    }
예제 #5
0
        public IEnumerable <SerialStatus> OpenSerialPort([FromBody] SerialPortName data)
        {
            Console.WriteLine("Open {0} port!", data.PortName);

            //if (Port != null) Port = null;

            //Port = new SerialPortStream();
            if (Global.MyPort.IsOpen)
            {
                Global.MyPort.Close();
            }

            Global.MyPort.DataReceived += new EventHandler <SerialDataReceivedEventArgs>(serialPort_DataReceived);
            Global.RxData          = "";
            Global.MyPort.PortName = data.PortName;
            Global.MyPort.BaudRate = data.BaudRate;

            Global.MyPort.Open();

            return(Enumerable.Repeat(new SerialStatus
            {
                Connected = Global.MyPort.IsOpen && Global.MyPort.CanRead && Global.MyPort.CanWrite,
                StrError = data.PortName + "/ " + (Global.MyPort.CanWrite?"Can Write":"Not Write") + " / " + (Global.MyPort.CanRead ? "Can Read" : "Not Read"),
                StrRxData = "",
                StrTxData = ""
            }, 1));
        }
예제 #6
0
    private IEnumerator ConnectCoroutine()
    {
        var wait = new WaitForSeconds(0.1f);

        isConnect = false;
        yield return(wait);

        SerialPortName _sp = GetComponent <SerialPortName> ();

        if (_sp == null)
        {
            yield break;
        }
        _serial = SerialHandler.Instance.PortList[_sp.SerialListNo]; //SerialHandlerのリストと紐づく

        //USBの切断
        _serial.Close();
        _serial.OnDataReceived -= OnDataReceived;
        yield return(wait);

        //USBの接続
        joinMsg = string.Empty;
        if (_serial.Open(true))
        {
            _serial.OnDataReceived += OnDataReceived;
            isConnect = true;
        }
        yield break;
    }
        void InitSerial(SerialPortName portName, int baud)
        {
            // instantiate our serial port
            this.classicSerialPort = Device.CreateSerialPort(portName, baud);
            Console.WriteLine("\tCreated");

            // open the serial port
            this.classicSerialPort.Open();
            Console.WriteLine("\tOpened");
        }
예제 #8
0
 /// <summary>
 /// Create an IDxxLA RFID reader
 /// </summary>
 /// <param name="device">Device to use</param>
 /// <param name="serialPortName">Port name to use</param>
 public IDxxLA(IIODevice device, SerialPortName serialPortName) :
     this(device.CreateSerialMessagePort(
              serialPortName,
              suffixDelimiter : new byte[] { EndToken },
              preserveDelimiter : true,
              baudRate : BaudRate,
              dataBits : DataBits
              )
          )
 {
 }
예제 #9
0
        /// <summary>
        ///     Create a new SerialLcd object.
        /// </summary>
        /// <param name="config">TextDisplayConfig object defining the Lcd dimension (null will default to 16x2).</param>
        /// <param name="baudRate">Baud rate to use (default = 9600).</param>
        /// <param name="parity">Parity to use (default is None).</param>
        /// <param name="dataBits">Number of data bits (default is 8 data bits).</param>
        /// <param name="stopBits">Number of stop bits (default is one stop bit).</param>
        public SerialLcd(IIODevice device, SerialPortName port, TextDisplayConfig config = null, int baudRate = 9600,
                         Parity parity = Parity.None, int dataBits = 8, StopBits stopBits = StopBits.One)
        {
            if (config == null)
            {
                // assume a 16x2 Lcd.
                DisplayConfig = new TextDisplayConfig()
                {
                    Height = 2, Width = 16
                };
            }
            else
            {
                DisplayConfig = config;
            }

            comPort = device.CreateSerialPort(port, baudRate, dataBits, parity, stopBits);

            comPort.Open();

            // configure the Lcd controller for the appropriate screen size
            byte lines      = 0;
            byte characters = 0;

            switch (DisplayConfig.Width)
            {
            case 16:
                characters = (byte)LcdDimensions.Characters16Wide;
                break;

            case 20:
                characters = (byte)LcdDimensions.Characters20Wide;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(config.Width), "Display width should be 16 or 20.");
            }
            switch (DisplayConfig.Height)
            {
            case 2:
                lines = (byte)LcdDimensions.Lines2;
                break;

            case 4:
                lines = (byte)LcdDimensions.Lines4;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(config.Height), "Display height should be 2 or 4 lines.");
            }
            Send(new[] { ConfigurationCommandCharacter, characters, ConfigurationCommandCharacter, lines });
            Thread.Sleep(10);
        }
예제 #10
0
 public IEnumerable <SerialStatus> CloseSerialPort([FromBody] SerialPortName data)
 {
     Console.WriteLine("Close {0}.", data.PortName);
     if (Global.MyPort != null)
     {
         Global.MyPort.Close();
     }
     return(Enumerable.Repeat(new SerialStatus
     {
         Connected = !Global.MyPort.IsOpen,
         StrError = "Closed " + data.PortName,
         StrRxData = "",
         StrTxData = ""
     }, 1));
 }
    private IEnumerator ConnectCoroutine()
    {
        flg_TimeOver = false;
        timeOver     = 0.0f;

        Now_Mode = MODE.TURMINAL;
        Bef_Mode = MODE.TURMINAL;
        var wait = new WaitForSeconds(0.1f);

        yield return(null);

        if (NowCoroutine != null)
        {
            StopCoroutine(NowCoroutine);
        }
        yield return(null);

        SerialPortName _sp = GetComponent <SerialPortName>();

        if (_sp == null)
        {
            yield break;
        }
        _serial = SerialHandler.Instance.PortList[_sp.SerialListNo];   //SerialHandlerのリストと紐づく

        //USBの切断
        _serial.Close();
        yield return(wait);


        _serial.OnDataReceived -= OnDataRead_Init;
        _serial.OnDataReceived -= OnDataRead_Receive_Interval;
        _serial.OnDataReceived -= OnDataRead_Communication;
        yield return(wait);


        //USBの接続
        _serial.Open(true);
        yield return(wait);


        Now_Mode = MODE.NONE;
        Bef_Mode = MODE.TURMINAL;


        yield break;
    }
        public MeadowApp()
        {
            serialPortName = Device.SerialPortNames.Com1;

            Console.WriteLine("Get delimiter");
            // convert for later.
            delimiterBytes = Encoding.ASCII.GetBytes(delimiterString);

            Console.WriteLine("SerialMessagePort_Test");
            Console.WriteLine($"Using '{serialPortName.FriendlyName}'...");
            Console.WriteLine($"delimiter:{delimiterString}");

            //TestDoubleMessageWithSuffix();

            TestSuffixDelimiter();
            //TestPrefixDelimiter();

            //TestSuffixDelimeterAndBufferLengthForNulls();
        }
예제 #13
0
 public Hc06(IIODevice device, SerialPortName port, int baudRate = 921600)
 {
     _signal             = new AutoResetEvent(false);
     _port               = device.CreateSerialPort(port, baudRate, 8, Parity.None, StopBits.One);
     _port.DataReceived += (s, e) =>
     {
         //Console.WriteLine("DataReceived - {0}!", _port.BytesToRead);
         if (_internalOp)
         {
             _signal.Set();
             _internalOp = false;
         }
         else
         {
             DataReceived?.Invoke(this, new EventArgs());
         }
     };
     _internalOp = false;
 }
예제 #14
0
 public ISerialPort CreateSerialPort(SerialPortName portName, int baudRate, int dataBits = 8, Parity parity = Parity.None, StopBits stopBits = StopBits.One, int readBufferSize = 4096)
 {
     throw new NotImplementedException();
 }
예제 #15
0
 public Mt3339(IIODevice device, SerialPortName serialPortName)
     : this(device.CreateSerialMessagePort(
                serialPortName, suffixDelimiter : Encoding.ASCII.GetBytes("\r\n"),
                preserveDelimiter : true, readBufferSize : 512))
 {
 }
예제 #16
0
 public Yx5300(IIODevice device, SerialPortName serialPortName)
     : this(device.CreateSerialPort(
                serialPortName))
 {
 }
예제 #17
0
 public ISerialMessagePort CreateSerialMessagePort(SerialPortName portName, byte[] prefixDelimiter, bool preserveDelimiter, int messageLength, int baudRate = 9600, int dataBits = 8, Parity parity = Parity.None, StopBits stopBits = StopBits.One, int readBufferSize = 512)
 {
     throw new NotImplementedException();
 }
예제 #18
0
        public Vc0706(IIODevice device, SerialPortName serialPortName, int baudRate)
        {
            serialPort = device.CreateSerialPort(serialPortName, baudRate);

            _vc0706 = new Vc0706Core();
        }
예제 #19
0
 public void SetPortName(SerialPortName serialPortName)
 {
 }
예제 #20
0
 public Mb10x0(IIODevice device, SerialPortName portName)
 {
     serialPort = device.CreateSerialPort(portName, Baud);
     serialPort.Open();
 }
예제 #21
0
 /// <summary>
 ///     Create a new NMEA GPS object and attach to the specified serial port.
 /// </summary>
 /// <param name="port">Serial port attached to the GPS.</param>
 /// <param name="baudRate">Baud rate.</param>
 /// <param name="parity">Parity.</param>
 /// <param name="dataBits">Number of data bits.</param>
 /// <param name="stopBits">Number of stop bits.</param>
 public NMEA(IIODevice device, SerialPortName port, int baudRate, Parity parity, int dataBits, StopBits stopBits)
 {
     gps = new SerialTextFile(device, port, baudRate, parity, dataBits, stopBits, "\r\n");
     gps.OnLineReceived += GpsOnLineReceived;
 }