public RedparkStream(int baud, ParityType parity,StopBitsType stopbits) { RscMgr = new RscMgr(); RscMgr.Baud = baud; RscMgr.SetParity(parity); RscMgr.SetStopBits(stopbits); }
/// <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(string port, int baudRate, ParityType parity, int dataBits, NumberOfStopBits stopBits, string endOfLine) { _serialPort = new SerialPort(port, baudRate, parity, dataBits, stopBits); _endOfLine = endOfLine; _serialPort.DataReceived += _serialPort_DataReceived; }
public RedparkStream(int baud, ParityType parity, StopBitsType stopbits) { RscMgr = new RscMgr(); RscMgr.Baud = baud; RscMgr.SetParity(parity); RscMgr.SetStopBits(stopbits); }
public double ReadRegRTU2(byte slaveAddress, long Address, int nTimeout = 1000) { try { Byte[] recvBytes = new Byte[1024]; int recvSize = 0; byte[] CMD = new byte[8]; CMD[0] = slaveAddress; CMD[1] = 0X03; CMD[2] = (byte)((Address & 0X00FF00) >> 8); CMD[3] = (byte)((Address & 0X00FF) >> 0); CMD[4] = 0; CMD[5] = 0X02; int VALCRC = ParityType.CRC16(CMD, 6); CMD[6] = (byte)(VALCRC & 0X00FF); CMD[7] = (byte)((VALCRC & 0XFF00) >> 8); _serialPort.DiscardInBuffer(); _serialPort.Write(CMD, 0, CMD.Length); Stopwatch stopwatch = new Stopwatch(); stopwatch.Restart(); int data = 0; do { if (stopwatch.ElapsedMilliseconds > nTimeout) { return(double.NaN); } try { Byte d = (Byte)_serialPort.ReadByte(); recvBytes[recvSize++] = d; } catch (Exception ex) { continue; } if (recvSize >= 2 && recvBytes[1] == 0x02) { return(double.NaN); } if (recvSize >= 9 && recvBytes[1] == 0x03) { data |= recvBytes[3] << 24; data |= recvBytes[4] << 16; data |= recvBytes[5] << 8; data |= recvBytes[6]; return(data); } }while (true); } catch (Exception e) { MessageBox.Show("电抓 串口 读取数据异常 " + e.Message, "Err", MessageBoxButtons.OK, MessageBoxIcon.Error); return(double.NaN); } }
/// <summary> /// Create a new SerialLCD object. /// </summary> /// <param name="config">TextDisplayConfig object defining the LCD dimension (null will default to 16x2).</param> /// <param name="port">Com port the display is connected to.</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(TextDisplayConfig config = null, string port = "COM1", int baudRate = 9600, ParityType parity = ParityType.None, int dataBits = 8, NumberOfStopBits stopBits = NumberOfStopBits.One) { if (config == null) { // assume a 16x2 LCD. DisplayConfig = new TextDisplayConfig() { Height = 2, Width = 16 }; } else { DisplayConfig = config; } _comPort = new SerialPort(port, baudRate, parity, dataBits, 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); }
public void WriteRegRTU(byte slaveAddress, long Address, int val) { try { byte[] CMD = new byte[8]; CMD[0] = slaveAddress; CMD[1] = 0X06; CMD[2] = (byte)((Address & 0X00FF00) >> 8); CMD[3] = (byte)((Address & 0X00FF) >> 0); CMD[4] = (byte)((val & 0X00FF00) >> 8); CMD[5] = (byte)((val & 0X00FF) >> 0); int VALCRC = ParityType.CRC16(CMD, 6); CMD[6] = (byte)(VALCRC & 0X00FF); CMD[7] = (byte)((VALCRC & 0XFF00) >> 8); _serialPort.DiscardInBuffer(); _serialPort.Write(CMD, 0, CMD.Length); } catch (Exception e) { MessageBox.Show("电抓 串口 写入数据异常 " + e.Message, "Err", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public void SetLineProperty(BitsType bits, StopBitsType sbit, ParityType parity) { CheckRet(ftdi_set_line_property(ref ftdi, bits, sbit, parity)); }
[DllImport("libftdi1")] internal static extern int ftdi_set_line_property(ref ftdi_context ftdi, BitsType bits, StopBitsType sbit, ParityType parity);
public double ReadRegAscii(byte slaveAddress, long Address, int nTimeout = 1000) { Byte[] recvBytes = new Byte[1024]; int recvSize = 0; byte[] CMD = new byte[17]; CMD[0] = 0; string temp = (slaveAddress).ToString("X2"); //站号 CMD[1] = (byte)(slaveAddress).ToString("X2").ToCharArray()[0]; CMD[2] = (byte)(slaveAddress).ToString("X2").ToCharArray()[1]; //命令 CMD[3] = (byte)'0'; CMD[4] = (byte)'3'; //地址 byte addr = (byte)((Address & 0XFF00) >> 8); temp = ((byte)((Address & 0XFF00) >> 4)).ToString("X2"); CMD[5] = (byte)((byte)((Address & 0XFF00) >> 8)).ToString("X2").ToCharArray()[0]; CMD[6] = (byte)((byte)((Address & 0XFF00) >> 8)).ToString("X2").ToCharArray()[1]; temp = ((byte)((Address & 0X00FF) >> 0)).ToString("X2"); CMD[7] = (byte)((byte)((Address & 0X00FF) >> 0)).ToString("X2").ToCharArray()[0]; CMD[8] = (byte)((byte)((Address & 0X00FF) >> 0)).ToString("X2").ToCharArray()[1]; //读出个数 CMD[9] = (byte)'0'; CMD[10] = (byte)'0'; CMD[11] = (byte)'0'; CMD[12] = (byte)'1'; byte[] bytesAscii = new Byte[6]; bytesAscii[0] = slaveAddress; bytesAscii[1] = 0x03; bytesAscii[2] = (byte)((Address & 0XFF00) >> 8); bytesAscii[3] = (byte)(Address & 0X00FF); bytesAscii[4] = 0x00; bytesAscii[5] = 0x01; int VALLRC = ParityType.LRC(bytesAscii, 6); temp = ((byte)((VALLRC & 0XF0) >> 4)).ToString("X"); CMD[13] = (byte)((byte)((VALLRC & 0XF0) >> 4)).ToString("X").ToCharArray()[0]; CMD[14] = (byte)((byte)(VALLRC & 0X0F)).ToString("X").ToCharArray()[0]; temp = ((byte)((VALLRC & 0X0F) >> 0)).ToString("X"); CMD[15] = 0X0D; CMD[16] = 0X0A; CMD[0] = (byte)':'; _serialPort.DiscardInBuffer(); _serialPort.Write(CMD, 0, CMD.Length); int data = 0; string result = ""; Stopwatch stopwatch = new Stopwatch(); stopwatch.Restart(); do { if (stopwatch.ElapsedMilliseconds > nTimeout) { return(double.NaN); } try { Byte d = (Byte)_serialPort.ReadByte(); recvBytes[recvSize++] = d; } catch (Exception ex) { continue; } if (recvSize >= 5 && recvBytes[3] == (Byte)'8' && recvBytes[3] == (Byte)'3') { return(double.NaN); } if (recvSize >= 15 && recvBytes[13] == 0x0D && recvBytes[14] == 0x0a) { result = Encoding.Default.GetString(recvBytes); result = result.Substring(7, 4); data = Convert.ToInt32(result, 16); return(data); } }while (true); }
/// <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(string port, int baudRate, ParityType parity, int dataBits, NumberOfStopBits stopBits) { _gps = new SerialTextFile(port, baudRate, parity, dataBits, stopBits, "\r\n"); _gps.OnLineReceived += _gps_OnLineReceived; }
[DllImport("libftdi")] internal static extern int ftdi_set_line_property(ref ftdi_context ftdi, BitsType bits, StopBitsType sbit, ParityType parity);
[DllImport("libftdi1", CallingConvention = CallingConvention.Cdecl)] internal static extern int ftdi_set_line_property(ref ftdi_context ftdi, BitsType bits, StopBitsType sbit, ParityType parity);