public bool Writebyte(int ByteAddress, Enums.StorageType storageType, Enums.StorageTypeIsV isV, int WriteValue) { if (WriteValue > 255) { return(false); }//最大写入值255 int i, Rece = 0; byte fcs; //位写操作返回都是字符串为:68 12 12 68 00 02 08 32 03 00 00 00 00 00 02 00 01 00 00 05 01 FF 47 16 byte[] ReceivesCheck = { 0x68, 0x12, 0x12, 0x68, 0x00, 0x02, 0x08, 0x32, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x05, 0x01, 0xFF, 0x47, 0x16 }; if (!this.serialPort1.IsOpen) { this.serialPort1.Open(); } ByteAddress = ByteAddress * 8; this.Wbyte[22] = 0x02; //Byte 22 为读取数据的长度,01: 1 Bit 02: 1 Byte 04: 1 Word 06: Double Word this.Wbyte[24] = 0x01; // Byte 24 为数据个数:这里是 01,一次读一个数据 this.Wbyte[26] = (byte)isV; this.Wbyte[27] = (byte)storageType; //偏移量,byte 28,29,30 存储器偏移量指针 (存储器地址 *8 ): // 如 VB100,存储器地址为 100,偏移量指针为 800,转换成 16 //进制就是 320H,则 Byte 28~29 这三个字节就是: 00 03 20 // buffer[11] = Convert.ToByte(address & 0xff);//地位,如320H,结果为20 //buffer[10] = Convert.ToByte((address / 0x100) & 0xff);//0x100 ->256 //buffer[9] = Convert.ToByte(address / 0x10000); this.Wbyte[28] = Convert.ToByte(ByteAddress / 0x10000); this.Wbyte[29] = Convert.ToByte((ByteAddress / 0x100) & 0xff); //0x100 ->256; this.Wbyte[30] = Convert.ToByte(ByteAddress & 0xff); //低位,如320H,结果为20; this.Wbyte[32] = 0x04; this.Wbyte[34] = 0x08; this.Wbyte[35] = Convert.ToByte(WriteValue); for (i = 4, fcs = 0; i < 36; i++) { fcs += this.Wbyte[i]; } int tt = Convert.ToInt32(fcs) % 256;//添加的代码 mod 256 this.Wbyte[36] = Convert.ToByte(tt); //再次发送指令 10 02 00 5C 5E 16 意思:请执行命令,其中,SD为起始符,始终为10H;DA为读指令的目的地址;SA源地址;FCS校验和,等于DA+SA+FC的数据和;ED始终为16H //SD DA SA FC FCS ED //10 02 00 5C 5E 16 for (i = 1, fcs = 0; i < 4; i++) { fcs += this.Affirm[i]; } tt = Convert.ToInt32(fcs) % 256;//添加的代码 mod 256 this.Affirm[4] = Convert.ToByte(tt); this.serialPort1.DiscardInBuffer(); this.serialPort1.DiscardOutBuffer(); this.serialPort1.Write(this.Wbyte, 0, this.Wbyte.Length); //while (this.serialPort1.BytesToRead == 0) {; } Thread.Sleep(100); Rece = this.serialPort1.ReadByte(); if (Rece == 0xE5) { this.serialPort1.DiscardInBuffer(); this.serialPort1.DiscardOutBuffer(); this.serialPort1.Write(this.Affirm, 0, 6); TimeSpan ts1 = new TimeSpan(DateTime.Now.Ticks);//返回的数据比在串口助手少第一个数据 0x68 PortTimeout(24, ts1, this.serialPort1); byte[] Receives = new byte[24]; this.serialPort1.Read(Receives, 0, 24); int n = 0; for (int j = 0; j < 24; j++) { if (Receives[j] != ReceivesCheck[j]) { n++; } } if (n == 0) { return(true); } else { return(false); } } else { return(false); } }
public bool ReadDWord(int Address, Enums.StorageType storageType, Enums.StorageTypeIsV isV, out long WordValue) { int i, Rece = 0; byte fcs; byte[] Receives = new byte[30]; if (!this.serialPort1.IsOpen) { this.serialPort1.Open(); } Address = Address * 8; this.Rbyte[22] = 0x06; //Byte 22 为读取数据的长度,01: 1 Bit 02: 1 Byte 04: 1 Word 06: Double Word this.Rbyte[24] = 0x01; //一次读取的个数 this.Rbyte[26] = (byte)isV; this.Rbyte[27] = (byte)storageType; this.Rbyte[28] = Convert.ToByte(Address / 0x10000); this.Rbyte[29] = Convert.ToByte((Address / 0x100) & 0xff); //0x100 ->256; this.Rbyte[30] = Convert.ToByte(Address & 0xff); //低位,如320H,结果为20; //this.Rbyte[28] = this.fbyte3; //this.Rbyte[29] = this.fbyte2; //this.Rbyte[30] = this.fbyte1; for (i = 4, fcs = 0; i < 31; i++) { fcs += this.Rbyte[i]; } int tt = Convert.ToInt32(fcs) % 256;//添加的代码 mod 256 this.Rbyte[31] = Convert.ToByte(tt); for (i = 1, fcs = 0; i < 4; i++) { fcs += this.Affirm[i]; } tt = Convert.ToInt32(fcs) % 256;//添加的代码 mod 256 this.Affirm[4] = Convert.ToByte(tt); // this.Affirm[4] = fcs; this.serialPort1.DiscardInBuffer(); this.serialPort1.DiscardOutBuffer(); this.serialPort1.Write(this.Rbyte, 0, Rbyte.Length); Thread.Sleep(200); Rece = this.serialPort1.ReadByte(); if (Rece == 0xE5) { this.serialPort1.DiscardInBuffer(); this.serialPort1.DiscardOutBuffer(); this.serialPort1.Write(this.Affirm, 0, 6); //for (i = 0; i < 10000; i++) // for (int j = 0; j < 1000; j++) ; // while (this.serialPort1.BytesToRead < 29) {; } Thread.Sleep(200); TimeSpan ts1 = new TimeSpan(DateTime.Now.Ticks); while (this.serialPort1.BytesToRead < 30)//返回的数据比在串口助手少第一个数据 0x68 { System.Threading.Thread.Sleep(100); TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks); TimeSpan ts = ts2.Subtract(ts1).Duration(); //如果接收时间超出设定时间,则直接退出 if (ts.TotalMilliseconds > 5000) { WordValue = 0; return(false); } }// 长度为30 this.serialPort1.Read(Receives, 0, 30); long d1 = Convert.ToInt32(Receives[25]);//用long类型避免出现负数 long c1 = d1 * 16777216; long c2 = Receives[26] * 0x10000; long c3 = Receives[27] * 256; long c4 = Receives[28]; WordValue = c1 + c2 + c3 + c4; return(true); } this.serialPort1.Close(); WordValue = 0; return(false); }
public bool Readbytes(int Address, int byteCount, Enums.StorageType storageType, Enums.StorageTypeIsV isV, out byte [] readValue) { if (byteCount > 222 || byteCount == 0 || byteCount < 0) { readValue = new byte[] { 0 }; return(false); } int i, Rece = 0; byte fcs; byte[] Receives = new byte[27 + byteCount]; if (!this.serialPort1.IsOpen) { this.serialPort1.Open(); } Address = Address * 8; this.Rbyte[22] = 0x02; //Byte 22 为读取数据的长度,01: 1 Bit 02: 1 Byte 04: 1 Word 06: Double Word this.Rbyte[24] = Convert.ToByte(byteCount); //一次读取的个数 this.Rbyte[26] = (byte)isV; this.Rbyte[27] = (byte)storageType; // buffer[11] = Convert.ToByte(address & 0xff);//地位,如320H,结果为20 //buffer[10] = Convert.ToByte((address / 0x100) & 0xff);//0x100 ->256 //buffer[9] = Convert.ToByte(address / 0x10000); this.Rbyte[28] = Convert.ToByte(Address / 0x10000); this.Rbyte[29] = Convert.ToByte((Address / 0x100) & 0xff); //0x100 ->256; this.Rbyte[30] = Convert.ToByte(Address & 0xff); //低位,如320H,结果为20; //this.Rbyte[28] = this.fbyte3; //this.Rbyte[29] = this.fbyte2; //this.Rbyte[30] = this.fbyte1; for (i = 4, fcs = 0; i < 31; i++) { fcs += this.Rbyte[i]; } int tt = Convert.ToInt32(fcs) % 256;//添加的代码 mod 256 this.Rbyte[31] = Convert.ToByte(tt); // this.Rbyte[31] = fcs; for (i = 1, fcs = 0; i < 4; i++) { fcs += this.Affirm[i]; } tt = Convert.ToInt32(fcs) % 256;//添加的代码 mod 256 this.Affirm[4] = Convert.ToByte(tt); // this.Affirm[4] = fcs; this.serialPort1.DiscardInBuffer(); this.serialPort1.DiscardOutBuffer(); this.serialPort1.Write(this.Rbyte, 0, Rbyte.Length); while (this.serialPort1.BytesToRead == 0) { ; } Rece = this.serialPort1.ReadByte(); if (Rece == 0xE5) { this.serialPort1.DiscardInBuffer(); this.serialPort1.DiscardOutBuffer(); this.serialPort1.Write(this.Affirm, 0, 6); Thread.Sleep(100); TimeSpan ts1 = new TimeSpan(DateTime.Now.Ticks); while (this.serialPort1.BytesToRead < 27)//返回的数据比在串口助手少第一个数据 0x68 { System.Threading.Thread.Sleep(100); TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks); TimeSpan ts = ts2.Subtract(ts1).Duration(); //如果接收时间超出设定时间,则直接退出 if (ts.TotalMilliseconds > 5000) { readValue = new byte[] { 0 }; return(false); } } this.serialPort1.Read(Receives, 0, Receives.Length); this.serialPort1.Close(); readValue = new byte[byteCount]; for (int j = 0; j < byteCount; j++) { readValue[j] = Receives[25 + j]; } return(true); } else { readValue = new byte[] { 0 }; return(false); } }//测试没有读到数据,还没有成功
public bool ReadWords(int Address, int WordCount, Enums.StorageType storageType, Enums.StorageTypeIsV isV, out byte [] WordValue) { if (WordCount > 255 || WordCount == 0 || WordCount < 0) { WordValue = new byte[] { 0 }; return(false); } int i, Rece = 0; byte fcs; byte[] Receives = new byte[28 + WordCount * 2]; if (!this.serialPort1.IsOpen) { this.serialPort1.Open(); } Address = Address * 8; this.Rbyte[22] = 0x04; //Byte 22 为读取数据的长度,01: 1 Bit 02: 1 Byte 04: 1 Word 06: Double Word this.Rbyte[24] = Convert.ToByte(WordCount); //一次读取的个数 this.Rbyte[26] = (byte)isV; this.Rbyte[27] = (byte)storageType; this.Rbyte[28] = Convert.ToByte(Address / 0x10000); this.Rbyte[29] = Convert.ToByte((Address / 0x100) & 0xff); //0x100 ->256; this.Rbyte[30] = Convert.ToByte(Address & 0xff); //低位,如320H,结果为20; //this.Rbyte[28] = this.fbyte3; //this.Rbyte[29] = this.fbyte2; //this.Rbyte[30] = this.fbyte1; for (i = 4, fcs = 0; i < 31; i++) { fcs += this.Rbyte[i]; } int tt = Convert.ToInt32(fcs) % 256;//添加的代码 mod 256 this.Rbyte[31] = Convert.ToByte(tt); for (i = 1, fcs = 0; i < 4; i++) { fcs += this.Affirm[i]; } tt = Convert.ToInt32(fcs) % 256;//添加的代码 mod 256 this.Affirm[4] = Convert.ToByte(tt); // this.Affirm[4] = fcs; this.serialPort1.DiscardInBuffer(); this.serialPort1.DiscardOutBuffer(); this.serialPort1.Write(this.Rbyte, 0, Rbyte.Length); Thread.Sleep(200); Rece = this.serialPort1.ReadByte(); if (Rece == 0xE5) { this.serialPort1.DiscardInBuffer(); this.serialPort1.DiscardOutBuffer(); this.serialPort1.Write(this.Affirm, 0, 6); //for (i = 0; i < 10000; i++) // for (int j = 0; j < 1000; j++) ; // while (this.serialPort1.BytesToRead < 29) {; } Thread.Sleep(200); TimeSpan ts1 = new TimeSpan(DateTime.Now.Ticks); while (this.serialPort1.BytesToRead < 27 + WordCount * 2)//返回的数据比在串口助手少第一个数据 0x68 { System.Threading.Thread.Sleep(100); TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks); TimeSpan ts = ts2.Subtract(ts1).Duration(); //如果接收时间超出设定时间,则直接退出 if (ts.TotalMilliseconds > 5000) { WordValue = new byte[] { 0 }; return(false); } }// 长度为30 WordValue = new byte[WordCount * 2]; this.serialPort1.Read(Receives, 0, 30); for (int j = 0; j < WordValue.Length; j++) { WordValue[j] = Receives[25 + j]; } //WordValue = (int)(Receives[25] * 256 + Receives[26]); return(true); } this.serialPort1.Close(); WordValue = new byte[] { 0 }; return(false); }
public bool Readbits(int ByteAddress, int bitnumber, int bitCount, Enums.StorageType storageType, Enums.StorageTypeIsV isV, out int[] bitValue) { if (bitCount > 255 || bitCount == 0 || bitCount < 0) { bitValue = new int[] { 0 }; return(false); } int i, Rece = 0; byte fcs; byte[] Receives = new byte[27 + bitCount]; if (!this.serialPort1.IsOpen) { this.serialPort1.Open(); } ByteAddress = ByteAddress * 8 + bitnumber; this.Rbyte[22] = 0x01; //Byte 22 为读取数据的长度,01: 1 Bit 02: 1 Byte 04: 1 Word 06: Double Word this.Rbyte[24] = Convert.ToByte(bitCount); // Byte 24 为数据个数:这里是 01,一次读一个数据 this.Rbyte[26] = (byte)isV; this.Rbyte[27] = (byte)storageType; //偏移量,byte 28,29,30 存储器偏移量指针 (存储器地址 *8 ): // 如 VB100,存储器地址为 100,偏移量指针为 800,转换成 16 //进制就是 320H,则 Byte 28~29 这三个字节就是: 00 03 20 // buffer[11] = Convert.ToByte(address & 0xff);//地位,如320H,结果为20 //buffer[10] = Convert.ToByte((address / 0x100) & 0xff);//0x100 ->256 //buffer[9] = Convert.ToByte(address / 0x10000); this.Rbyte[28] = Convert.ToByte(ByteAddress / 0x10000); this.Rbyte[29] = Convert.ToByte((ByteAddress / 0x100) & 0xff); //0x100 ->256; this.Rbyte[30] = Convert.ToByte(ByteAddress & 0xff); //低位,如320H,结果为20; for (i = 4, fcs = 0; i < 31; i++) { fcs += this.Rbyte[i]; } int tt = Convert.ToInt32(fcs) % 256;//添加的代码 mod 256 this.Rbyte[31] = Convert.ToByte(tt); //再次发送指令 10 02 00 5C 5E 16 意思:请执行命令,其中,SD为起始符,始终为10H;DA为读指令的目的地址;SA源地址;FCS校验和,等于DA+SA+FC的数据和;ED始终为16H //SD DA SA FC FCS ED //10 02 00 5C 5E 16 for (i = 1, fcs = 0; i < 4; i++) { fcs += this.Affirm[i]; } tt = Convert.ToInt32(fcs) % 256;//添加的代码 mod 256 this.Affirm[4] = Convert.ToByte(tt); this.serialPort1.DiscardInBuffer(); this.serialPort1.DiscardOutBuffer(); this.serialPort1.Write(this.Rbyte, 0, Rbyte.Length); while (this.serialPort1.BytesToRead == 0) { ; } Rece = this.serialPort1.ReadByte(); if (Rece == 0xE5) { this.serialPort1.DiscardInBuffer(); this.serialPort1.DiscardOutBuffer(); this.serialPort1.Write(this.Affirm, 0, 6); TimeSpan ts1 = new TimeSpan(DateTime.Now.Ticks); while (this.serialPort1.BytesToRead < Receives.Length - 1)//返回的数据比在串口助手少第一个数据 0x68 { System.Threading.Thread.Sleep(100); TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks); TimeSpan ts = ts2.Subtract(ts1).Duration(); //如果接收时间超出设定时间,则直接退出 if (ts.TotalMilliseconds > 5000) { bitValue = new int[] { 0 }; return(false); } }// 长度为27 this.serialPort1.Read(Receives, 0, Receives.Length); bitValue = new int[bitCount]; for (int j = 0; j < bitCount; j++) { bitValue[j] = (int)Receives[24 + j]; } return(true); } else { bitValue = new int[] { 0 }; return(false); } }//读取失败