C 串列埠通訊總結

2022-01-23 15:58:32 字數 3086 閱讀 6933

在c#串列埠通訊開發過程中有的產家只提供通訊協議,這時候開發人員要自己根據協議來封裝方法,有的產家比較人性化提供了封裝好的通訊協議方法供開發人員呼叫。

1、只提供通訊協議(例如,今年早些時候開發的出鈔機):

如:///

/// 出鈔 dispense(0x45)

///

public void chuchao()

try;

int bcc = 0;

for (int i = 0; i < send.length - 1; i++)

bcc ^= send[i];

send[send.length - 1] = (byte)bcc;

sp.write(send, 0, send.length);

log("操作命令:" + getbytesstring(send, 0, send.length, " "));

for (int i = 0; i < 3; i++)

else

}byte recive = new byte[14];

for (int i = 0; i < 3; i++)

if (bcc != recive[recive.length - 1])

, 0, 1);

log("收到資訊:" + getbytesstring(recive, 0, recive.length, " ") + ",接收標識:nck:失敗");

}else

, 0, 1);

log("收到資訊:" + getbytesstring(recive, 0, recive.length, " ") + ",接收標識:ack:成功");

break;}}

error errorcode = new error();

errorcode.code = recive[9];

log("接收到的命令:" + recive[3] + ",接收到的錯誤碼:" + errorcode.code + "--" + errorcode.errormsg);

if (recive[3] != dipsense || errorcode.code > 0x31)

}catch (exception e)

finally}}

2、產家提供通訊協議方法(例如,今年早些時候開發的銀行卡支付機):

這時候你只要在你的專案bin/debug下面新增產家提供的dll,然後,再這樣:

/// /// 

開啟串列埠

///

///

串口號字串

/// ///

串列埠檔案控制代碼

///

備註:必須先呼叫此函式,獲得指定串列埠的串列埠檔案控制代碼,才可呼叫其他函式。

///

可以同時開啟多個串列埠,獲得多個串列埠檔案控制代碼,但不能多次開啟同乙個串列埠。

///

使用完畢後,必須呼叫commclose()關閉串列埠。

///

[dllimport("crt_310.dll", entrypoint = "commopen", setlasterror = true, charset = charset.ansi, exactspelling = true, callingconvention = callingconvention.stdcall)]

public static extern

intptr commopen(string port);

/// ///

按指定的波特率開啟串列埠 (該函式完成的功能= commopen 函式+ commsetting 函式)

///

///

串口號字串

///

指定波特率

///

波特率=1200,2400,4800,9600,19200,38400。

///

例如:commopen("com1",9600);

///

///

串列埠檔案控制代碼

///

備註:必須先呼叫此函式,獲得指定串列埠的串列埠檔案控制代碼,才可呼叫其他函式。

///

可以同時開啟多個串列埠,獲得多個串列埠檔案控制代碼,但不能多次開啟同乙個串列埠。

///

使用完畢後,必須呼叫commclose()關閉串列埠。

[dllimport("crt_310.dll", entrypoint = "commopenwithbaut", setlasterror = true, charset = charset.ansi, exactspelling = true, callingconvention = callingconvention.stdcall)]

public static extern

intptr commopenwithbaut(string port, uint data);

然後就可以像這樣呼叫了:

public string cardboxpositiontoread()

data = packagek100dll.m100a_checkcardposition(comhandle, false, 0, cardstates, recordinfo);

if (data != 0)

switch (cardstates[0])

break;

//通道有卡

default:

packagek100dll.m100a_commclose(comhandle);

return

"請取走卡片或者業務正在辦理,請稍候!";

}switch (cardstates[1])

packagek100dll.m100a_commclose(comhandle);

return

"true";}}

catch (exception)

}

C 串列埠通訊總結

在c 串列埠通訊開發過程中有的產家只提供通訊協議,這時候開發人員要自己根據協議來封裝方法,有的產家比較人性化提供了封裝好的通訊協議方法供開發人員呼叫。1 只提供通訊協議 例如,今年早些時候開發的出鈔機 如 出鈔 dispense 0x45 public void chuchao try int bc...

C 串列埠通訊

串列埠通訊類 serialportdao.csusing system using system.collections.generic using system.text using system.io.ports namespace ly.fuelstationpos.protocol set ...

C 串列埠通訊

本文 串列埠介紹 串列埠叫做序列介面,也稱序列通訊介面,按電氣標準及協議來分包括rs 232 rs 422 rs485 usb等。rs 232 c rs 422與rs 485標準只對介面的電氣特性做出規定,不涉及接外掛程式 電纜或協議。usb是近幾年發展起來的新型介面標準,主要應用於高速資料傳輸領域...