SerialPort serialPort = new SerialPort();
StringBuilder result = new StringBuilder();
int stateNumber = 0;
string stateMessage = "정상";
try
{
serialPort.PortName = "COM2";// "COM1/COM2";
serialPort.BaudRate = 9600;
serialPort.Parity = Parity.None;
serialPort.DataBits = 8;
serialPort.StopBits = StopBits.One;
serialPort.Encoding = System.Text.Encoding.GetEncoding(949);
serialPort.Handshake = Handshake.None;
//serialPort.WriteTimeout = SerialPort.InfiniteTimeout;
//serialPort.ReadTimeout = SerialPort.InfiniteTimeout;
//serialPort.RtsEnable = true;
//serialPort.DtrEnable = true;
serialPort.ReceivedBytesThreshold = 1;
serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceived);
if (!serialPort.IsOpen)
{
serialPort.Open();
}
stateNumber = 1;
if (!serialPort.DsrHolding)
{
//"장치 전원이나 회선의 연결 상태를 확인하세요"
}
stateNumber = 2;
//"인쇄중"
if (serialPort.BytesToWrite > 0)
{
//인쇄 시작
}
string tempPrintDate = "";
//tempPrintDate += "^XA^LH0,0^LL450^FWB^FS"; //헤더
//tempPrintDate += "^FO0,0^A0I,35,30^FD-------------------^FS"; //입고수량
//tempPrintDate += "^XZ"; //' 유효기간
tempPrintDate += "^XA^LH0,0^LL410^FWB^FS^FO90,20^A0,35,30^FB290^FD89160-3X000^FS^FO130,0^A0,55,50^FB390^FDMDF12 RC^FS^FO180,0^A0,35,30^FB380^FDMDF12 112H NAT RC^FS^FO220,0^A0,35,30^FB380^FD5 K01 20130115^FS^BY2^FO260,-70^BC,80,Y,N,N^FD16730152831^FS^BY2,3,80^FO365,-50^BC,80,N,N,N^FDK0D1F26N112HE^FS^FO460,0^A0,25,20^FB300^FDK0D1F26N112HE^FS^FO460,40^FR^XGAEO,1,1^FS^FO0,0^A0I,35,30^FD-------------------^FS^XZ";
//tempPrintDate += "^XA^LH0,0^LL410^FWB^FS^FO90,20^A0,35,30^FB290^FD89160-3X000^FS^FO130,0^A0,55,50^FB390^FDMDF12 RC^FS^FO180,0^A0,35,30^FB380^FDMDF12 112H NAT RC^FS^FO220,0^A0,35,30^FB380^FD5 K01 20130115^FS^BY2^FO260,-70^BC,80,Y,N,N^FD16730152831^FS^BY2,3,80^FO365,-50^BC,80,N,N,N^FDK0D1F26N112HE^FS^FO460,0^A0,25,20^FB300^FDK0D1F26N112HE^FS^FO460,40^FR^XGAEO,1,1^FS^FO0,0^A0I,35,30^FD-------------------^FS^XZ";
//tempPrintDate += "^XA^LH0,0^LL410^FWB^FS^FO90,20^A0,35,30^FB290^FD89160-3X000^FS^FO130,0^A0,55,50^FB390^FDMDF12 RC^FS^FO180,0^A0,35,30^FB380^FDMDF12 112H NAT RC^FS^FO220,0^A0,35,30^FB380^FD5 K01 20130115^FS^BY2^FO260,-70^BC,80,Y,N,N^FD16730152831^FS^BY2,3,80^FO365,-50^BC,80,N,N,N^FDK0D1F26N112HE^FS^FO460,0^A0,25,20^FB300^FDK0D1F26N112HE^FS^FO460,40^FR^XGAEO,1,1^FS^FO0,0^A0I,35,30^FD-------------------^FS^XZ";
//tempPrintDate += "^XA^LH0,0^LL410^FWB^FS^FO90,20^A0,35,30^FB290^FD89160-3X000^FS^FO130,0^A0,55,50^FB390^FDMDF12 RC^FS^FO180,0^A0,35,30^FB380^FDMDF12 112H NAT RC^FS^FO220,0^A0,35,30^FB380^FD5 K01 20130115^FS^BY2^FO260,-70^BC,80,Y,N,N^FD16730152831^FS^BY2,3,80^FO365,-50^BC,80,N,N,N^FDK0D1F26N112HE^FS^FO460,0^A0,25,20^FB300^FDK0D1F26N112HE^FS^FO460,40^FR^XGAEO,1,1^FS^FO0,0^A0I,35,30^FD-------------------^FS^XZ";
//tempPrintDate += "^XA^LH0,0^LL410^FWB^FS^FO90,20^A0,35,30^FB290^FD89160-3X000^FS^FO130,0^A0,55,50^FB390^FDMDF12 RC^FS^FO180,0^A0,35,30^FB380^FDMDF12 112H NAT RC^FS^FO220,0^A0,35,30^FB380^FD5 K01 20130115^FS^BY2^FO260,-70^BC,80,Y,N,N^FD16730152831^FS^BY2,3,80^FO365,-50^BC,80,N,N,N^FDK0D1F26N112HE^FS^FO460,0^A0,25,20^FB300^FDK0D1F26N112HE^FS^FO460,40^FR^XGAEO,1,1^FS^FO0,0^A0I,35,30^FD-------------------^FS^XZ";
serialPort.Write(tempPrintDate);
serialPort.ReadExisting();
MessageBox.Show(serialPort.BytesToWrite.ToString());
stateNumber = 3;
if (serialPort.IsOpen)
{
serialPort.Close();
}
stateNumber = 4;
}
catch (Exception ex)
{
stateNumber = -1;
stateMessage = "Msg-" + ex.Message + " Source-" + ex.Source + " Error-" + stateNumber;
}
result.AppendLine("{");
result.AppendLine("\"s\":\"" + stateNumber + "\"");
result.AppendLine(",\"msg\":\"" + stateMessage + "\"");
result.AppendLine("}");
serialPort.BytesToWrite 为什么获取不到值呢?
获得的值 始终是0
LZ 代码写没怎么看明白,但是你应该在DataReced函数中接受数据,才能接收到发给串口的数据吧!