public void SendByASCII(Dictionary <int, int> sendcontexts) { SendContexts = sendcontexts; StringBuilder sb = null; SerialPortDao serialPortDao = SerialPortDao.GetSerialPortDao(); serialPortDao.Open(); try { //KeyValuePair<int, int> sendcontext = sendcontexts[1]; foreach (KeyValuePair <int, int> sendcontext in sendcontexts) { //$001,1234# 显示 1234;其中 001 是屏的地址码。若是 3 位数码管,只能发送 3 个 ASCII 字符。 //$001,# 不显示 sb = new StringBuilder(); sb.Append("$"); sb.Append((sendcontext.Key + 10).ToString("D3")); //物理地址大于仓道号10 sb.Append(","); if (sendcontext.Value == 0) { sb.Append(""); } else { sb.Append(sendcontext.Value); } sb.Append("#"); serialPortDao.SendData(sb.ToString()); Thread.Sleep(30); } serialPortDao.Close(); } catch (Exception) { throw; } finally { serialPortDao.Close(); } }
static public void SendByMODBUS(Dictionary <int, int> sendcontexts) { SerialPortDao serialPortDao = SerialPortDao.GetSerialPortDao(); byte[] sendcode = null; try { if (sendcontexts != null) { serialPortDao.Open(); } foreach (KeyValuePair <int, int> sendcontext in sendcontexts) { if (sendcontext.Value > 0) { byte[] recevedata = new byte[2]; byte address; const byte functioncode = 0x06; byte[] register = new byte[2]; byte[] code = new byte[2]; byte[] crc = new byte[2]; sendcode = new byte[8]; address = Convert.ToByte(sendcontext.Key + 10); string a = sendcontext.Value.ToString("x4"); code = strToToHexByte(a); sendcode[0] = address; sendcode[1] = functioncode; sendcode[2] = register[0]; sendcode[3] = register[1]; sendcode[4] = code[0]; sendcode[5] = code[1]; crc = CRC.crc_16(sendcode, 6); sendcode[6] = crc[6]; sendcode[7] = crc[7]; //cobsendcontext.Items.Clear(); foreach (byte b in sendcode) { StringBuilder sb = new StringBuilder(); sb.Append(b.ToString("X2")); //cobsendcontext.Items.Add(sb.ToString()); } serialPortDao.SendCommand(sendcode, ref recevedata, 3); //Thread.Sleep(5); } else { byte[] recevedata = new byte[2]; byte address; const byte functioncode = 0x10; byte[] register = new byte[2]; byte[] registercount = new byte[2]; byte[] code = new byte[2]; byte[] crc = new byte[2]; sendcode = new byte[21]; address = Convert.ToByte(sendcontext.Key + 10); sendcode[0] = address; sendcode[1] = functioncode; sendcode[2] = 0; sendcode[3] = 112; sendcode[4] = 0; sendcode[5] = 6; sendcode[6] = 12; for (int i = 7; i <= 18; i++) { sendcode[i] = 0; } crc = CRC.crc_16(sendcode, 19); sendcode[19] = crc[19]; sendcode[20] = crc[20]; //cobsendcontext.Items.Clear(); foreach (byte b in sendcode) { StringBuilder sb = new StringBuilder(); sb.Append(b.ToString("X2")); //cobsendcontext.Items.Add(sb.ToString()); } serialPortDao.SendCommand(sendcode, ref recevedata, 3); //Thread.Sleep(10); } } //serialPortDao.Close(); } catch (Exception) { throw; } finally { //serialPortDao.Close(); } }