From e3d223c756b56a89ac735ecb4304bc67cdc32ce8 Mon Sep 17 00:00:00 2001 From: ShendoXT Date: Sun, 15 Jan 2023 23:00:00 +0100 Subject: [PATCH] Added Unirom via TCP --- MemcardRex/GUI/cardReaderWindow.cs | 111 +++++---- MemcardRex/GUI/mainWindow.Designer.cs | 3 + MemcardRex/GUI/mainWindow.cs | 36 ++- MemcardRex/GUI/preferencesWindow.Designer.cs | 4 +- MemcardRex/Hardware/Unirom.cs | 247 ++++++++++++++----- 5 files changed, 296 insertions(+), 105 deletions(-) diff --git a/MemcardRex/GUI/cardReaderWindow.cs b/MemcardRex/GUI/cardReaderWindow.cs index 387147d..1b07e3d 100644 --- a/MemcardRex/GUI/cardReaderWindow.cs +++ b/MemcardRex/GUI/cardReaderWindow.cs @@ -43,10 +43,13 @@ enum DeviceId:int { DexDrive, MemCARDuino, PS1CardLink, - PS3MemCardAdaptor, - Unirom + PS1CardLinkTCP, + Unirom, + UniromTCP, + PS3MemCardAdaptor }; + //Active device ID int currentDeviceIdentifier; public cardReaderWindow() @@ -158,7 +161,7 @@ public byte[] readMemoryCardPS1CLnk(Form hostWindow, string applicationName, str mainProgressBar.Maximum = 1024; //Set current device to PS1CardLink - currentDeviceIdentifier = (int) DeviceId.MemCARDuino; + currentDeviceIdentifier = (int) DeviceId.PS1CardLink; //Set window title and information this.Text = "PS1CardLink communication"; @@ -215,21 +218,9 @@ public byte[] readMemoryCardPS3MCA(Form hostWindow, string applicationName, stri else return null; } - //Read a Memory Card from Unirom - public byte[] readMemoryCardUnirom(Form hostWindow, string applicationName, string comPort, string remoteAddress, int remotePort, int cardSlot) + //Set everything for Unirom read be it via serial or TCP + public byte[] setupUniromRead(Form hostWindow, string applicationName, string errorString) { - string errorString; - - //Initialize Unirom - if (remoteAddress.Length > 0) - { - errorString = uniromDevice.StartUniromTCP(remoteAddress, remotePort); - } - else - { - errorString = uniromDevice.StartUnirom(comPort, cardSlot, (int) Unirom.Mode.Read, 1024); - } - //Check if there were any errors if (errorString != null) { @@ -245,9 +236,6 @@ public byte[] readMemoryCardUnirom(Form hostWindow, string applicationName, stri //Set scale for progress bar mainProgressBar.Maximum = 1024; - //Set current device to Unirom - currentDeviceIdentifier = (int) DeviceId.Unirom; - //Set window title and information this.Text = "Unirom communication"; infoLabel.Text = "Waiting for Unirom to store contents in RAM.\nTransfer will start after all the sectors have been read."; @@ -274,8 +262,32 @@ public byte[] readMemoryCardUnirom(Form hostWindow, string applicationName, stri else return null; } + //Read a Memory Card from Unirom via TCP + public byte[] readMemoryCardUniromTCP(Form hostWindow, string applicationName, string remoteAddress, int remotePort, int cardSlot) + { + //Initialize Unirom via TCP + string errorString = uniromDevice.StartUniromTCP(remoteAddress, remotePort, cardSlot, (int)Unirom.Mode.Read, 1024); + + //Set current device to Unirom via TCP + currentDeviceIdentifier = (int)DeviceId.UniromTCP; + + return setupUniromRead(hostWindow, applicationName, errorString); + } + + //Read a Memory Card from Unirom via Serial + public byte[] readMemoryCardUnirom(Form hostWindow, string applicationName, string comPort, int cardSlot) + { + //Initialize Unirom + string errorString = uniromDevice.StartUnirom(comPort, cardSlot, (int) Unirom.Mode.Read, 1024); + + //Set current device to Unirom + currentDeviceIdentifier = (int)DeviceId.Unirom; + + return setupUniromRead(hostWindow, applicationName, errorString); + } + //Write a Memory Card to DexDrive - public void writeMemoryCardDexDrive(Form hostWindow, string applicationName, string comPort, byte[] memoryCardData, int frameNumber) + public void writeMemoryCardDexDrive(Form hostWindow, string applicationName, string comPort, byte[] memoryCardData, int frameNumber) { //Initialize DexDrive string errorString = dexDevice.StartDexDrive(comPort); @@ -444,24 +456,9 @@ public void writeMemoryCardPS3MCA(Form hostWindow, string applicationName, strin PS3MCA.StopPS3MemCardAdaptor(); } - //Write a Memory Card to Unirom - public void writeMemoryCardUnirom(Form hostWindow, string applicationName, string comPort, int cardSlot, string remoteAddress, int remotePort, byte[] memoryCardData, int frameNumber) + //Set up everything for Unirom write be it serial or TCP + private void setupUniromWrite(Form hostWindow, string applicationName, byte[] memoryCardData, int frameNumber, string errorString) { - string errorString; - - //Store checksum before opening port - uniromDevice.LastChecksum = uniromDevice.CalculateChecksum(memoryCardData); - - //Initialize Unirom - if (remoteAddress.Length > 0) - { - errorString = uniromDevice.StartUniromTCP(remoteAddress, remotePort); - } - else - { - errorString = uniromDevice.StartUnirom(comPort, cardSlot, (int) Unirom.Mode.Write, frameNumber); - } - //Check if there were any errors if (errorString != null) { @@ -477,9 +474,6 @@ public void writeMemoryCardUnirom(Form hostWindow, string applicationName, strin //Set scale for progress bar mainProgressBar.Maximum = frameNumber / 16; - //Set current device to Unirom - currentDeviceIdentifier = (int)DeviceId.Unirom; - //Set window title and information this.Text = "Unirom communication"; infoLabel.Text = "Writing data to Unirom..."; @@ -496,6 +490,33 @@ public void writeMemoryCardUnirom(Form hostWindow, string applicationName, strin uniromDevice.StopUnirom(); } + //Write a Memory Card to Unirom via TCP + public void writeMemoryCardUniromTCP(Form hostWindow, string applicationName, string remoteAddress, int remotePort, int cardSlot, byte[] memoryCardData, int frameNumber) + { + //Store checksum before opening port + uniromDevice.LastChecksum = uniromDevice.CalculateChecksum(memoryCardData); + + //Set current device to Unirom + currentDeviceIdentifier = (int)DeviceId.UniromTCP; + + string errorString = uniromDevice.StartUniromTCP(remoteAddress, remotePort, cardSlot, (int)Unirom.Mode.Write, frameNumber); + + setupUniromWrite(hostWindow, applicationName, memoryCardData, frameNumber, errorString); + } + + //Write a Memory Card to Unirom via serial + public void writeMemoryCardUnirom(Form hostWindow, string applicationName, string comPort, int cardSlot, byte[] memoryCardData, int frameNumber) + { + //Store checksum before opening port + uniromDevice.LastChecksum = uniromDevice.CalculateChecksum(memoryCardData); + + //Set current device to Unirom + currentDeviceIdentifier = (int)DeviceId.Unirom; + + string errorString = uniromDevice.StartUnirom(comPort, cardSlot, (int) Unirom.Mode.Write, frameNumber); + + setupUniromWrite(hostWindow, applicationName, memoryCardData, frameNumber, errorString); + } private void OKbutton_Click(object sender, EventArgs e) { @@ -529,6 +550,7 @@ private void backgroundReader_DoWork(object sender, DoWorkEventArgs e) break; case (int) DeviceId.PS1CardLink: + case (int) DeviceId.PS1CardLinkTCP: tempDataBuffer = PS1CLnk.ReadMemoryCardFrame(i); break; @@ -537,6 +559,7 @@ private void backgroundReader_DoWork(object sender, DoWorkEventArgs e) break; case (int)DeviceId.Unirom: + case (int)DeviceId.UniromTCP: tempDataBuffer = uniromDevice.ReadMemoryCardFrame(i); break; } @@ -556,7 +579,7 @@ private void backgroundReader_DoWork(object sender, DoWorkEventArgs e) private void backgroundReader_ProgressChanged(object sender, ProgressChangedEventArgs e) { - if(currentDeviceIdentifier == (int)DeviceId.Unirom) + if(currentDeviceIdentifier == (int)DeviceId.Unirom || currentDeviceIdentifier == (int)DeviceId.UniromTCP) { if(!mainProgressBar.Visible && uniromDevice.StoredInRam) { @@ -581,7 +604,7 @@ private void backgroundWriter_DoWork(object sender, DoWorkEventArgs e) int frameSize = 128; //Unirom works with 2048 byte chunks - if (currentDeviceIdentifier == (int)DeviceId.Unirom) frameSize = 2048; + if (currentDeviceIdentifier == (int)DeviceId.Unirom || currentDeviceIdentifier == (int)DeviceId.UniromTCP) frameSize = 2048; byte[] tempDataBuffer = new byte[frameSize]; ushort i = 0; @@ -611,6 +634,7 @@ private void backgroundWriter_DoWork(object sender, DoWorkEventArgs e) break; case (int) DeviceId.PS1CardLink: + case (int) DeviceId.PS1CardLinkTCP: lastStatus = PS1CLnk.WriteMemoryCardFrame(i, tempDataBuffer); break; @@ -619,6 +643,7 @@ private void backgroundWriter_DoWork(object sender, DoWorkEventArgs e) break; case (int)DeviceId.Unirom: + case (int)DeviceId.UniromTCP: lastStatus = uniromDevice.WriteMemoryCardChunk(i, tempDataBuffer); break; } diff --git a/MemcardRex/GUI/mainWindow.Designer.cs b/MemcardRex/GUI/mainWindow.Designer.cs index d267a3c..123bfab 100644 --- a/MemcardRex/GUI/mainWindow.Designer.cs +++ b/MemcardRex/GUI/mainWindow.Designer.cs @@ -498,6 +498,7 @@ private void InitializeComponent() this.uniromOverTCPToolStripMenuItem.Name = "uniromOverTCPToolStripMenuItem"; this.uniromOverTCPToolStripMenuItem.Size = new System.Drawing.Size(215, 22); this.uniromOverTCPToolStripMenuItem.Text = "Unirom over TCP"; + this.uniromOverTCPToolStripMenuItem.Click += new System.EventHandler(this.uniromOverTCPToolStripMenuItem_Click); // // toolStripMenuItem16 // @@ -584,6 +585,7 @@ private void InitializeComponent() this.uniromOverTCPToolStripMenuItem1.Name = "uniromOverTCPToolStripMenuItem1"; this.uniromOverTCPToolStripMenuItem1.Size = new System.Drawing.Size(215, 22); this.uniromOverTCPToolStripMenuItem1.Text = "Unirom over TCP"; + this.uniromOverTCPToolStripMenuItem1.Click += new System.EventHandler(this.uniromOverTCPToolStripMenuItem1_Click); // // toolStripMenuItem22 // @@ -668,6 +670,7 @@ private void InitializeComponent() this.uniromOverTCPToolStripMenuItem2.Name = "uniromOverTCPToolStripMenuItem2"; this.uniromOverTCPToolStripMenuItem2.Size = new System.Drawing.Size(215, 22); this.uniromOverTCPToolStripMenuItem2.Text = "Unirom over TCP"; + this.uniromOverTCPToolStripMenuItem2.Click += new System.EventHandler(this.uniromOverTCPToolStripMenuItem2_Click); // // toolStripMenuItem24 // diff --git a/MemcardRex/GUI/mainWindow.cs b/MemcardRex/GUI/mainWindow.cs index 5125b3a..290162b 100644 --- a/MemcardRex/GUI/mainWindow.cs +++ b/MemcardRex/GUI/mainWindow.cs @@ -2283,7 +2283,11 @@ private void formatHardwareCard(int hardDevice) break; case 5: //Unirom - new cardReaderWindow().writeMemoryCardUnirom(this, appName, mainSettings.communicationPort, mainSettings.cardSlot, "", 0, blankCard.saveMemoryCardStream(true), frameNumber); + new cardReaderWindow().writeMemoryCardUnirom(this, appName, mainSettings.communicationPort, mainSettings.cardSlot, blankCard.saveMemoryCardStream(true), frameNumber); + break; + + case 6: //Unirom via TCP + new cardReaderWindow().writeMemoryCardUniromTCP(this, appName, mainSettings.remoteCommunicationAddress,mainSettings.remoteCommunicationPort, mainSettings.cardSlot, blankCard.saveMemoryCardStream(true), frameNumber); break; } } @@ -2329,7 +2333,7 @@ private void exportRAWSaveToolStripMenuItem1_Click(object sender, EventArgs e) private void uniromToolStripMenuItem_Click(object sender, EventArgs e) { //Read a Memory Card from Unirom - byte[] tempByteArray = new cardReaderWindow().readMemoryCardUnirom(this, appName, mainSettings.communicationPort, "", 0, mainSettings.cardSlot); + byte[] tempByteArray = new cardReaderWindow().readMemoryCardUnirom(this, appName, mainSettings.communicationPort, mainSettings.cardSlot); cardReaderRead(tempByteArray); } @@ -2342,7 +2346,7 @@ private void uniromToolStripMenuItem1_Click(object sender, EventArgs e) if (PScard.Count > 0) { //Open Unirom communication window - new cardReaderWindow().writeMemoryCardUnirom(this, appName, mainSettings.communicationPort, mainSettings.cardSlot, "", 0, PScard[listIndex].saveMemoryCardStream(getSettingsBool(mainSettings.fixCorruptedCards)), 1024); + new cardReaderWindow().writeMemoryCardUnirom(this, appName, mainSettings.communicationPort, mainSettings.cardSlot, PScard[listIndex].saveMemoryCardStream(getSettingsBool(mainSettings.fixCorruptedCards)), 1024); } } @@ -2351,5 +2355,31 @@ private void uniromToolStripMenuItem2_Click(object sender, EventArgs e) //Format a Memory Card on Unirom formatHardwareCard(5); } + + private void uniromOverTCPToolStripMenuItem_Click(object sender, EventArgs e) + { + //Read a Memory Card from Unirom via TCP + byte[] tempByteArray = new cardReaderWindow().readMemoryCardUniromTCP(this, appName, mainSettings.remoteCommunicationAddress, mainSettings.remoteCommunicationPort, mainSettings.cardSlot); + + cardReaderRead(tempByteArray); + } + + private void uniromOverTCPToolStripMenuItem1_Click(object sender, EventArgs e) + { + int listIndex = mainTabControl.SelectedIndex; + + //Check if there are any cards to write + if (PScard.Count > 0) + { + //Open Unirom communication window + new cardReaderWindow().writeMemoryCardUniromTCP(this, appName, mainSettings.remoteCommunicationAddress, mainSettings.remoteCommunicationPort, mainSettings.cardSlot, PScard[listIndex].saveMemoryCardStream(getSettingsBool(mainSettings.fixCorruptedCards)), 1024); + } + } + + private void uniromOverTCPToolStripMenuItem2_Click(object sender, EventArgs e) + { + //Format a Memory Card on Unirom via TCP + formatHardwareCard(6); + } } } diff --git a/MemcardRex/GUI/preferencesWindow.Designer.cs b/MemcardRex/GUI/preferencesWindow.Designer.cs index 5ca57b7..efe21f5 100644 --- a/MemcardRex/GUI/preferencesWindow.Designer.cs +++ b/MemcardRex/GUI/preferencesWindow.Designer.cs @@ -332,9 +332,9 @@ private void InitializeComponent() this.cardSlotLabel.AutoSize = true; this.cardSlotLabel.Location = new System.Drawing.Point(248, 136); this.cardSlotLabel.Name = "cardSlotLabel"; - this.cardSlotLabel.Size = new System.Drawing.Size(153, 13); + this.cardSlotLabel.Size = new System.Drawing.Size(86, 13); this.cardSlotLabel.TabIndex = 111; - this.cardSlotLabel.Text = "PS1CardLink/Unirom card slot:"; + this.cardSlotLabel.Text = "Unirom card slot:"; // // cardSlotCombo // diff --git a/MemcardRex/Hardware/Unirom.cs b/MemcardRex/Hardware/Unirom.cs index 5318a49..556a590 100644 --- a/MemcardRex/Hardware/Unirom.cs +++ b/MemcardRex/Hardware/Unirom.cs @@ -5,25 +5,38 @@ using System; using System.IO.Ports; using System.Threading; +using System.Net.Sockets; +using System.Text; namespace UniromCommunication { class Unirom { - public enum Mode - { + public enum Mode { Read, Write }; - //Unirom communication port + enum ComMode { + Serial, + TCP + } + + //Unirom serial port private SerialPort OpenedPort = null; + //Unirom TCP comm + TcpClient OpenTcpClient = null; + NetworkStream TcpStream = null; + //Flag indicating if Unirom finished storing data to RAM on the PS1 private bool storedInRam = false; private UInt32 lastChecksum = 0; + //Serial or TCP + private int activeComMode; + public bool StoredInRam { get { return storedInRam; } @@ -69,70 +82,154 @@ public UInt32 CalculateChecksum(byte[] inBytes) return returnVal; } - public string StartUnirom(string ComPortName, int cardSlot, int mode, int frameCount) + //Clear input buffer + private void FlushInput() { - int timeout = 10; + if (activeComMode == (int)ComMode.Serial) + OpenedPort.DiscardInBuffer(); + else + PortReadExisting(); + } - //Define a port to open - OpenedPort = new SerialPort(ComPortName, 115200, Parity.None, 8, StopBits.One); - OpenedPort.ReadBufferSize = 4096; + //Write string data to opened port + private void PortWrite(string message) + { + if (activeComMode == (int)ComMode.Serial) + OpenedPort.Write(message); + else + { + char[] msgData = message.ToCharArray(); - //Try to open a selected port (in case of an error return a descriptive string) - try{ OpenedPort.Open(); } - catch (Exception e) { return e.Message; } + for(int i = 0; i < msgData.Length; i++) + { + TcpStream.WriteByte((byte) msgData[i]); + } + } + } + + //Write binary data to opened port + private void PortWrite(byte[] buffer, int offset, int count) + { + if (activeComMode == (int)ComMode.Serial) + OpenedPort.Write(buffer, offset, count); + else + TcpStream.Write(buffer, offset, count); + } + + //Read binary data from opened port + private void PortRead(byte[] buffer, int offset, int count) + { + if (activeComMode == (int)ComMode.Serial) + OpenedPort.Read(buffer, offset, count); + else + TcpStream.Read(buffer, offset, count); + } + + //Read single byte from the opened port + private int PortReadByte() + { + if (activeComMode == (int)ComMode.Serial) + return OpenedPort.ReadByte(); + else + return TcpStream.ReadByte(); + } + + //Read all available data from the port + private string PortReadExisting() + { + if (activeComMode == (int)ComMode.Serial) + return OpenedPort.ReadExisting(); + else + { + if (OpenTcpClient.Available < 1) return null; + + byte[] streamData = new byte[OpenTcpClient.Available]; + TcpStream.Read(streamData, 0, streamData.Length); + return Encoding.UTF8.GetString(streamData); + } + } + + //Return number of bytes waiting in the input buffer + private int PortBytesToRead() + { + if (activeComMode == (int)ComMode.Serial) + return OpenedPort.BytesToRead; + else + { + return OpenTcpClient.Available; + } + } + + //Return number of bytes waiting in the output buffer + private int PortBytesToWrite() + { + if (activeComMode == (int)ComMode.Serial) + return OpenedPort.BytesToWrite; + else + { + return 0; //We are lying here since .net doesn't support it + } + } + + //Communication init sequence + private string InitUnirom(string ComPortName, int cardSlot, int mode, int frameCount) + { + int timeout = 100; //Start clean - OpenedPort.DiscardInBuffer(); + FlushInput(); - if (mode == (int) Mode.Read) + if (mode == (int)Mode.Read) { - OpenedPort.Write("MCDN"); - Thread.Sleep(100); + PortWrite("MCDN"); + Thread.Sleep(200); //Check if this is Unirom - if (!"MCDNOKV2".Equals(OpenedPort.ReadExisting())) + if (!"MCDNOKV2".Equals(PortReadExisting())) return "Unirom was not detected on '" + ComPortName + "' port."; } else { - OpenedPort.Write("MCUP"); - Thread.Sleep(100); + PortWrite("MCUP"); + Thread.Sleep(200); //Check if this is Unirom - if (!"MCUPOKV2".Equals(OpenedPort.ReadExisting())) + if (!"MCUPOKV2".Equals(PortReadExisting())) return "Unirom was not detected on '" + ComPortName + "' port."; } + Console.WriteLine("Dotud"); + //Switch to V2 protocol - OpenedPort.Write("UPV2"); + PortWrite("UPV2"); //Wait for response from Unirom - while (OpenedPort.BytesToRead < 4) + while (PortBytesToRead() < 4) { Thread.Sleep(10); if (timeout > 0) timeout--; else break; } - if (!"OKAY".Equals(OpenedPort.ReadExisting())) + if (!"OKAY".Equals(PortReadExisting())) { return "Unirom was not detected on '" + ComPortName + "' port."; } //Send slot we want to use - OpenedPort.Write(byteFromUint32((UInt32) cardSlot), 0, 4); + PortWrite(byteFromUint32((UInt32)cardSlot), 0, 4); //Start transfer - if (mode == (int) Mode.Read) + if (mode == (int)Mode.Read) { - OpenedPort.Write("MCRD"); + PortWrite("MCRD"); } else { //Number of bytes to write - OpenedPort.Write(BitConverter.GetBytes(frameCount * 128), 0, 4); + PortWrite(BitConverter.GetBytes(frameCount * 128), 0, 4); //Checksum - OpenedPort.Write(BitConverter.GetBytes(lastChecksum), 0, 4); + PortWrite(BitConverter.GetBytes(lastChecksum), 0, 4); lastChecksum = 0; } @@ -141,22 +238,53 @@ public string StartUnirom(string ComPortName, int cardSlot, int mode, int frameC return null; } - public string StartUniromTCP(string Address, int Port) + public string StartUnirom(string ComPortName, int cardSlot, int mode, int frameCount) { - return null; + //This is serial communication + activeComMode = (int) ComMode.Serial; + + //Define a port to open + OpenedPort = new SerialPort(ComPortName, 115200, Parity.None, 8, StopBits.One); + OpenedPort.ReadBufferSize = 4096; + + //Try to open a selected port (in case of an error return a descriptive string) + try{ OpenedPort.Open(); } + catch (Exception e) { return e.Message; } + + return InitUnirom(ComPortName, cardSlot, mode, frameCount); + } + + public string StartUniromTCP(string remoteAddress, int remotePort, int cardSlot, int mode, int frameCount) + { + //This is TCP communication + activeComMode = (int) ComMode.TCP; + + //Try to set up comm link + try + { + OpenTcpClient = new TcpClient(remoteAddress, remotePort); + OpenTcpClient.ReceiveBufferSize = 4096; + OpenTcpClient.SendBufferSize = 4096; + + TcpStream = OpenTcpClient.GetStream(); + } + catch (Exception e) + { return e.Message; } + + return InitUnirom(remoteAddress + ":" + remotePort.ToString(), cardSlot, mode, frameCount); } public byte[] ReadMemoryCardFrame(int frameNumber) { byte[] tempData = new byte[16]; - int timeout = 10; + int timeout = 100; if (!storedInRam) { //If data is still not being ready wait for the next cycle - if (OpenedPort.BytesToRead < 12) + if (PortBytesToRead() < 12) { - Thread.Sleep(10); + Thread.Sleep(100); return null; } @@ -165,13 +293,13 @@ public byte[] ReadMemoryCardFrame(int frameNumber) byte[] sizeData = new byte[4]; //MCRD response - OpenedPort.Read(respData, 0, 4); + PortRead(respData, 0, 4); //Address in RAM - OpenedPort.Read(addressData, 0, 4); + PortRead(addressData, 0, 4); //Size of data - OpenedPort.Read(sizeData, 0, 4); + PortRead(sizeData, 0, 4); //Unirom finished storing data to RAM storedInRam = true; @@ -179,20 +307,20 @@ public byte[] ReadMemoryCardFrame(int frameNumber) Thread.Sleep(10); //Start dumping data - OpenedPort.Write("DUMP"); + PortWrite("DUMP"); //Wait for response - while (OpenedPort.BytesToRead < 16) + while (PortBytesToRead() < 16) { Thread.Sleep(10); } //Handshake - OpenedPort.Read(tempData, 0, 16); + PortRead(tempData, 0, 16); //Send address in RAM - OpenedPort.Write(addressData, 0, 4); - OpenedPort.Write(sizeData, 0, 4); + PortWrite(addressData, 0, 4); + PortWrite(sizeData, 0, 4); //Dumping will begin on the next cycle return null; @@ -203,26 +331,26 @@ public byte[] ReadMemoryCardFrame(int frameNumber) //Unirom requires more data request every 2048 bytes, or every 16 frames if (frameNumber != 0 && frameNumber % 16 == 0) { - OpenedPort.Write("MORE"); + PortWrite("MORE"); } - //Timeout if there is no data for 100ms - timeout = 10; - while (OpenedPort.BytesToRead < 128) + //Timeout if there is no data for 1000ms + timeout = 100; + while (PortBytesToRead() < 128) { Thread.Sleep(10); if (timeout > 0) timeout--; else return null; } //Read frame data - OpenedPort.Read(frameData, 0, 128); + PortRead(frameData, 0, 128); //If this was the last frame fetch checksum if (frameNumber == 1023) { - OpenedPort.Write("MORE"); - while (OpenedPort.BytesToRead < 4) ; - OpenedPort.Read(tempData, 0, 4); + PortWrite("MORE"); + while (PortBytesToRead() < 4) ; + PortRead(tempData, 0, 4); lastChecksum = Uint32FromByte(tempData); } @@ -234,23 +362,23 @@ public bool WriteMemoryCardChunk(ushort FrameNumber, byte[] ChunkData) { ulong chunkChecksum = 0; - OpenedPort.Write(ChunkData, 0, 2048); + PortWrite(ChunkData, 0, 2048); for (int j = 0; j < 2048; j++) { chunkChecksum += ChunkData[j]; } - while (OpenedPort.BytesToWrite != 0) + while (PortBytesToWrite() != 0) Thread.Sleep(1); string cmdBuffer = ""; while (cmdBuffer != "CHEK") { - if (OpenedPort.BytesToRead != 0) + if (PortBytesToRead() != 0) { - cmdBuffer += (char)OpenedPort.ReadByte(); + cmdBuffer += (char)PortReadByte(); } while (cmdBuffer.Length > 4) @@ -259,15 +387,15 @@ public bool WriteMemoryCardChunk(ushort FrameNumber, byte[] ChunkData) if (cmdBuffer == "CHEK") { - OpenedPort.Write(BitConverter.GetBytes(chunkChecksum), 0, 4); + PortWrite(BitConverter.GetBytes(chunkChecksum), 0, 4); Thread.Sleep(1); while (cmdBuffer != "MORE" && cmdBuffer != "ERR!") { - if (OpenedPort.BytesToRead != 0) + if (PortBytesToRead() != 0) { - char readVal = (char)OpenedPort.ReadByte(); + char readVal = (char)PortReadByte(); cmdBuffer += readVal; } while (cmdBuffer.Length > 4) @@ -291,8 +419,13 @@ public bool WriteMemoryCardChunk(ushort FrameNumber, byte[] ChunkData) //Cleanly close Unirom communication public void StopUnirom() { - OpenedPort.Close(); + if (activeComMode == (int) ComMode.Serial) + OpenedPort.Close(); + else + { + if (TcpStream != null) TcpStream.Close(); + if (TcpStream !=null) OpenTcpClient.Close(); + } } - } }