我有一台通过 RS232C 接口连接到计算机的投币机 (SC350/360)。我有描述通信协议的技术文档,还包括一个用于操作机器的工作帕斯卡程序。我复制了 pascal 代码并在 Turbo Pascal 上进行了测试,使用 DosBox,Windows 7 64 位,代码编译成功。我现在想要实现的是将这些帕斯卡代码转换为 C#.Net,但我在将几行代码转换为 C# 时遇到了困难,因为我在串行端口编程方面没有太多经验。
这是 Pascal 中用于初始化与机器通信的代码。(将波特率设置为 9600,8 位,无奇偶校验,1 个停止位)
uses crt;
const
{ COM1: RS232 port address }
RXTX = $3F8; { $2F8 if COM2: is used }
ACK = 6;
NAK = 21;
ESC = 27;
var
dummy,
checkSum : integer;
key : char;
protocol : integer;
var i : integer;
begin
i := 1843200 div 9600 div 16;
port[RXTX + 3] := $80;
port[RXTX + 1] := hi(i);
port[RXTX]:= lo(i);
port[RXTX + 3] := 3;
port[RXTX + 4] := $A;
while odd(port[RXTX + 5]) do
begin
dummy := port[RXTX];
delay(10);
end;
end; { InitComm }
我想出的上述代码对应的 C# 是;(如果我错了,请纠正我)
SerialPort port=new SerialPort("COM1",9600,Parity.None,8,StopBits.One);
但我不明白如何转换其余的帕斯卡程序。其中一些我遇到困难的程序是;
procedure Tx(data : integer);
{ Transmit a character on serial channel }
begin
while port[RXTX + 5] and $20 = 0 do;
port[RXTX] := data and $FF;
end; { Tx }
function RxWait : integer;
{ Waits for a character from serial channel }
begin
while not odd(port[RXTX + 5]) do;
RxWait := port[RXTX];
end; { RxWait }
procedure Tx2(data : integer);
{ Transmit a char on serial channel + Calculate check sum }
begin
Tx(data);
checkSum := (checkSum + data) and $FF;
end; { Tx2 }
你们能帮我如何将这些帕斯卡代码转换为等效的 C# 吗?我知道我可以使用“port.Write”方法写入端口,但这不能完全适合端口数组的涡轮帕斯卡代码。(例如port[RXTX + 3] := $80;
)我不知道端口数组索引“RXTX+3”是什么指的是与C#有关。
如果您能帮我解决这个问题,我将不胜感激,我希望我能学会自己转换其余的帕斯卡代码。:)
使用我从这里的好人那里得到的帮助,我已经为 pascal 程序编写了以下等效的 C# 代码。如果我在代码中犯了错误,请纠正我。
public void Tx(int data)
{
if (!port.IsOpen)
port.Open();
port.Write(new byte[] { (byte)(data & 0xFF) }, 0, 1);
port.Close();
}
/// <summary>
/// Wait for a character from serial channel
/// </summary>
/// <returns></returns>
public int RxWait()
{
if (!port.IsOpen)
port.Open();
int readByte = port.ReadByte();
port.Close();
return readByte;
}
/// <summary>
/// Transmit a char on serial channel + Calculate check sum
/// </summary>
/// <param name="data"></param>
public void Tx2(int data)
{
Tx(data);
checkSum = (checkSum + data) & 0xFF;
}
顺便说一下,这里是设备文档中描述的协议。
Computer SC 350/360
–––––––> ESC (message start)
–––––––> Command
<––––––> Data (direction depends on command)
<––––––> Check sum (direction depends on command)
<––––––– Receipt:
- ACK (if check sum is correct) or
- NAK (if check sum is incorrect)
此外,我在这里为其余代码提供了一个发送命令以计算硬币数量的示例。
/// <summary>
/// Transmit command (no data) on serial channel
/// </summary>
/// <param name="c1"></param>
/// <param name="c2"></param>
/// <param name="sendCheckSum"></param>
public void TxCommand(char c1, char c2, bool sendCheckSum)
{
Tx(ESC);
checkSum = 0;
Tx2((int)c1);
Tx2((int)c2);
if (sendCheckSum)
{
Tx2(checkSum);
dummy = RxWait();
}
}
/// <summary>
/// Read n bytes from serial channel
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
public double ReadNumber(int n)
{
double number;
int i;
number = checkSum = 0;
for (i = 0; i < n; i++)
number = number * 256 + RxWait();
dummy = RxWait();
return number;
}
/// <summary>
/// Read the number of Coins counted
/// </summary>
/// <returns>Number of Coins</returns>
public double ReadCountReg()
{
TxCommand('R', 'C', false);
double coinsCounted = ReadNumber(4);
dummy = RxWait();
return coinsCounted;
}
发送计数硬币的命令;
double coinsCounted = ReadCountReg();
Console.WriteLine(Math.Round(coinsCounted, 0) + " coins counted");
读取计数寄存器的协议是;
Computer SC 350/360
–––––––> ESC
–––––––> “R”
–––––––> “C”
<––––––– CountRegister (CR)
<––––––– (CR ^ FF00,000016 + CR ^ FF,000016 + CR ^ FF0016 +
CR ^ FF16) ^ FF16
<––––––– ACK