Hi, All
I meet a big problem, I don't have any idea to fix it.
sdk path: SDK_2.3.0_MAPS-KS22\boards\mapsks22\usb_examples\usb_device_cdc_vcom\bm
PC OS: window 7 64bit, MCU: mks22,
I use usb_device_cdc_vcom to develop ks22 .
I want use my PC to access virtual com port, so I write a sample code through WINDOWS API.
here is my source code on windows:
void setConfig() {
DCB dcbSerialParams = { 0 }; // Initializing DCB structure
COMMTIMEOUTS timeouts = { 0 };
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
GetCommState(hComm, &dcbSerialParams);
dcbSerialParams.BaudRate = CBR_9600; // Setting BaudRate = 9600
dcbSerialParams.ByteSize = 8; // Setting ByteSize = 8
dcbSerialParams.StopBits = ONESTOPBIT;// Setting StopBits = 1
dcbSerialParams.Parity = NOPARITY; // Setting Parity = None
//dcbSerialParams.fInX = 1;
//sdcbSerialParams.fOutX=1;
if (!SetCommState(hComm, &dcbSerialParams))
printf("SetCommState fail!\n");
GetCommTimeouts(hComm, &timeouts);
timeouts.ReadIntervalTimeout = 0; // in milliseconds
timeouts.ReadTotalTimeoutConstant = 500; // in milliseconds
timeouts.ReadTotalTimeoutMultiplier = 0; // in milliseconds
timeouts.WriteTotalTimeoutConstant = 500; // in milliseconds
timeouts.WriteTotalTimeoutMultiplier = 0; // in milliseconds
if(!SetCommTimeouts(hComm, &timeouts))
printf("SetCommTimeouts fail!\n");
}
int OpenCom() {
hComm = CreateFile("\\\\.\\COM12", // for COM1OM9 only use COM1
GENERIC_READ | GENERIC_WRITE, //Read/Write
0, // No Sharing
NULL, // No Security
CREATE_ALWAYS,// Open existing port only
0, // Non Overlapped I/O
NULL); // Null for Comm Devices
if (hComm == INVALID_HANDLE_VALUE) {
printf("Error in opening serial port\n");
return -1;
}
else
printf("opening serial port successful\n");
setConfig();
return 0;
}
void ComIO(PACKET_INFO packet_info) {
unsigned char pBuffer[7];
printf(" usize:%d\n", uBufSize);
memcpy(pBuffer, &packet_info, uBufSize);
for (int i = 0; i < uBufSize; i++)
printf("ComIO buffer %u is %d:\n", i, pBuffer[i]);
DWORD dNoOFBytestoWrite; // No of bytes to write into the port
DWORD dNoOfBytesWritten = 0; // No of bytes written to the port
dNoOFBytestoWrite = uBufSize;
printf("size of dNoOFBytestoWrite: %d\n", dNoOFBytestoWrite);
BOOL bRet = WriteFile(hComm, // Handle to the Serial port
pBuffer, // Data to be written to the port
dNoOFBytestoWrite, //No of bytes to write
&dNoOfBytesWritten, //Bytes written
NULL);
if (dNoOfBytesWritten > 0)
printf("write successful\n");
else
printf("write fail\n");
if (bRet)
{
printf("write good!\n");
}
else
{
DWORD dwError = GetLastError();
printf("error: %d\n", dwError);
}
}
int main(){
XXXX // give packet_info some value in here
OpenCom() ;
ComIO(packet_info);
}
My question is when I use my sample code to access port,
open com and write com is successful, but KS22(MCU) can not receive my send data.
but I use PUTTY to access port first, KS22 can receive my send data. after that I run my sample code, it works too.
ks22 can receive my send data through my sample code.
it seems like I need use PUTTY to open port and send data first, after that I can use my sample code to access port.
it seems like PUTTY do some communicate, that my sample code didn't do.
how can I modify my sample code, makes it work.(don't need to use PUTTY access port first)
I don't have any idea, who can help me please~
THX
BR,
Nancy
Hi Nancy:
PuTTY is open source software, I would suggest you download the PuTTY and check how PuTTY access com port.
Download PuTTY - a free SSH and telnet client for Windows
Regards
Daniel