public void Test1() { var dataList = new byte[] { 0x06, 0x00, 0x20, 0x00 }; var crc = CRC.CRC16(dataList); Console.WriteLine(PmjSerialPort.GetHexString(crc)); }
/// <summary> /// 判断是否已经打开 /// </summary> /// <param name="printerName"></param> /// <returns></returns> private bool OpenPmj(out string printerName) { if (string.IsNullOrEmpty(comboBoxPmj.Text)) { throw new Exception("喷码机端口不能为空"); } //首先关闭原来的串口 if (null != _pmjSerialPort) { var portName = _pmjSerialPort.GetPortName(); //比较端口的名称,如果端口名称已经改变,就需要重新建立连接 if (!portName.Equals(comboBoxPmj.Text)) { _pmjSerialPort.Close(); _pmjSerialPort = new PmjSerialPort(comboBoxPmj.Text, new PmjDataRecv()); } } //如果不存在的话,也需要重新建立 if (null == _pmjSerialPort) { _pmjSerialPort = new PmjSerialPort(comboBoxPmj.Text, new PmjDataRecv()); } return(_pmjSerialPort.HasPrinter(out printerName)); }
/// <summary> /// 开始打印数据 /// </summary> /// <param name="pmjSerialPort"></param> /// <param name="pLCSerialPort"></param> /// <param name="pageList"></param> /// <param name="timeOut"></param> public static void PrintList(PmjSerialPort pmjSerialPort, PLCSerialPort pLCSerialPort, List <int> pageList, int timeOut) { //开始打印文件 for (var i = 0; i < pageList.Count; i++) { PrintFile(pmjSerialPort, pLCSerialPort, pageList[i], i, 2000); } }
public static bool PrintFile(PmjSerialPort pmjSerialPort, PLCSerialPort plcSerialPort, int fileIndex, int index, int timeOut) { //设置打印的文件 pmjSerialPort.SetParameter((ushort)(fileIndex)); //设置打印机重启,现在修改了设备之后可能就不需要重启了,直接开始打印 //plcSerialPort.SetBitValue(105, 1); //重新连接打印机 //Thread.Sleep(500); //var indexSum = 0; //重新检测PLC的状态,知道获取到02状态 //while (true) //{ // var status = plcSerialPort.GetD10Status(); // if(status == 2) // { // break; // } // Thread.Sleep(200); // indexSum += 200; // if(indexSum > 3000) // { // throw new Exception("无法检测到02信号"); // } //} //重新连接 //if (!pmjSerialPort.ReConnectPrinter()) //{ // throw new Exception("重启后无法重新连接打印机"); //} Thread.Sleep(200); //初始化 WaitStatus2(plcSerialPort, 10000); //通知plc开始执行打印的指令 if (!plcSerialPort.SetBitValue(100 + index + 1, 1)) { throw new Exception("写入PLC指令失败"); } //开始执行打印 pmjSerialPort.Print(); //初始化 WaitStatus2(plcSerialPort, 10000); //打印完成之后,写入M20X指令 if (!plcSerialPort.SetBitValue(200 + index + 1, 1)) { throw new Exception("写入PLC状态指令失败"); } Console.WriteLine($"成功打印第{fileIndex + 1}个文件"); //打印完之后,等待200毫秒 Thread.Sleep(200); return(true); }
private void btnDownload_Click(object sender, EventArgs e) { try { //执行下载之前,先扫描是否存在打印机, 如果存在就下发,如果不存在就提示用户 if (null == _pmjSerialPort) { _pmjSerialPort = new PmjSerialPort(comboBoxPmj.Text, new PmjDataRecv()); } var flagPrinter = _pmjSerialPort.HasPrinter(out string printerName); lbPmjStatus.Text = printerName; if (!flagPrinter) { MessageBox.Show("没有扫描到打印机"); return; } //判断下发对象是否存在 if (_pmjDataList.Count == 0) { MessageBox.Show("请编辑下发对象"); return; } //获取File的列表 var cmbData = cmbFileList.SelectedItem as CmbDataItem; //下发下载的指令 var commandDownload = CommandFactory.GetDownloadCommand((byte)cmbData.Value); var result = _pmjSerialPort.SendCommand(commandDownload, out DataResult dataResult); if (!result) { MessageBox.Show("下发下载文件的命令执行失败"); return; } // foreach (var item in _pmjDataList) { //下发文本 if (item.DataType == EnumPmjData.文本) { var label = item.Control as Label; var settingPanel = (item.DataSource as TextSettingParameter).UserControl as TextSetting; var commandText = CommandFactory.GetTextCommand((ushort)(label.Left / 2), (ushort)(label.Top / 2), Encoding.Default.GetBytes(label.Text), (ushort)settingPanel.GetSelectFont().Value); var resultForSend = _pmjSerialPort.SendCommand(commandText, out DataResult resultF); if (!resultForSend) { MessageBox.Show("文本命令下发失败"); break; } } //下发序列号 if (item.DataType == EnumPmjData.序号) { var lable = item.Control as Label; var settingPanel = (item.DataSource as SerialNumberParameter).UserControl as SerialNumberSetting; var timeCommand = CommandFactory.GetSerialNumberCommand((ushort)lable.Text.Length, (ushort)settingPanel.GetFontValue(), (ushort)(lable.Left / 2), (ushort)(lable.Top / 2), (ushort)settingPanel.GetIntervalValue(), (uint)settingPanel.GetInitValue()); var resultForSend = _pmjSerialPort.SendCommand(timeCommand, out DataResult resultF); if (!resultForSend) { MessageBox.Show("序列号命令下发失败"); break; } } //下发设置的时间 if (item.DataType == EnumPmjData.时间) { var label = item.Control as Label; var settingPanel = (item.DataSource as TimeSettingParameter).UserControl as TimeSetting; var format = settingPanel.GetTimeFormat(); var fontSize = settingPanel.GetFongValue(); //获取时间的偏移量 var timeOffset = settingPanel.GetTimeOffSet(); var commandTime = CommandFactory.GetTimeCommand(format, (ushort)fontSize, (ushort)(label.Left / 2), (ushort)(label.Top / 2), timeOffset); var resultForSend = _pmjSerialPort.SendCommand(commandTime, out DataResult resultF); if (!resultForSend) { MessageBox.Show("时间命令下发失败"); break; } } //图片 if (item.DataType == EnumPmjData.图片 || item.DataType == EnumPmjData.二维码 || item.DataType == EnumPmjData.条码) { var control = (item.Control as PictureBox); var bitmap = control.Image as Bitmap; Console.WriteLine($"图片大小 width:{bitmap.Width} height:{bitmap.Height}"); var flagImage = _pmjSerialPort.WriteImageBuffer(bitmap, control.Left / 2, control.Top / 2); if (!flagImage) { MessageBox.Show("图片命令下发失败"); break; } } } //结束下载,首先获取x的最大的终点 var xEnd = GetEndX(); var command = CommandFactory.GetFinishDownloadCommand(0, (ushort)xEnd); var flag = _pmjSerialPort.SendCommand(command, out DataResult p); if (!flag) { MessageBox.Show("结束下载命令下发失败"); return; } MessageBox.Show("下载文件成功"); return; }catch (Exception ex) { MessageBox.Show(ex.Message); } }