I have a coin counter (SC350 / 360) that connects to a computer via the RS232C interface. I have technical documentation that describes communication protocols, and a pascal work program is also included to control the machine. I copied the pascal code and tested it on Turbo Pascal using DosBox, for Windows 7 64 bit, and the code compiles successfully. What I wanted to achieve was to convert these codes to C # .Net, but it was difficult for me to convert several lines to C #, since I do not have much experience programming on the serial port.
This was the code in Pascal to initialize communication with the machine. (Set baud rate to 9600, 8 bit, no parity, 1 stop bit)
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 }
# ; (, , )
SerialPort port=new SerialPort("COM1",9600,Parity.None,8,StopBits.One);
, pascal. , , - :
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 }
, , , #? , "port.Write", - (, port[RXTX + 3] := $80;). , "RXTX +3 ' #.
, , , .:)
# pascal, , . , , .
public void Tx(int data)
{
if (!port.IsOpen)
port.Open();
port.Write(new byte[] { (byte)(data & 0xFF) }, 0, 1);
port.Close();
}
public int RxWait()
{
if (!port.IsOpen)
port.Open();
int readByte = port.ReadByte();
port.Close();
return readByte;
}
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)
, .
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();
}
}
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;
}
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